Design Gmail
hard 60 minDesign a mail service like Gmail.
Video lesson · Chapter 1 of 5
The receiving server takes responsibility
Theory
Read or listen first. About 2 min.
What it is
A mail service gives each person a mailbox. It speaks SMTP with the rest of the internet to send and receive, and it gives its own people an app to read, label and search.
It is two systems that share one store. One side talks to millions of servers we do not control. The other is a fast private database with one partition of data per person.
Why it exists
Mail is the one messaging network nobody owns. Any server may send to any other, which is why it is everywhere, and also why most design effort goes into deciding whom to believe.
It also makes a strict promise. When our server answers 250, the sender may delete its copy. From that moment, losing the message is our fault, so durability comes before every feature.
A short history
August 1982
RFC 821 describes SMTP, the protocol servers still use to hand mail to each other.
1 April 2004
Google announces a Gmail preview with 1,000 MB of storage, built around search instead of folders.
October 2008
RFC 5321 and RFC 5322 replace the earlier standards for SMTP and for the message format.
June 2012
RFC 6647 describes greylisting: say 'try later' to unknown clients, because much junk never comes back.
May 2026
RFC 9989 replaces RFC 7489 as the specification of DMARC.
Words to know
- SMTP
- The protocol one mail server uses to give a message to another.
- MX record
- The DNS entry that says which servers accept mail for a domain.
- Envelope
- The sender and recipients given in the SMTP conversation. They can differ from the From and To that a reader sees.
- SPF, DKIM, DMARC
- Is this server allowed to send for the domain? Is the signature valid? Does one of them match the visible From?
- Label
- A tag on a message. A message may carry many, unlike a folder.
- Thread
- A conversation: messages linked by their reply headers.
- Bounce
- A message back to the sender saying delivery failed.
- Change number
- A counter that goes up with each change to a mailbox, so a device can ask: what is new since 812?
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
Understand it
The order matters: write it down, say thank you, and only then do the sorting.
Scope
What are we building?
Send and receive mail with the whole internet, keep junk out, show conversations, label, and search. Comparable products include Outlook, Yahoo Mail, Proton Mail, Apple Mail, Zoho Mail, Fastmail, HEY and Tuta. Calendar and chat are separate exercises.
Accept
Never lose a message after saying yes.
Judge
Junk out, real letters in. A hidden real letter is the worse mistake.
Find
Years of mail, one person, under 500 ms.
The finished system
Where the design ends up
This is the last version. The Design tab starts with one service and one database and adds a part only when something breaks.
The whole system
Summary
The whole topic in one minute.
A mail service must first accept safely: store the raw message on several machines, then answer 250. Everything else, from threads to search, happens afterwards and can be retried.
All of one person's data sits together, partitioned by mailbox. Spam is judged in layers, outgoing mail waits in one queue per destination, search uses one small index per mailbox, and devices catch up with a change number.
- Saying 250 moves responsibility to us. Store first, answer second, process third.
- Retries make duplicates, so filing a message must be idempotent.
- One person's mail lives in one partition: metadata in a wide-column store, bodies in object storage.
- Authentication says who sent it. It does not say the mail is wanted.
- Outgoing mail waits per destination, so one broken domain cannot block the others.
- Search is one small index per mailbox, updated shortly after delivery.