Chapter 8: Distributed Email Service Design
Loading audio…
ⓘ This audio and summary are simplified educational interpretations and are not a substitute for the original text.
The design scope includes core functions like sending and receiving emails, fetching and filtering emails, advanced searching by subject and body, and anti-spam/anti-virus protection. The architecture moves beyond traditional mail servers, which relied on local file directories like Maildir and suffered from disk I/O bottlenecks and complexity, necessitating a distributed database solution. The modern system uses various protocols: SMTP (Simple Mail Transfer Protocol) for inter-server communication, and POP (Post Office Protocol) and IMAP (Internet Mail Access Protocol) for client retrieval, often supplemented by HTTPS for webmail or custom protocols like JMAP (JSON Meta Application Protocol) over WebSocket for real-time updates. The high-level architecture separates concerns into Web Servers, Real-time Servers, and a Storage Layer consisting of a Metadata Database, Attachment Store (like Amazon S3), Distributed Cache (like Redis), and a Search Store utilizing an inverted index for full-text queries. Both the email sending flow and receiving flow leverage distributed message queues to enable asynchronous processing and independent scaling of mail components. The database deep dive reveals that due to the isolation of user data and the large size of individual emails, highly customized NoSQL databases (such as one with Bigtable characteristics) are preferred over relational databases. To support queries for fetching read or unread emails efficiently in a NoSQL environment, the approach utilizes denormalization, splitting email records into separate tables. A key challenge is achieving high email deliverability, which involves establishing sender reputation by using dedicated IP addresses, monitoring feedback loops for soft and hard bounces, and implementing authentication protocols like SPF, DKIM, and DMARC to combat phishing. Search functionality, being write-heavy, can be implemented via external engines like Elasticsearch or highly optimized custom solutions that utilize structures like the Log-Structured Merge-Tree (LSM) to minimize disk I/O. Finally, the system design ensures high availability through a multi-data center setup with replicated data, prioritizing consistency for mailbox data over availability during failover.