Building An Order Matching Engine
Ever bought stocks or shares? Chances are, you’ve interacted with an order matching engine without knowing it.
An order matching engine is the core of an exchange. It matches each incoming buy or sell order against the orders resting in the order book, waiting to be filled. Anything that can’t be matched straight away joins the book and waits its turn.
For a matching engine to be effective, it needs to be:
- Fair - Orders are filled by price-time priority: best price first, and among orders at the same price, earliest first.
- Deterministic - The same sequence of orders must always produce the same trades. This is what makes fairness verifiable, and it lets the engine recover from a crash by replaying its input.
- Fast - Markets move quickly, and the occasional slow match matters more than the average speed. The number to watch is tail latency: how slow the slowest matches are.
Together, these lead to a counterintuitive design choice: the matching core is typically single-threaded. Because arrival order decides who gets filled first, several threads sharing one book would just take turns behind a lock, adding overhead without adding parallelism. A single thread also makes determinism straightforward, since orders are processed one at a time in the order they arrived. Only the core is single-threaded, though: the rest of the pipeline (network I/O, message parsing, market data publishing) works on separate threads.
In this series, we’ll implement a limit-order matching engine in several programming languages, beginning with C++, Haskell, Python, and Rust, then profile each one to see where its time goes. Each language takes a different approach to memory management and concurrency, and profiling will show what those approaches cost.
First, though, we’ll look at how an order matching engine works: what a limit order is, how the order book is structured, and the architecture every implementation will share.
2026