Book Deep Reads · System Design Interview Vol 2 · Chapter 13

Design a Stock Exchange

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

中文 →

What is this chapter about?

You tap "buy" on your phone and a few milliseconds later it says "filled". The machine that paired you with a seller is a stock exchange, and this chapter teaches you how to build one. It is the strangest chapter in the book: the previous twelve teach you to spread work across more machines. This one goes the other way — it pulls the most critical work back into a single thread on a single machine.

An analogy first

Picture a farmers' market with two lists on the wall: everything people are willing to pay, and everything people are willing to accept, each sorted best-first. Someone shouts "I'll pay 10". The clerk checks the sell list for anything at 10 or cheaper: if there is, they pair it on the spot; if not, the slip goes up on the wall in price order to wait. Among equal prices, whoever arrived first goes first. That wall is the "order book", and that rule is "price first, then time".

The hard part isn't the crowd

You would guess an exchange is hard because of volume. It isn't. The book budgets a billion orders a day; spread over a six-and-a-half-hour session that's about forty-three thousand a second — lighter than a big company's own monitoring system.

The hard parts are three others: be absurdly fast (a hair slower and someone else takes the price you wanted); never let anyone cut the line (first come, first served is a legal obligation here, not a nicety); and never get a single one wrong, while being able to rewind the tape frame by frame, because a regulator can always ask what exactly happened in that one second.

Its answer: let only one person work

If ten clerks edited that wall at once, they'd have to take turns — and who actually got there first would be genuinely hard to say. Once the order is fuzzy, line-cutting has already happened and nobody can reconstruct it afterwards. So the exchange does something plain: one clerk, and only one.

But it puts a ticket machine in front of that clerk. However many lanes orders arrive on, each takes a number at the door — 1, 2, 3 — written into a ledger, and they go in strictly by number. Two good things follow at once. The order is nailed down by the numbers, so cutting in is impossible. And replaying that ledger reproduces the market exactly as it was at any instant — on a different machine you get an identical result, which is why a standby can take over at any moment.

As for "won't one clerk be slow" — no. The work is all in memory: glance at two lists, cross out a few lines. The British exchange LMAX has publicly said its single thread handles six million orders a second. A machine's time goes into the network and the operating system, not into the matching. The cost is real, though: one stock's wall lives on one machine, so you grow only by giving different stocks to different machines — you cannot scale a single stock.

One line to remember

An exchange doesn't win with more machines. It wins with one queue order nobody can alter: take a number at the door, let one thread work strictly by number, and keep the ledger so you can rewind. Speed, fairness and forensics all grow out of that one move.

Want the order book structure, the sequencer, the latency budget and real exchange numbers? → Switch to the deep read