Case study demo
Email service
Send, store and search mail at scale.
An interactive demo of how this system behaves. It runs in your browser.
Design walkthrough: Email service
An email service accepts a message, delivers it to each recipient's mailbox, and lets people search years of mail.
Requirements
What it must do
- Send a message to one or many recipients, with attachments.
- Show each user their folders, newest mail first.
- Search mail by word, sender or date.
How well it must do it
- Never lose an accepted message.
- Accept a message within about a second. Delivery may take a few more.
- Block spam and viruses before they reach the inbox.
Rough numbers
- 100 million users sending 20 emails a day is 2 billion emails a day, about 23,000 a second.
- At 50 KB on average, that is 100 TB of new mail a day.
How a request flows
Write pathSend an email
- Sender
- Mail servers
- Outgoing queue
- Mail workers
- Mailbox store
- Search index
Sender to Mail servers
The sender's app submits the message. The server checks the sender and answers "accepted" quickly.
Mail servers to Outgoing queue
The message waits in a queue, so a slow recipient never blocks the sender.
Outgoing queue to Mail workers
A worker scans it for spam and viruses.
Mail workers to Mailbox store
For a recipient on this service, the worker writes the message into their mailbox. For an outside address, it sends the message on over SMTP.
Mailbox store to Search index
The message's words are added to the recipient's search index a moment later.
Read pathOpen the inbox and search
- Recipient
- Mailbox store
- Search index
Recipient to Mailbox store
The app asks for the newest 50 messages in the inbox. It loads subject lines first, and a body only when it is opened.
Recipient to Search index
A search for "invoice" asks the index, which lists the matching message ids.
Search index to Mailbox store
The app loads those messages from the mailbox store.
Scaling it
- Partition mailboxes by user id. All of one user's mail sits on one group of servers, so listing a folder touches one partition.
- Store each attachment once in object storage, and let every recipient's copy point to it.
- Keep one search index per user. A search then scans one small index, not all mail.
What breaks, and what happens then
The recipient's mail server is down
- What users see
- The message cannot be delivered now.
- What the design does
- The worker retries with growing waits for up to a few days. After that it sends the sender a bounce message.
A worker crashes in the middle of a delivery
- What users see
- The message is still in the queue.
- What the design does
- Another worker picks it up. A message id stops the same mail landing twice in a mailbox.
The search index is behind
- What users see
- A new message is in the inbox but not in search yet.
- What the design does
- Accept the short delay. The inbox list reads the mailbox store directly, so new mail always shows.
Trade-offs
- One copy of a message per recipient, or one shared copy
- A good default
- Share large attachments, and keep a small record per recipient.
- What it costs
- Deleting needs a count of who still points at the attachment.
- Index mail when it arrives, or when the user first searches
- A good default
- When it arrives, so search is always fast.
- What it costs
- Every message costs index work, even if nobody ever searches for it.
Common mistakes
- Delivering inside the send request. One slow outside server would then make sending slow for everyone.
- One search index for all users. Most searches only ever need one user's mail.
Questions an interviewer might ask
How does the service stop spam?
Check that the sending server is allowed to send for that domain. The SPF and DKIM records of the domain say so. Also score the content, and limit senders who mail many strangers.
How do you sync mail across a phone and a laptop?
Each mailbox keeps a change counter. A device sends the last counter it saw and gets only what changed since.
How would you support "undo send"?
Hold the message in the queue for about 10 seconds before a worker takes it. Undo removes it from the queue.