Design an email system (Gmail)
How a message travels between providers over SMTP, how spam and spoofing are filtered, and how a mailbox is stored and searched at huge scale.
On the roadmap:Interview Ready · step 5: Real-world designs
Lesson 2 of 18 · 11 min read · quiz · exercise
The problem
An email system receives and sends messages for billions of mailboxes, stores them for years, keeps spam out, and lets users search and organise (labels, threads).
Requirements
- Reliable delivery: a message is never silently lost.
- Interoperability with every other mail provider.
- Spam, phishing and virus filtering.
- Large attachments, fast search, and strong privacy.
How email moves between providers
Email uses SMTP to send between servers. The key ideas:
- MX records in DNS tell you which servers accept mail for a domain.
- Delivery is store-and-forward: a server accepts a message, persists it, and passes it on, retrying if the next hop is down.
- Clients usually read mail through IMAP (or POP3) or through a provider's web and mobile API.
Figure SDA-RWS-02-1 · Servers talk SMTP to each other; readers fetch with IMAP or POP · © forcsguy.com
Image: Tim Sheerman-Chase · CC BY 2.0 · Source · Wikimedia Commons: Old Post Sorting Office, Portsmouth
Architecture
Figure SDA-RWS-02-2 · Email delivery architecture · © forcsguy.com
Sending
- The client sends the message to its provider's submission server (with authentication).
- The message is saved and put on an outbound queue, so slow recipients never slow the sender.
- A worker finds the recipient domain's MX hosts, connects with SMTP, and delivers. The worker signs the message (DKIM).
- Temporary failures are retried with backoff for days; permanent failures generate a bounce message.
Receiving
- The incoming server accepts the connection and verifies the sender with SPF, DKIM and DMARC.
- Filters score the message for spam, phishing and malware (rules, reputation, and machine learning).
- The message is written to the user's mailbox: metadata in a scalable database, bodies and attachments in object storage, and an entry in the search index.
- Clients are told (push, IMAP IDLE) that new mail arrived.
Figure SDA-RWS-02-3 · Where a received message is stored and how readers are told · © forcsguy.com
Fighting spam and spoofing
| Mechanism | What it does |
|---|---|
| SPF | The domain lists which servers may send for it |
| DKIM | The sender signs the message; receivers check the signature |
| DMARC | The domain says what to do when SPF or DKIM fail, and gets reports |
| Reputation | Track sending behaviour per IP and domain; rate-limit new senders |
| Content filters | Spam classifiers, link and attachment scanning |
Storage design
- A mailbox is append-heavy and read by recent items. Partition by user id, so one user's mail sits together.
- Keep metadata (labels, flags, thread id, snippet) separate from large bodies and attachments.
- Threads group messages by headers (References, In-Reply-To) and subject.
- Search uses an inverted index per user, updated as mail arrives.
- Deduplicate identical large attachments with content hashes.
- Keep multiple replicas and backups: email is people's records.
Scale and reliability
- Queues absorb bursts, for example newsletter sends.
- Rate-limit outgoing mail per account to stop compromised accounts spamming.
- Spread incoming servers across regions; MX records can list several with priorities.
- Privacy: encrypt in transit (TLS between servers) and at rest, and control internal access carefully.
Real-world reading
Related reading
- Marc Brooker's BlogMarc Brooker · brooker.co.zaBlogs
- NATS ServerNATS · CNCF / NATSGitHub
- Confluent BlogConfluent engineers · ConfluentBlogs
- Elastic BlogElastic engineers · ElasticBlogs
Show 6 moreShow fewer
- Moving Core Business Search to Elasticsearch at YelpYelp engineers · YelpCase Studies
- Domain-Driven Design ReferenceEric Evans · Domain LanguageBooks
- Moderating Promotional Spam and Inappropriate Content in Photos at Scale at YelpScale at Yelp engineers · Scale at YelpCase Studies
- Google Research BlogGoogle researchers · GoogleBlogs
- Khan Academy Engineering BlogKhan Academy engineers · Khan AcademyBlogs
- Salesforce BlogSalesforce engineers · SalesforceBlogs
Check your understanding
Question 1 of 3
0 correct so far
How does a sending mail server find where to deliver a message for [email protected]?
Go deeper
- How email works: RFC 5321 (SMTP) overview by IETF: search for the standard; the first sections are readable
Practise it: A newsletter goes out
Everything below is already set up for this lesson: change the design, run the traffic, and see whether you meet the goal.
- 1The task
- 2Build and test
A bulk send creates a spike of writes to the mail store, up to 1,600 requests per second.
Goal: Under 1% errors and the queue clears within 30 s.
Callers do not need the write applied this very second. What can hold it?
Try it yourself
A user sends an email with a 10 MB attachment to a person at another provider. List the steps from the Send button to the message appearing in the recipient's inbox.
Click what a strong design here would need:
More in Real systems
© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.