Trading · BraivIQ AI Engineering Playbook
Building A High-Performance Limit Order Book And Matching Engine In Code: Price-Time Priority, Lock-Free Data Structures And 10M+ Orders Per Second
The matching engine is the beating heart of every exchange and trading venue - the piece of code that maintains the order book and pairs buyers with sellers, deterministically, millions of times a second. It is also one of the most demanding systems in all of software engineering: it must be correct to the cent, fair to the microsecond, and fast enough that latency is measured in nanoseconds. In 2026 a new generation of engineers is building these systems in Rust, using lock-free data structures to hit throughputs north of ten million orders per second. This flagship playbook is a developer-and-enterprise-grade tour of how a limit order book and matching engine actually work in code - the data structures, the matching rules, the concurrency, and the reasons this remains one of the hardest and most fascinating problems in systems programming.
· 15 min read · By BraivIQ Engineering
Price-time - Price-time priority: best price first, then earliest order first - the fairness rule the engine must enforce exactly · 10M+ /sec - Throughput modern Rust matching engines target, with latency measured in nanoseconds · Deterministic - Given the same inputs, the engine must produce the same fills every time - non-negotiable · Lock-free - Concurrency without locks - atomics and careful data structures to avoid contention at the core
Behind every exchange, every trading venue, every crypto matching platform sits one piece of software that everything else exists to serve: the matching engine. Its job sounds simple - maintain the order book, and pair buyers with sellers - and is anything but. It must be perfectly correct, because it is trading real value and a single wrong fill is a real financial and legal event. It must be scrupulously fair, applying its matching rules identically to every participant to the microsecond. And it must be extraordinarily fast, because in trading, latency is competitive advantage measured in nanoseconds. This combination - correct to the cent, fair to the microsecond, fast to the nanosecond - makes the matching engine one of the most demanding systems in all of software engineering, and in 2026 it is enjoying a renaissance as engineers rebuild these systems in Rust with lock-free data structures. This flagship playbook is how they actually work in code.
What A Matching Engine Actually Does
At its core, a matching engine maintains a limit order book - the live record of all resting buy orders (bids) and sell orders (asks) for an instrument - and, as new orders arrive, decides what matches. A limit order says 'buy up to this quantity at this price or better'; it either matches immediately against resting orders on the other side or rests in the book waiting. A market order takes whatever liquidity is available now. When a new order can match, the engine pairs it against resting orders and generates trades (fills); whatever is left rests in the book. Everything hinges on the rule that decides which resting order gets matched first, and the industry standard is price-time priority: the best price wins, and among orders at the same price, the one that arrived earliest wins. Encoding that rule correctly, and enforcing it identically for everyone, is the engine's central responsibility and the source of its fairness.
The Data Structure Problem
The order book's data structure is where matching-engine engineering lives or dies, because it must support a punishing set of operations at extreme speed. You need to find the best bid and best ask instantly, add and cancel orders at any price level constantly (cancels vastly outnumber trades in real markets), and match through price levels in strict priority order - all millions of times a second. The canonical design keeps price levels sorted so the best prices are immediately reachable, and at each price level maintains a time-ordered queue of orders so the earliest gets filled first, preserving price-time priority. Cancellation - the most frequent operation - must be fast, which usually means an index from order ID straight to the order's position so you can remove it without scanning. The art is choosing structures where the hot operations (best price, add, cancel, match) are all fast simultaneously, because optimising one at the expense of another is how real engines fall over under load.
- Sorted price levels - so the best bid and best ask are found in constant or near-constant time, and matching walks levels in priority order.
- Time-ordered queue per price level - preserving price-time priority so the earliest order at a price is filled first (FIFO).
- Order-ID index - a direct map from order ID to its location, so the most frequent operation, cancellation, is fast and never scans.
- Compact, cache-friendly memory layout - because at these speeds, cache misses and pointer chasing dominate, so data locality is performance.
Order Types: More Than Just Limit And Market
A production engine supports a family of order types, each a small rule the matching logic must honour, and getting their interactions right is a large part of the work. Immediate-or-cancel (IOC) matches what it can right now and cancels any remainder rather than resting. Fill-or-kill (FOK) executes completely and immediately or not at all. Post-only orders must add liquidity, rejecting if they would immediately match. Iceberg orders show only a small visible slice of a large order, replenishing as they fill, so the full size is hidden from the book. Each type is individually simple but they compose into real complexity - an IOC iceberg, a post-only that would cross - and the engine must handle every combination deterministically and correctly. This is where matching-engine test suites become enormous, because the edge cases between order types are exactly where subtle, costly bugs hide.
Determinism: The Property Everything Depends On
A matching engine must be deterministic: given the same sequence of orders, it must always produce exactly the same sequence of trades. This is not a nice-to-have; it is foundational, because determinism is what lets you replicate the engine for fault tolerance (a replica fed the same inputs reaches the same state), replay history to reproduce and debug any incident exactly, and prove fairness and correctness to participants and regulators. Determinism has hard implications for how you write the code: the matching core must avoid anything that could vary between runs - no wall-clock-dependent decisions in the matching logic, no unordered iteration that could differ, no nondeterministic concurrency in the sequence of events. Orders are typically funnelled into a single, strictly-ordered sequence before matching precisely so that the outcome is a pure, reproducible function of that input sequence. Sacrifice determinism for a speed trick and you lose the ability to replicate, replay and audit - which in a trading venue is unacceptable.
Concurrency And Why Rust Is Winning
Reconciling determinism with extreme throughput is where modern concurrency techniques and language choice come in, and it is why trading firms and exchanges are increasingly building new matching engines and market-data handlers in Rust. The tension is real: the matching core often benefits from being single-threaded and strictly ordered for determinism, while the surrounding system - ingesting orders, validating them, doing pre-trade risk checks, distributing market data - must be massively concurrent. Lock-free data structures, built on atomic operations rather than locks, let multiple threads make progress without blocking each other, minimising the contention that murders latency at these speeds. Rust's appeal is that it delivers this control - manual memory management, no garbage-collection pauses (a killer for tail latency), and fine-grained concurrency - while its ownership model catches whole classes of concurrency and memory bugs at compile time, which in a system trading real money is enormously valuable. The result, in the best 2026 systems, is deterministic price-time matching at ten million-plus orders per second with pre-trade risk checks and UDP-multicast market-data distribution.
Risk Checks And Market Data: The Engine In Context
The matching engine never runs alone. In front of it sit pre-trade risk checks - validating every incoming order against limits before it can touch the book, because an erroneous or malicious order must be stopped before it executes, not after. Behind it, a market-data pipeline reconstructs and distributes the state of the book to participants, typically over high-performance transports like UDP multicast so everyone sees updates fairly and fast. And around it sits the sequencing, persistence and replication that make the whole thing reliable and auditable. Understanding the matching engine in this context matters: the core matching logic is the crown jewel, but it is the surrounding risk, sequencing, persistence and distribution that turn a clever algorithm into a trustworthy trading venue. This is the full architecture BraivIQ thinks about when building trading systems - the engine and everything that keeps it correct, fast and accountable.
A matching engine must be correct to the cent, fair to the microsecond, and fast to the nanosecond, all at once, deterministically, forever. That is why it is one of the hardest problems in systems programming - and why a generation of engineers is rebuilding it in Rust with lock-free data structures to get there.
- BraivIQ Engineering
The Bottom Line For Engineers
The limit order book and matching engine is where the deepest disciplines of systems engineering converge: data-structure design under punishing constraints, deterministic execution, lock-free concurrency, and correctness where mistakes are measured in money. Building one well means getting the price-time-priority data structures right so the hot operations are all fast, handling the full family of order types deterministically, preserving replayable determinism as sacred, and choosing tools - increasingly Rust - that give you nanosecond control without garbage-collection pauses or whole categories of concurrency bugs. It remains one of the most demanding and rewarding problems a systems engineer can take on, and it sits at the literal centre of every market. For teams building trading infrastructure, the engine is where the hardest engineering earns its keep. Educational engineering guidance only - not financial advice.
References & Further Reading
- OrderBook-rs - a high-performance, thread-safe limit order book in Rust with lock-free data structures: https://github.com/joaquinbejar/OrderBook-rs
- Designing a low-latency, high-performance order matching engine (Amitava Biswas, Medium): https://medium.com/@amitava.webwork/designing-low-latency-high-performance-order-matching-engine-a07bd58594f4
- Quantt - Rust for low-latency trading systems: https://www.quantt.co.uk/resources/rust-for-low-latency-trading
- GitHub - order-book topic (open-source matching engine implementations): https://github.com/topics/order-book
- UK FCA - Algorithmic trading compliance in wholesale markets: https://www.fca.org.uk/publications/multi-firm-reviews/algorithmic-trading-compliance-wholesale-markets