Requirements
- Match incoming buy and sell orders by price then time priority.
- Process orders deterministically with microsecond latency.
- Never lose an order and produce an auditable trade record.
High level design
The matching engine is a single threaded in memory order book per symbol.
- Order book: two sorted structures, bids descending and asks ascending, indexed by price level with time ordered queues.
- Sequencer: a single sequencer assigns a strict total order to incoming orders before matching.
- Matching: a new order crosses the book, filling against the best opposite price until exhausted or rested on the book.
- Persistence: append every event to a durable journal so the book can be rebuilt after a crash.
Bottlenecks
- Determinism: matching must be single threaded per symbol so the outcome is reproducible.
- Latency: keep the book in memory and avoid locks and garbage to hit microsecond budgets.
- Recovery: replay the journal into memory to restore state without losing orders.
Tradeoffs
- Single threaded per symbol limits one symbol throughput but guarantees deterministic order.
- In memory speed requires a separate durable journal for safety.
Key idea
A matching engine is a single threaded in memory order book ordered by a sequencer and backed by a durable journal, trading parallelism for deterministic microsecond matching.