Skip to content

Case study: Electronic stock exchange

Design a deterministic low-latency order matching engine with price-time priority, sequenced command logs, and O(1) order book operations.

35 min9 steps

By the end you can
  • Estimate peak order/cancel message rate and prove that the entire active order book fits in CPU cache and RAM
  • Design an in-memory limit order book using price levels, doubly-linked FIFO queues, and an orderId hash map for O(1) cancel and match
  • Use a partitioned sequencer log and single-threaded deterministic matching to guarantee strict price-time priority without ordering violations
  • Predict how the exchange handles hot earnings-release symbols, market-open bursts, and hot-standby failover

Pin down exchange fairness and determinism

Price-time priority

  • Best price first; FIFO within price; deterministic replay

Order entry

  • Integer ticks; clOrdId dedup; fills/cancels; no lost sequenced commands

Why don't stock exchanges store the live order book in a relational database and match orders with `SELECT ... FOR UPDATE`?

Answer this step to continue.