BOOKS DEEP-READ · SYSTEM DESIGN INTERVIEW VOL 2 · CH 8

Design a Distributed Email Service

System Design Interview Vol 2 · Ch 8 · Alex Xu & Sahn Lam · 2022

中文 →

What is this chapter about?

The mailbox you open every morning has to run a billion private mailboxes at once: take everything in, let you find any of it again, and look identical when you pick up a different phone. This chapter builds one of those from scratch.

A strange thing first

The rules for moving mail around were written in the 1980s and have barely changed. Yet this is one of the heaviest chapters in the book — because the hard part was never "how do I get the letter there", it's "where do I put it once it lands, and how do I ever find it again". A mail service is not really a messaging system. It's an enormous warehouse.

Why the old world couldn't cope

Early mail servers were charmingly simple: one machine, and everybody's letters piled up in a folder on it. That falls over on every axis once the crowd arrives — a single day's incoming mail can fill a machine; when the machine dies, that whole group of people goes dark; and mail is essentially never deleted (lawyers and auditors want it kept), so the pile only grows.

There's a counter-intuitive part too. What actually strains the system isn't receiving mail — it's the little taps you don't think about. Marking something read, adding a label, dragging it to archive: every one of those is a write. Read forty letters a day and that's forty more writes.

The core ideas

1. One cubby per person. Mail has a luxury other systems envy: nobody ever looks inside someone else's mailbox. Every operation concerns exactly one person. So you cut the warehouse into one cubby per person, and every read and write touches only one cubby — which makes the whole category of "coordinate five machines to get one thing done" problems simply disappear.

2. Letters and parcels get stored apart. The text is light; the attachment is heavy. So attachments go to the big depot next door, and the cubby holds only a claim ticket. Flipping through your inbox then never has to drag the parcels along.

3. Everyone gets their own little catalogue. Searching mail and searching the web are different animals: web search is a billion people consulting one giant catalogue; mail search is a billion tiny catalogues, each serving one person. Yours is thin — which is exactly why a letter that landed a second ago is already findable.

4. The doorman only signs for it. When mail arrives you can't leave the courier standing outside: sign for it, drop it on the conveyor belt, and do the slow work — virus scan, spam filter, sorting — further down the line.

The honest cost

One letter actually gets written to three places: the cubby, the depot, and the catalogue. So deleting one means deleting three — and for a short while those three can disagree. You delete a message and search may still show a ghost of it for a moment.

Remember this

The hard part of a mail service is storing and finding, not sending. Its great asset is that nobody reads anyone else's mailbox: cut the warehouse into one cubby per person and the expensive work collapses into single-cubby work. Heavy attachments move to the depot next door; catalogues are per-person. The cost is that one letter lives in three places and they briefly disagree.

Want the mechanisms, the notation and the diagrams? → Switch to Deep mode