BOOK DEEP READ · System Design Interview Vol 2 · Ch 4

Distributed Message Queue

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

中文 →

What is this chapter about?

You tap "Place order" in a food delivery app. In that one second, the restaurant has to get the ticket, dispatch has to start looking for a courier, inventory has to come down, the coupon has to be marked used, fraud detection wants a look, and the analytics dashboard wants a tally. This chapter is about the pipe in the middle — and it teaches you to build one yourself.

A strange thing first

This thing is famous for being fast, and yet it writes every single message onto a disk rather than keeping it in memory. Stranger still, it outperforms plenty of designs that never touch a disk at all.

Why the old way breaks

Could you skip the pipe? The order service could just call everyone in turn: call the restaurant, call dispatch, call inventory, call fraud. Three problems, stacked.

First, the order service has to know every department, so each new department means editing the order code again. Second, fraud detection can handle five thousand things a second, and at the start of a big sale fifty thousand orders arrive each second — calling it does no good and drags the order service down too. Third, if the fraud machine restarts, those few minutes of orders are never checked by anyone, because a shout, once shouted, is gone.

The core idea: stop shouting, start bookkeeping

What the pipe really is, is not a megaphone but a ledger. The order service calls nobody; it just adds a line to the last page of the book. Anyone who wants to know what happened comes and reads the book, and keeps track themselves of which line they got to.

All three problems dissolve. The order service knows the book, not the departments. Fraud detection can read slowly; the book will not hurry it. And when fraud detection restarts, it picks up from the line it noted down — the record is still there, so nothing is ever "missed and gone". A new department is easier still: it starts at line one and disturbs no one.

Why writing to disk is fast here

Because it only ever writes to the last page. A disk is slow at flipping back and forth hunting for a page; it is not slow at adding to the end. A ledger that only grows and never gets edited happens to avoid the one thing disks are worst at — and that is the entire secret behind daring to use a disk at all.

When one book isn't enough, open several

One ledger has one person writing on its last page, so however fast they are, there is a ceiling. The answer is several books, split by order number: numbers ending 0 to 3 go in the first book, 4 to 6 in the second, and several people write at once. The cost lands right there: within one book the order of events is perfectly reliable, but across books, who came first is anyone's guess.

One line to remember

A message queue is not a megaphone. It is a ledger that only grows at the end, where each reader tracks their own place. It dares to write to disk because it only writes at the end, and it scales because one book becomes several — and the moment you split it, all you still hold is the half-promise that each book is internally ordered.

Want the disk layout, the replication protocol and the delivery semantics? → Switch to the deep read