Order books and matching engines
Updated Nov 10, 2026 · 4 min read
An exchange has one job. It takes orders to buy and sell, and pairs them up fairly and fast. The part that does the pairing is the matching engine, and the list of orders waiting to be paired is the order book. The design is unusual for a modern system, with one thread, everything in memory and one strict line of events, and each of those choices is there on purpose.
Orders and the book
Most orders are limit orders, such as “buy 500 shares at $50.05 or less”. If something on the other side matches, the order trades right away. Whatever can’t trade rests on the book. The book has two sides, bids to buy sorted from the highest price down, and asks to sell sorted from the lowest price up. The gap between the best bid and the best ask is the spread.
Price, then time
When an order arrives, the engine matches it against the best price on the other side first. Among resting orders at the same price, the one that arrived first fills first. This is price-time priority, and it rewards both offering a better price and showing up early. Each trade happens at the resting order’s price.
| Bid | Price | Arrived | Fills |
|---|---|---|---|
| Carol, 200 | $50.06 | Last | 200, best price |
| Alice, 500 | $50.05 | First | 400, earliest at $50.05 |
| Bob, 300 | $50.05 | Second | None, still waiting |
An incoming order walks the book until it is filled or reaches its limit. A buy for 1,000 at $50.11 takes everything offered at $50.10, then at $50.11, and stops before $50.12. Whatever is left rests on the book as a new bid at $50.11.
One thread per stock
Every order has to see the book exactly as the order before it left it. That makes matching one stock a strict line, which is why engines run one thread per stock with no locks. Threads sharing a book would need locks, would race, and could match orders in a different order than they arrived. A single thread working on a book in memory can handle around a million orders a second, far more than any one stock sees. The exchange scales by spreading different stocks across cores and machines.
The sequencer and the log
Before an order reaches the engine, a sequencer stamps it with the next number and appends it to a log. That number, not any clock, defines the order of events. Gateways in front of it check that orders are well formed and that traders are within their limits, so bad orders never cost the engine anything.
Crashes and standbys
The book lives in memory, so a crash loses it. Rebuilding is simple because the engine is deterministic. Load the latest snapshot of the book, then replay every logged order after it in sequence order. The result is exactly the book before the crash. Rebuilding from the trades table would miss resting orders and cancels, and sorting by gateway timestamps would get the order wrong, since clocks differ and timestamps tie.
Most exchanges go a step further and run a standby engine that reads the same log as it is written. It holds the same book at all times, so it can take over in moments instead of minutes.
The latency budget
Traders measure an exchange in microseconds. If the target is 100 microseconds from an order arriving to its answer going out, a database commit at about a millisecond would spend ten times the budget on its own. That is why the book is in memory and the log is an append-only file copied to a standby, rather than rows in a database.
| Choice | Why |
|---|---|
| Book in memory | No disk or database on the matching path |
| One thread per stock | No locks, no races, deterministic replay |
| Sequenced log | One agreed order of events, and recovery by replay |
| Snapshots | Recovery replays an hour of the log, not a whole day |
| Standby engine | Takes over in moments, already holding the book |
Estimating the load
A trading day is 6.5 hours, or 23,400 seconds, and the load is not even. The open and the close are far busier than midday. For 500 million orders a day, the average is about 21,000 a second across all stocks. At ten times that in the first minute, and 5% of it on the busiest stock, its engine sees about 11,000 orders a second, a small slice of one thread’s capacity.