Skip to main content
BLUEPRINT #04Media & Streaming

Design a Distributed Email Service

Target AWS Architecture:DynamoDBS3SQSSNS
10-Stage Structure:1. Requirements→2. Sizing→3. Topology→4. Data Model→5. AWS Topology→6. Deep-Dive→7. Failures→8. SRE Playbooks

1. Problem Statement & Scope

System Mission

Design a massively scalable, secure, and highly available distributed email infrastructure capable of handling tens of billions of daily inbound and outbound messages, supporting standard protocols (SMTP, IMAP, POP3, JMAP), real-time spam and malware filtering, cryptographic sender authentication (SPF, DKIM, DMARC), decoupled blob storage for attachments, and sub-second full-text mailbox search.

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

Functional Requirements

  1. Inbound Email Ingestion (MX): Accept incoming RFC 5322 MIME messages over SMTP (Port 25/587 with STARTTLS).
  2. Outbound Email Dispatch: Queue, sign with DKIM private keys, and reliably deliver outbound emails to external recipient MX hosts.
  3. Mailbox Hierarchy & Threading: Support user folders (Inbox, Sent, Trash, Drafts, Custom Labels), read/unread status, and conversation threading via In-Reply-To and References headers.
  4. Decoupled Storage & Attachments: Separate lightweight mailbox metadata from large raw MIME bodies and binary attachments (up to 25Β MB25\text{ MB}).
  5. Full-Text Search: Sub-second search across email subjects, sender addresses, recipient lists, and message body contents.
  6. Security & Spam Filtering: Verify SPF, DKIM, and DMARC alignment; evaluate incoming payloads against real-time IP reputation lists and Bayesian spam classifiers.

Non-Functional Requirements (SLAs/SLOs)

  • High Availability: 99.999%99.999\% uptime for mail access and delivery (<5.26Β minutes< 5.26\text{ minutes} downtime/year).
  • Data Durability: Zero data loss (100%100\% durability for committed emails; 11 9s durability via ).
  • Delivery Latency: Outbound mail delivered to recipient MX in <5Β seconds< 5\text{ seconds} (P95P95); Inbound mail visible in user inbox in <2Β seconds< 2\text{ seconds}.
  • Mailbox Read Latency: Rendering first 50 folder messages in <50Β ms< 50\text{ ms} (P99P99).
  • Search Query Latency: Full-text query across 100,000 user emails in <300Β ms< 300\text{ ms}.

Out-of-Scope

  • Real-time collaborative calendar and scheduling suites (CalDAV/iCalendar protocols).
  • Web-based video conferencing integrations.

2. Capacity & Scale Estimation

Traffic & Throughput Calculations

  • Active Mailboxes: 100Β Million100\text{ Million} accounts.
  • Daily Inbound & Outbound Emails: 1Β Billion1\text{ Billion} emails/day. AverageΒ IngestionΒ QPS=109Β emails86,400Β sβ‰ˆ11,574Β emails/sec\text{Average Ingestion QPS} = \frac{10^9 \text{ emails}}{86,400 \text{ s}} \approx 11,574 \text{ emails/sec} PeakΒ IngestionΒ QPSΒ (3Γ—Β morningΒ surge)=35,000Β emails/sec\text{Peak Ingestion QPS (3}\times\text{ morning surge)} = \mathbf{35,000\text{ emails/sec}}

Storage Sizing & Bandwidth Math

  • Average Email Size: 100Β KB100\text{ KB} (Metadata: 1Β KB1\text{ KB}, Text Body: 20Β KB20\text{ KB}, Attachments: 79Β KB79\text{ KB} average). DailyΒ RawΒ EmailΒ Ingestion=109Γ—100Β KB=100Β TB/dayβ€…β€ŠβŸΉβ€…β€Š36.5Β PB/year\text{Daily Raw Email Ingestion} = 10^9 \times 100\text{ KB} = 100\text{ TB/day} \implies \mathbf{36.5\text{ PB/year}}
  • Storage Tier Allocation:
    • Metadata (Hot): 109Γ—1Β KB=1Β TB/day=365Β TB/year10^9 \times 1\text{ KB} = 1\text{ TB/day} = \mathbf{365\text{ TB/year}}.
    • Raw MIME & Attachments: 109Γ—99Β KB=99Β TB/day=36.135Β PB/year10^9 \times 99\text{ KB} = 99\text{ TB/day} = \mathbf{36.135\text{ PB/year}}.
    • Search : Indexing subject + body text (β‰ˆ10Β KB/email\approx 10\text{ KB/email}) with 1 primary + 1 replica β€…β€ŠβŸΉβ€…β€Š20Β TB/day\implies 20\text{ TB/day}.
  • Network Egress / Ingress Bandwidth: PeakΒ NetworkΒ Throughput=35,000Β msgs/sΓ—100Β KB=3.5Β GB/s=28Β Gbps\text{Peak Network Throughput} = 35,000 \text{ msgs/s} \times 100\text{ KB} = 3.5\text{ GB/s} = \mathbf{28\text{ Gbps}}

3. High-Level Architecture & AWS Component Mapping

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

