Quick revision
The short answer
A single thread that owns an order book needs no locks, and its results depend only on the order of its inputs, which gives both speed and determinism; we scale by spreading symbols across threads and machines. A sequencer gives every incoming message a number and writes it to a journal before matching, the engine consumes the journal strictly in order, and replicas consume the same sequenced stream with the same deterministic logic, so a replay or a replica reaches exactly the same state and can take over at a known sequence number. Latency comes from in-memory books, pre-allocated objects, ring buffers, pinned busy-spinning threads and kernel bypass, and determinism forbids hidden inputs such as the clock.
What to remember
In this answer, we saw that a single thread per order book avoids locks and makes results depend only on input order, and that we scale by partitioning symbols across threads and machines. The sequencer numbers every message and writes it to a replicated journal before matching, so replaying the journal rebuilds identical state and replicas following the same stream can take over at a known sequence number. Low latency comes from in-memory books, pre-allocation, ring buffers, busy-spinning pinned threads and kernel bypass, and determinism requires that time and other outside values arrive in the sequenced input.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A single thread that owns an order book needs no locks, and its results depend only on the order of its inputs, which gives both speed and determinism; we scale by spreading symbols across threads and machines. A sequencer gives every incoming message a number and writes it to a journal before matching, the engine consumes the journal strictly in order, and replicas consume the same sequenced stream with the same deterministic logic, so a replay or a replica reaches exactly the same state and can take over at a known sequence number. Latency comes from in-memory books, pre-allocated objects, ring buffers, pinned busy-spinning threads and kernel bypass, and determinism forbids hidden inputs such as the clock.
Why this question is asked
It surprises many engineers that the fastest matching engines are single-threaded. We are taught that more threads mean more throughput, but a matching engine is a case where locks, contention and nondeterminism cost more than parallelism gives. Interviewers ask this to see whether we understand the reasoning behind the architecture, popularised by designs such as the LMAX exchange and its disruptor, rather than just the vocabulary. In this answer, we will see why one thread per book wins, how the sequencer and journal turn the engine into a replayable state machine, what makes it fast, how replicas take over, and which hidden inputs break determinism.
1Why One Thread Per Book
Every order for a symbol reads and changes the same small structure: the best price levels of its book. If several threads matched orders for the same book, they would need locks around it, and under load they would spend their time waiting for each other and moving the book's memory between CPU caches. Worse, the result would depend on which thread won each lock, so the same inputs could produce different trades.
One thread owning the book avoids both problems. It processes one message at a time, with the book hot in its CPU cache, and needs no locks at all. A modern core can match several million simple orders per second this way. The result depends only on the order in which messages arrive, which we control. To use more cores, we partition by symbol: thread 1 owns symbols A to F, thread 2 owns G to M, and so on, on one or several machines. Symbols are independent, so this partitioning costs nothing in correctness.
2The Sequencer and the Journal
Orders arrive at many gateways at once, so something must decide their single order. The sequencer does that: it takes messages from the gateways, gives each one the next sequence number, and writes it to a journal, an append-only log, before the engine sees it. The journal is replicated to other machines, so a sequenced message survives the loss of one machine. The engine then consumes the journal strictly in sequence order, never skipping or reordering.
This turns the engine into a deterministic state machine. Its state after message N is a pure function of messages 1 to N. Recovery is a replay: load the latest snapshot of the books, taken at some sequence number, and replay the journal from there. Outputs, the acknowledgements, executions and market data, are numbered too, so a gateway that lost some can ask for them again by number.
- Gatewaysreceive client messages, validate, risk checks
- Sequencerassigns sequence numbers, one order for everything
- Replicated journalinput written and copied before matching
- Matching engine threadsone per symbol group, in-memory books
- Output journalnumbered executions and market data
- Gateways and market datadeliver, resend by sequence on request
6 layers, from the top down. Step through them one at a time.
Replicas read the same input journal and produce the same outputs.
3Where the Microseconds Go
Determinism gives us correctness; the latency comes from removing every source of delay on the hot path. Books live entirely in memory, so matching never touches a disk. Objects such as orders and execution messages are pre-allocated in pools and reused, so the Java garbage collector has almost nothing to collect and causes no pauses; with Java 25, low-pause collectors such as ZGC help further, but the real win is not allocating on the hot path at all.
Stages are connected with ring buffers, fixed-size arrays that one thread writes into and the next reads from using sequence counters instead of locks, which is the idea behind the LMAX disruptor. The threads busy-spin, checking for new work in a tight loop instead of sleeping, because waking a sleeping thread costs microseconds, and each thread is pinned to its own CPU core so it keeps its cache. Network messages bypass the operating system kernel using libraries that let the application read the network card directly, which saves the system call and copy costs. Together these keep the time from a message reaching the engine to its acknowledgement in the low microseconds.
4Replicas and Takeover
A replica is a second engine on another machine that reads the same replicated input journal and runs the same code. Because the logic is deterministic, after processing message N it holds exactly the same books as the primary. It does not need to copy the primary's state; it computes it. Its outputs are suppressed while it is a standby, but it records the last sequence number it has processed.
When the primary fails, the replica takes over at a known point: it has processed everything in the journal up to sequence N, so it continues with N plus 1. No order is lost, because anything sequenced was journaled and replicated before the primary saw it, and no order is reordered, because the sequence decides the order, not the arrival time at the replica.
8 messages in order. Play them, or step through them one at a time.
The replica already holds identical books, so takeover is a matter of who speaks.
5Hidden Inputs That Break Determinism
Determinism holds only if every value the matching logic uses comes from the sequenced input or from the state built from it. Reading the wall clock inside matching breaks it, because the replica reads a slightly different time; so does generating a random number, iterating over a structure whose order depends on memory addresses, or reading a configuration file that might change. The fix is to put such values into the stream.
Notice that time itself becomes an input. Even the market close is a sequenced message, so a replay months later produces exactly the trades that happened.
6Follow-up questions
Does single-threaded mean the engine cannot use the whole machine?
Only the matching of one symbol group is single-threaded. The gateways, risk checks, journaling, replication and market data each run on their own threads or machines, and many matching threads run side by side, each owning its own symbols.
What about orders that involve two symbols, such as spreads?
Multi-leg orders break the clean partitioning, because two books must be updated together. Venues usually place related symbols, such as a future and its spreads, on the same engine thread, or run a dedicated engine for combinations that sends implied orders into the outright books.
How large does the journal get, and how is recovery kept fast?
Each message is perhaps 64 to 200 bytes, so even hundreds of millions of messages per day are tens of gigabytes. Engines take snapshots of the books at known sequence numbers, for example every few minutes on a replica, so recovery loads a snapshot and replays only the journal after it.
7Conclusion
In this answer, we saw that a single thread per order book avoids locks and makes results depend only on input order, and that we scale by partitioning symbols across threads and machines. The sequencer numbers every message and writes it to a replicated journal before matching, so replaying the journal rebuilds identical state and replicas following the same stream can take over at a known sequence number. Low latency comes from in-memory books, pre-allocation, ring buffers, busy-spinning pinned threads and kernel bypass, and determinism requires that time and other outside values arrive in the sequenced input.
Open with
An order book is a small, hot structure that every order touches, so multiple threads would need locks and fight over the cache, and the result would depend on who won the lock.
Then, if they want more
One thread owning the book needs no locks, keeps the book in its cache and makes the outcome depend only on input order, so I scale by giving each thread a group of symbols.
A sequencer gives every incoming message a sequence number and writes it to a replicated journal before matching, and the engine consumes the journal strictly in order. That makes the engine a deterministic state machine: replay from a snapshot plus the journal and you get identical books and trades.
Replicas read the same journal and run the same code with outputs suppressed, so on failure a replica takes over from the last sequence it processed, with nothing lost or reordered.
For latency I keep books in memory, pre-allocate objects so there is no garbage collection, connect stages with ring buffers like the disruptor, busy-spin threads pinned to cores and use kernel-bypass networking. Determinism also means no hidden inputs: no wall clock, randomness or config reads inside matching; the sequencer stamps the time into the message.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why are matching engines usually single-threaded per group of symbols, and how do a...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/f1586eb85a/