System DesignSep 19, 2026⏱️ 2 min read
Design a Distributed Email Service: SMTP Relays, Inbox Sharding & Spam Filtering

Design a Distributed Email Service: SMTP Relays, Inbox Sharding & Spam Filtering

Why Modern Webmail Scales Beyond Legacy IMAP/POP3

Designing a webmail provider serving 1 billion users and ~100 billion emails/day requires bridging 1980s SMTP federation protocols with high-scale distributed storage, virus/spam pipelines, and sub-second inbox search across multi-gigabyte mailboxes.
Distributed Email Service Architecture

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

Emails average ~50 KB metadata + optional 10 MB+ attachments (100 PB+ storage/year). Never store giant PDF/ZIP blobs inside database columns!
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 by email_id (TimeUUID/Snowflake). Loading a user's Inbox page 1 reads one contiguous partition slice!
  • Precomputed Folders & Labels: Storing email_id pointers inside materialized emails_by_label tables avoids slow WHERE 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!

  1. Asynchronous Outgoing Queues: Enqueue outbound messages to Kafka (outgoing_mail topic). If a recipient's SMTP server returns 4xx temporary throttling, retry with exponential backoff and update the user's Sent vs Draft/Bounced status asynchronously.
  2. 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, and body tokens while keeping search scoped strictly to user_id shards.

Share this article

Comments