Component Responsibility Breakdown

ComponentAWS TechnologyOperational Role & Configuration
Inbound SMTP GatewayAmazon SES (Inbound)Receives incoming SMTP traffic on port 25 with STARTTLS, performs initial SPF/DKIM verification, streams raw RFC 5322 MIME payloads directly to .
Raw Payload StoreContent-addressable storage layout: s3://mail-raw-blobs/<user_id>/<year>/<month>/<message_id>.eml. Encrypted via AWS KMS. Lifecycle moves mail >90Β days> 90\text{ days} to Infrequent Access.
MIME Parser FleetAmazon ECS FargateDecouples MIME body parts, extracts plain-text/HTML, unpacks attachments, computes thread hashes (References/In-Reply-To), and populates + .
Mailbox DatabaseHigh-performance storing folder views, thread indexes, read/unread statuses, and attachment manifests with single-digit millisecond latency.
Search EngineAmazon Managed distributed BM25 search cluster indexing email subjects, sender/recipient addresses, and tokenized message bodies.
Outbound DispatcherAmazon SES Dedicated IPsEnforces outbound rate limiting, signs messages with 2048-bit DKIM RSA keys, handles automatic IP warming pools and bounce/complaint processing.

4. API Interface Design & Wire Protocols

1. Send Outbound Email (REST API)

http
POST /v1/mail/messages/send HTTP/1.1
Host: mail.production.aws.internal
Authorization: Bearer <jwt_token>
Content-Type: application/json

{
  "to": ["architect@distributed-systems.org"],
  "cc": ["lead-sre@company.com"],
  "subject": "System Design Architecture Review: LSM vs B-Trees",
  "body_html": "<p>Team, attached is the revised proposal for the write-optimized storage engine.</p>",
  "body_plain": "Team, attached is the revised proposal for the write-optimized storage engine.",
  "in_reply_to": "<msg_88129a@distributed-systems.org>",
  "attachments": [
    {
      "file_name": "lsm_benchmark_results.pdf",
      "content_type": "application/pdf",
      "s3_temp_upload_key": "temp-uploads/user_101/tmp_pdf_998124"
    }
  ]
}

Response: 202 Accepted

json
{
  "message_id": "<msg_20260904_998124756@mail.aws.internal>",
  "status": "QUEUED_FOR_DELIVERY",
  "queued_at": 1718000050123
}

2. List Folder Messages (Cursor Pagination)

http
GET /v1/mail/folders/INBOX/messages?limit=25&cursor=eyJkYXRlIjoxNzE4MDAwMDAwLCJtc2dfaWQiOiJtc2dfMTAxIn0= HTTP/1.1
Host: mail.production.aws.internal
Authorization: Bearer <jwt_token>

Response: 200 OK

json
{
  "folder": "INBOX",
  "unread_count": 4,
  "total_count": 1420,
  "messages": [
    {
      "message_id": "msg_20260904_998124756",
      "thread_id": "thread_abc123xyz",
      "from": {"name": "Alice Wang", "email": "alice@cloud.org"},
      "subject": "System Design Architecture Review",
      "snippet": "Team, attached is the revised proposal for the write-optimized...",
      "has_attachments": true,
      "is_read": false,
      "received_at": 1718000050000
    }
  ],
  "next_cursor": "eyJkYXRlIjoxNzE3OTk5MDAwLCJtc2dfaWQiOiJtc2dfMDk5In0="
}

5. Data Models & Storage Architecture

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

DynamoDB Single-Table Schema (MailboxCoreTable)

() ()Attributes & PayloadsGSI1- / GSI1-
USER#<user_id>FOLDER#INBOX#DATE#<timestamp>#MSG#<msg_id>thread_id, from, subject_preview, is_read: false, s3_key, has_att: trueTHREAD#<thread_id> / DATE#<timestamp>
USER#<user_id>FOLDER#SENT#DATE#<timestamp>#MSG#<msg_id>thread_id, to: [...], subject_preview, s3_keyTHREAD#<thread_id> / DATE#<timestamp>
USER#<user_id>FOLDER_STATS#INBOXunread_count: 14, total_count: 1250, updated_at: 1718000050β€”
USER#<user_id>MSG_DETAIL#<msg_id>s3_mime_key, body_preview, headers_json, attachments_listβ€”

Part 2: Production Deep-Dive Locked1 Coin = 24 Hours

Unlock Complete Architecture & Production Runbooks

Your Balance:40 Coins

You have explored the free architectural preview (~45%). Spend 1 Coin to unlock the remaining 6 production deep-dive sections for a full 24 hours.

Sections Included in This 24-Hour Pass:
6. Core Algorithms & Deep-Dive Workflows
7. Architectural Trade-Off Matrix & Primitive Links
8. Critical Failure Modes, Resiliency & Edge Cases
9. Production Pitfalls & Anti-Patterns (The "Top 5 Gotchas")
10. Production Runbook & Observability Guide
11. System Design Interview Rubric & Deep-Dive Strategy
Keeps page unlocked for exactly 24 hoursSpend coins to fund LLM & compute infrastructure