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.

Case study · Live model

How a mail service behaves

A mail service takes in messages for its users. Mail servers accept each message and put it in a queue. Workers check it for spam and write it to a mailbox, and an indexer then makes it searchable. Stop the mailbox store, change the spam limit, or send more mail than the indexer can take.

Try it

Lab time 3.0 s
inbox or spamsearchSenders, 14 a secondSenders14 a second30% is spamMail servers, accept each messageMail serversaccept each message45 acceptedQueue, 0 waitingQueue0 waitingemptyMail workers, 2 of 4 busyMail workers2 of 4 busyspam limit 0.5Mailbox store, 28 in inboxesMailbox store28 in inboxes15 in spam foldersSearch index, up to dateSearch indexup to date1 to addReaders, 4 searches a secondReaders4 searches a second0 found nothing
  • Senders — 14 a second — healthy — 30% is spam
  • Mail servers — accept each message — healthy — 45 accepted
  • Queue — 0 waiting — healthy — empty
  • Mail workers — 2 of 4 busy — healthy — spam limit 0.5
  • Mailbox store — 28 in inboxes — healthy — 15 in spam folders
  • Search index — up to date — healthy — 1 to add
  • Readers — 4 searches a second — healthy — 0 found nothing
  • A message arrives
  • A message moves to the next step
  • Into an inbox
  • Into a spam folder
  • A search
Time to the mailbox
103 ms
from accepted to stored, last 3 s
Waiting
0
nothing is waiting
Spam in the inbox
0
of 15 spam messages: 0% of it
Real mail in spam
0
of 30 real messages: 0% of it
Index is behind
under 1 s
1 stored messages not yet searchable
Searches with no result
0/ 9
for a message already in the inbox

Numbers add up:45 accepted = 0 in the queue + 2 being checked + 0 waiting for the store + 28 in inboxes + 15 in spam folders · 43 stored = 42 in the index + 1 to addThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Mail
14 a second
30%
0.5

A message with a spam score over the limit goes to the spam folder. Lower is stricter.

Workers and the store
4
1
Mailbox store

One worker checks a message in 100 ms. One indexer adds a message in 45 ms.

Messages waiting, and how far the search index is behind (seconds)
Messages waitingIndex behind (s)
00.10.10.20.21s2s3s
Show data table
TimeMessages waitingIndex behind (s)
1s00
1s00
2s00
2s00
3s00.1
3s00

What just happened

Nothing yet. Start traffic or run an experiment.

What this model simplifies
  • This is only the mail that comes in. Sending mail out to other services is the same pattern turned round: a queue, and tries again and again over hours when the other side does not answer.
  • A spam score here is one number with some chance in it. A real filter learns from millions of messages, from what users mark as spam, and from who the sender is.
  • Each message is one small item. Real mail has attachments, which are kept in an object store, once for all the people who got the same file.
  • The queue never fills up. A real mail server that cannot keep a message tells the sender to try again later, and the sender does.
  • A reader searches only for mail from the last 10 s. That is the case where a slow index shows; older mail is found once the index has reached it.
  • There is one mailbox store. A real service splits users over many stores, with copies of each.

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

  1. Sender
  2. Mail servers
  3. Outgoing queue
  4. Mail workers
  5. Mailbox store
  6. Search index
  1. Sender to Mail servers

    The sender's app submits the message. The server checks the sender and answers "accepted" quickly.

  2. Mail servers to Outgoing queue

    The message waits in a queue, so a slow recipient never blocks the sender.

  3. Outgoing queue to Mail workers

    A worker scans it for spam and viruses.

  4. 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.

  5. 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

  1. Recipient
  2. Mailbox store
  3. Search index
  1. 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.

  2. Recipient to Search index

    A search for "invoice" asks the index, which lists the matching message ids.

  3. 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.