Communication
Design Gmail
01
Requirements
Requirements
- Receive email from any SMTP sender on the internet
- Send email to any recipient domain (outbound SMTP)
- Store per-user mailbox: read/unread, starred, labeled, archived
- Conversation threading (stitch replies via In-Reply-To / References headers)
- Full-text search across message bodies + attachments
- Labels (Gmail's innovation — multi-label instead of folders), filters, priorities
- Attachments up to ~25 MB inline; Drive links for larger
- Spam + phishing filtering; DKIM/SPF/DMARC verification
- Mail delivery from sender's send to recipient's inbox: < 60 seconds typical
- 99.99% availability for receive; 99.9% for send (retries are built into SMTP)
- No lost mail — durability is sacred; users lose trust fast
- Inbox load < 200 ms p99; search < 500 ms p99
- Strong consistency on user actions (mark-read, archive, delete)
- Scale to ~1.8B users, ~15 GB avg quota, total ~~25 EB storage pre-dedup
02
Scale Estimation
Scale Estimation
03
API Design
API Design
Three surfaces: the SMTP edge (internet-facing protocol, not HTTP), the web/mobile API (HTTP+JSON for client apps), and IMAP/POP (legacy clients). Real engineering attention goes into SMTP edge and the web API.
Standard SMTP conversation: HELO → MAIL FROM → RCPT TO → DATA. Sender verification (SPF lookup on sender domain), DKIM signature check, DMARC policy enforcement all happen here. Most spam rejected at SMTP-time before even accepting bytes.
List messages matching a query. Gmail's query syntax is the primary API. Returns message stubs with metadata + snippets. Backed by per-user Elasticsearch-equivalent.
Fetch full message including body, headers, and attachment refs. Sent separately from listing so list operations are fast + body is lazy-loaded.
Submit outgoing message (web UI). Server does DKIM-sign, store in "Sent", enqueue for outbound SMTP delivery. Returns message_id immediately; delivery happens async.
Add/remove labels (marking read = remove UNREAD label). Strong-consistency in user's per-user datastore; fast path for UI responsiveness.
Fetch full conversation (all replies in a thread). Thread ID is computed server-side from Message-ID chain.
User reports spam. Not just moves the message — feeds back into the spam classifier (user signal). This is how the filter improves over time.
04
Architecture
Architecture
The system has a distinctive shape: an SMTP edge fleet facing the hostile internet, a spam + policy layer that decides accept/reject/quarantine, a per-user mailbox store (sharded by user_id) holding messages + labels + thread data, and a search index per user. An outbound SMTP fleet handles send-mail with delivery reputation management.
05
Deep Dive — Inbound Flow + Spam / Phishing Defense
Deep Dive — Inbound Flow + Spam / Phishing Defense
When a mail lands at smtp.gmail.com:25, these steps run before we even store the message:
- Connection-level check. Sender IP reputation lookup. Known bad-actor networks get 4xx temp-fail (forces retry from clean IPs) or hard reject.
- SMTP handshake. Standard SMTP. During
MAIL FROM, sender's domain SPF record is checked — "is this IP authorized to send for this domain?" Failures flagged or rejected based on DMARC policy. - DATA phase. Accept message bytes. On body completion, verify DKIM signature (cryptographic proof the domain owner signed this message). Unsigned or invalid signatures heavily penalized by spam classifier.
- Spam classifier. ML model reads headers + body features (links, suspicious TLDs, phrase patterns, sender reputation, recipient engagement history with this sender, etc.). Output:
{spam_prob, phishing_prob, label}. - Attachment + URL scanning. Attachments run through VirusTotal-equivalent in-house scanner. URLs checked against Google Safe Browsing. Phishing URLs trigger rewrite to warning interstitial.
- Routing decision. Accept (→ inbox), accept-to-spam (→ spam folder), reject (4xx temp or 5xx hard). Non-accepts return over SMTP so sending server knows.
- Storage write. Accepted messages written to per-user mailbox (sharded by recipient user_id). Thread computed from headers (In-Reply-To + References + subject-similarity heuristics). Search index updated.
- Notify. Push notification to device if user has app installed; IMAP IDLE for long-polling mail clients.
sequenceDiagram
participant S as Sender MTA
participant E as SMTP edge
participant F as Spam / virus
participant M as Mailbox svc
participant IX as Search index
participant N as Notify
S->>E: CONNECT + HELO
E->>E: IP rep lookup
S->>E: MAIL FROM + RCPT TO
E->>E: SPF + DMARC check
S->>E: DATA (headers+body)
E->>E: DKIM verify
E->>F: score message
alt accept to inbox
F-->>E: ham
E->>M: store for user
M->>IX: index body
M->>N: fanout event
E-->>S: 250 OK
else accept to spam
F-->>E: spam
E->>M: store to SPAM label
E-->>S: 250 OK (quiet)
else reject
F-->>E: malware / policy
E-->>S: 550 Rejected
end
Per-user mailbox shard. Bigtable row key is something like user_id#inv_ts#msg_id — so all messages for a user are co-located and scanning the inbox by time is a cheap range read. Labels stored as a separate indexed table (user_id#label#inv_ts#msg_id → msg_id) so "all messages with label=Work" is also a range read.
Search. Each user has their own inverted index. A query "from:boss receipt" intersects the "from:boss" posting list with the "receipt" posting list, restricted to that user's corpus. Indexes live in a separate service; built asynchronously after message ingest (≤ few seconds lag). Google's custom Zanzibar-style ACL applies — never search across users.
Outbound reputation. To send email that actually lands in inboxes (not spam folders at the recipient), Gmail must maintain pristine IP reputation. Outbound fleet sends from dedicated IPs with SPF + DKIM signing. Rate limits per sending-user. Bulk senders (marketing accounts) are flagged and throttled. Gmail's deliverability advantage is maintained by aggressively shutting down senders who get reported as spam.
"SMTP edge does connection-level IP reputation, SPF/DKIM/DMARC auth, then accepts DATA. Before writing, message scores through ML spam + phishing classifier with URL + attachment scanning. Accept decision routes to inbox, spam folder, or reject. Per-user mailbox on Bigtable keyed by (user_id, ts). Threading via In-Reply-To headers. Per-user search index built asynchronously. Outbound fleet DKIM-signs and maintains IP reputation via throttling + abuse response. User spam reports feed back into classifier training."
06
Tradeoffs & Design Choices
Tradeoffs & Design Choices
- Labels vs folders. Gmail's big bet. Folders are hierarchical single-placement; labels are multi-valued. Enables "archive but I'll find it via search" — a UX that requires reliable fast search. Folders are storage-cheap; labels require reverse index.
- Per-user sharding vs domain sharding. Per-user is simpler for consistency + per-user quotas + privacy boundaries. Domain sharding is natural for corporate Workspace. Gmail does per-user + domain-aware routing.
- Threading algorithm quality. In-Reply-To/References is RFC-compliant. But email clients strip these fields; same-subject heuristics needed as fallback. Each added heuristic has false-positive risk. Interview-worthy because it's never fully solved.
- Attachments inline vs separate. Separate: dedup across messages (same PDF attached to 100 forwards = 1 copy). Inline costs more storage. Gmail stores separate + dedup + garbage-collects when all refs gone.
- Accept-and-filter vs reject-at-SMTP. Accept + filter to spam folder = better user experience (can recover). Reject = defends sender reputation. Gmail does both: soft-spam to folder for maybe-spam, hard-reject for obvious malware / IP-reputation-bad.
07
Failure Modes
Failure Modes
08
Evolution
Evolution
MVP — per-user folders, IMAP, simple MTA
Folder-based mailbox on local disk per user. Sendmail / Postfix for SMTP. No unified search. Works at Yahoo Mail early-2000s scale.
Search-first + labels (Gmail 2004)
Per-user inverted index. Multi-label messages instead of single-folder. "Just archive, you'll search for it" paradigm shift.
ML spam + DKIM/DMARC
Bayesian → deep-learning spam classifier. DKIM cryptographic sender auth. DMARC policy enforcement. Spam rate drops from 50% of inbox to < 1%.
Conversation view + smart categories
Thread detection + Primary/Social/Promotions tabs. Smart Reply + Smart Compose (ML-generated suggestions) added later.
LLM-integrated (Help me write, summarize)
Gemini-style models draft replies, summarize threads, extract action items. Requires per-user, per-thread context — surfacing relevant messages back into the model prompt is the new "search".
Watch and read
References & Videos
Try next
Free to read · better with Enzo
Whiteboard this with Enzo
Enzo runs it as a live system design round on the whiteboard and grades your trade-offs.