Order Matching Engine
A stock exchange doesn’t just record trades. It runs an algorithm that decides which buyer gets matched with which seller. That algorithm is the matching engine, and its design choices are unusually interesting.
The Limit Order Book#
The core data structure is the limit order book (LOB): two sorted collections of orders, bids (buy orders) and asks (sell orders). Bids are sorted by price descending (highest buyer first), asks by price ascending (lowest seller first). A trade happens when the best bid price meets or exceeds the best ask price.
When you submit a limit order at $99.50, you join the bid side at that price level. If there’s already a queue at $99.50, you wait behind orders that arrived earlier. This is price-time priority: best price wins first, then earliest arrival wins ties.
Why Single-Threaded Is Intentional#
The matching engine for a given instrument typically runs on a single thread. No concurrent access, no locks on the hot path. If you have multiple threads competing for the order book, you need synchronization, and synchronization adds latency and creates non-determinism in the match order.
Single-threaded with a queue is faster than multi-threaded with locks, at the throughput levels most exchanges operate at.
At Oracle#
I didn’t work in finance, but I worked on transaction reconciliation for billing systems. The lesson was the same: order matters when replaying events. If two transactions both modify the same account and you replay them out of sequence, you get the wrong final state. The matching engine makes this explicit by assigning a global sequence number to every order before matching. Event sourcing gives you this property too: replay in sequence, always get the same result.
What I’m Learning#
Market orders (execute immediately at whatever price is available) versus limit orders (execute only at my specified price or better) are essentially priority levels. Market orders cut the queue; limit orders wait in it.
What other single-threaded-by-design systems have you worked with?