Design Gmail
hard 60 minDesign a mail service like Gmail.
See it run
Follow a message through the doors, the spam score, its conversation, and back out of the search index.
Operations
Receive a message
Cheap checks first, durable storage second, the promise third, and everything slow after that.
Step 1 of 10
Connect
holder
sender
Their server opens a connection to one of our MX hosts. Why: DNS sent it here.
Step 1: Connect
def on_message(env, raw): if reputation(env.ip).bad: return 554 spf = check_spf(env.ip, env.mail_from) dkim, dmarc = check_signature(raw), check_policy(raw, spf) score = spam.score(env, raw, spf, dkim, dmarc) # time limit if score > 0.95: return 550 ref = objects.put(raw) # replicated log.append(env.rcpt, ref, score) # durable return 250 # now it is oursdef worker(entry): mailbox.file_once(entry) # idempotent index.later(entry); push.hint(entry.mailbox)- Latency
- under 2 s to 250
- Load
- 3.75M/s peak
- Offered, peak
- 7.5M/s
- Refused (assumed)
- half
Ask at each step: who is holding the message now?
In the real world
The same a mail service doing real jobs. Pick one and step through it.
We stored the message, but the sender never heard us.
Step 1 of 4
Stored
The raw message and its log entry are durable.
Step 1: Stored
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Saying 250 before the message is durable
criticalA crash loses mail that the sender has already deleted.
Wrongqueue_in_memory.put(raw)return 250Rightref = objects.put(raw) # replicatedlog.append(ref) # durablereturn 250Dropping duplicates with a Bloom filter
criticalA false positive discards a real letter.
Wrongif bloom.seen(message_id): returnRight# conditional write; loses nothingmailbox.insert_if_absent(fingerprint, row)Adding reputation to a risk score
major0.85 means a good sender. Added as it is, it makes good senders look like junk.
Wrongscore = 0.3 * content + 0.2 * reputationRightscore = 0.3 * content + 0.2 * (1 - reputation)Bouncing junk after accepting it
majorThe sender address may be forged, so the bounce goes to somebody innocent.
Wrongaccept(); later: send_bounce(mail_from)Right# refuse inside the conversationreturn 550Trusting the user named in the query
criticalThe search filter decides whose mail is read.
Wrongindex.search(q["user_id"], q["text"])Rightindex_of(session.user_id).search(q["text"])
Interview tips
Say who holds it
- Before 250 the sender, after 250 we do.
- Every crash point has a holder.
- 451 is honest; losing mail is not.
Lead with the partition
- 67 MB of metadata a mailbox.
- Every key starts with the mailbox.
- Search reads one small index.
Be careful with trust
- Authenticated is not wanted.
- Message-ID is written by a stranger.
- Attachments are parsed in a sandbox.
Let work be late
- Index and push after filing.
- Attachment text minutes later.
- Retries for 5 days, per domain.