Design a Distributed Email Service: SMTP Relays, Inbox Sharding & Spam Filtering
Why Modern Webmail Scales Beyond Legacy IMAP/POP3
Distributed Email Service Architecture
1. Protocols & Authentication Security
- Outgoing & Cross-Domain: SMTP relays deliver mail between providers (
gmail.com↔rudresh.in). - Web / Mobile Clients: Modern webmail uses HTTPS APIs + WebSockets (for instant push inbox updates) rather than verbose stateful IMAP polling, keeping IMAP/POP3 endpoints only for legacy third-party desktop clients.
- Anti-Spoofing Triad:
- SPF: DNS record whitelisting authorized sending IP ranges.
- DKIM: Cryptographic RSA/Ed25519 signature verifying message body & headers weren't tampered in transit.
- DMARC: Policy instructing receivers to quarantine or reject mail failing SPF/DKIM.
2. Separating Hot Metadata From Heavy Attachments
Incoming Email ──► [SMTP Gateway + Virus/Spam Classifier]
│
┌──────────┴──────────┐
▼ ▼
[Metadata NoSQL DB] [S3 Blob Storage]
(user_id, folder, ts, (Compressed, Encrypted
subject, body, att_ids) Deduplicated Attachments)
- Metadata Store (Cassandra / Wide-Column): Partition by
(user_id, folder)and cluster byemail_id(TimeUUID/Snowflake). Loading a user'sInboxpage 1 reads one contiguous partition slice! - Precomputed Folders & Labels: Storing
email_idpointers inside materializedemails_by_labeltables avoids slowWHERE label = 'Starred'scans. - Attachment Deduplication: Hash attachments (
SHA-256) so forwarding a 15 MB slide deck to 500 coworkers stores only 1 physical object in S3 via reference counting!
3. Outgoing Queue, Bounce Loops & Full-Text Search
- Asynchronous Outgoing Queues: Enqueue outbound messages to Kafka (
outgoing_mailtopic). If a recipient's SMTP server returns4xxtemporary throttling, retry with exponential backoff and update the user'sSentvsDraft/Bouncedstatus asynchronously. - Distributed Full-Text Search (Elasticsearch / Inverted Index): Partition a per-user inverted search index asynchronously whenever mail arrives, is deleted, or moves folders—indexing
subject,sender, andbodytokens while keeping search scoped strictly touser_idshards.