AlgoViz

Design Gmail

hard 60 min

Design 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

Our systemTheir serverSMTP inSpam checkObject storageKafkaaccepted mailMail workerMailbox storeSearch index

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

Our systemTheir serverSMTP inSpam checkObject storageKafkaaccepted mailMail workerMailbox storeSearch index

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

    critical

    A crash loses mail that the sender has already deleted.

    Wrong
    queue_in_memory.put(raw)return 250
    Right
    ref = objects.put(raw)     # replicatedlog.append(ref)            # durablereturn 250
  • Dropping duplicates with a Bloom filter

    critical

    A false positive discards a real letter.

    Wrong
    if bloom.seen(message_id):    return
    Right
    # conditional write; loses nothingmailbox.insert_if_absent(fingerprint, row)
  • Adding reputation to a risk score

    major

    0.85 means a good sender. Added as it is, it makes good senders look like junk.

    Wrong
    score = 0.3 * content + 0.2 * reputation
    Right
    score = 0.3 * content + 0.2 * (1 - reputation)
  • Bouncing junk after accepting it

    major

    The sender address may be forged, so the bounce goes to somebody innocent.

    Wrong
    accept(); later: send_bounce(mail_from)
    Right
    # refuse inside the conversationreturn 550
  • Trusting the user named in the query

    critical

    The search filter decides whose mail is read.

    Wrong
    index.search(q["user_id"], q["text"])
    Right
    index_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.