BOOKS DEEP-READ · SYSTEM DESIGN INTERVIEW VOL 1 · CH 10

Design a Notification System

System Design Interview Vol 1 · Ch 10 · Alex Xu · 2020

中文 →

What is this chapter about?

The line that pops onto your lock screen — "your parcel is out for delivery", "someone replied to you" — is the visible end of a whole notification system. Chapter 10 asks you to build one. It sounds like a small job. It is in fact the book's cleanest lesson in a hard truth: you are accountable for something whose most important step is not yours to control.

An analogy: three couriers who don't take orders from you

Think of the system as a mail depot. You write the letters, but the last mile must be handed to three outside couriers: one for iPhones (Apple's), one for Android phones (Google's), one for text messages (the carriers). The catch is that each of them follows only its own house rules.

Apple's courier says: if nobody is home, I keep only the most recent letter and throw the rest away. Google's says: I will hold at most 100 letters per door, and if a 101st arrives I discard the lot; anything undelivered for four weeks I discard too. Neither gives you a signed receipt — only "I have taken the letter".

Why the obvious approach falls over

The obvious approach: as soon as the order goes through, your code knocks on the courier's door and waits for an answer before moving on. Three problems, each worse than the last. The courier takes an extra second and your checkout endpoint takes an extra second too. The one machine doing the errands dies and every pending notification evaporates. Sale day arrives, volume goes up fiftyfold, and there is nowhere to add hands.

The core move: put a conveyor belt in the middle

The fix is a conveyor belt. The business side just drops "what to send, to whom" onto the belt and walks away — a matter of milliseconds. At the far end stands a row of workers who pick items off and do the running around. However slow the courier is, that slowness never reaches your main flow.

The crucial detail is that there is one belt per courier: one for Apple, one for Google, one for SMS, one for email. If the SMS courier jams today, only that one belt backs up while the other three keep moving; if SMS falls behind, you add workers to that belt alone. One blocked lane must not block the other three — that is the most valuable sentence in the chapter.

And three small things you must handle yourself

A belt alone is not enough. The switch: before sending, check whether this person wants this kind of notification at all. The serial number: stamp every letter with a unique number and have the worker check "have I sent this one already?" before running — because failed errands get retried, and retries always produce duplicates. The cap: at most N letters per person per day. The third is the one people skip, and its cost is the worst of the three: annoy someone into switching notifications off and you can no longer reach them at all — not even with "payment successful" — and that door usually stays shut.

The honest cost: once there is a belt, notifications are no longer instant — usually a few hundred milliseconds to a few seconds, and minutes when a lane backs up. And you can only promise "delivered at least once"; keeping duplicates out is on you, via the serial number.

One line to remember

A notification system is hard not because sending is hard, but because the last mile belongs to someone else: third-party channels guarantee neither delivery nor exactly-one delivery. All you truly control is your own side — a belt that separates business logic from delivery, a separate lane per channel, a serial number to absorb duplicates, and not annoying users into switching you off.

Want the architecture, the delivery semantics and the diagrams? → Switch to the deep read