~learn.
0:00 / 1:21
LLD · Order Book

A matching engine with zero locks, on purpose

transcript

This matching engine has zero locks inside the order book. And it's simpler for it. Not slower.

The obvious design: a lock around the book, many matcher threads. Matching is short and CPU-only. A lock just adds contention. And the order of events depends on who won the lock. Instead: one matcher thread per symbol. No locks in the book. One total order. The same input always gives the same trades.

Gateway threads build and validate each order. Each order goes onto its symbol's queue. The only synchronised object. One matcher thread per symbol runs commands one at a time. Two symbols never interact. So they run in parallel.

Price-time priority needs one order of events anyway. Someone has to decide who was first. The queue does.

The hammer test: eight threads, 1,000 random orders each. Neither book is ever crossed. Every share sent is traded or resting. All accounted for.

And if the matcher dies? Log every command before matching. Replay the log. Same book. Same trades.

One matcher per symbol, behind a queue. No locks, and a log you can replay.

Full breakdown in the lecture.