Black Friday: 30% off every plan through November 30. See the prices
SysDesignPrep.com
Study guide

Updated 24 October 2026

Low-latency systems and matching engines

How exchanges and other microsecond systems are built: single-threaded deterministic cores, in-memory order books, sequencers and event logs, replication by replay, avoiding garbage collection and locks, kernel bypass, and fairness.

Reading is half of it. Now try it in an interview.An AI interviewer asks you to design a Stock Exchange, out loud or typed, and scores the answer for your level. Or watch a sample interview first, free with no sign-up to start.
Start an AI mock

Try it yourself, in a playground:Tail latency and hedged requestsBatching

Most web systems measure latency in milliseconds. Exchanges, trading systems and some game servers measure it in microseconds, and they must also be perfectly fair and never lose an order. The architecture that achieves this looks very different from a typical microservice stack: one thread doing all the critical work, everything in memory, and a log that makes the whole thing replayable. Stock exchange questions test whether you know this design.

The order book

An order book holds resting buy orders (bids) and sell orders (asks) for one instrument, sorted by price, then time (price-time priority). A new order:

  • Matches against the best opposite orders while prices cross, producing trades.
  • Rests in the book with any remaining quantity (limit orders), or is cancelled (market or immediate-or-cancel orders).

Data structures: a map from price level to a queue of orders, with the best price at the top (a sorted tree or an array indexed by price ticks), plus a hash map from order id to order for fast cancels. All in memory.

Single-threaded and deterministic

The matching engine for each instrument (or group of instruments) runs on one thread:

  • No locks, no contention, no race conditions: orders are processed strictly in sequence.
  • Deterministic: the same input sequence always produces the same output. This is what makes replication and recovery simple.
  • One core can process millions of orders per second when it never waits for I/O.

Scaling comes from partitioning by instrument, not from multithreading one book. See sharding and partitioning.

Sequencer and event log

Before matching, a sequencer assigns each incoming order a global sequence number and writes it to a durable, replicated log. The engine consumes the log in order. Consequences:

  • Fairness: arrival order is defined once, by the sequencer.
  • Recovery: replay the log (from a snapshot) to rebuild the exact state.
  • Replication: standby engines consume the same log and stay in identical state, ready to take over within milliseconds.
  • Downstream systems (market data, clearing, risk, audit) consume the output event stream.

This is event sourcing at its most extreme. See event sourcing and CQRS.

Avoiding latency spikes

At microsecond scale, the enemies are pauses and jitter:

  • No garbage collection pauses: preallocate objects and reuse them (object pools), or use languages and settings without GC on the hot path.
  • No locks or syscalls on the hot path; pass data between threads with lock-free ring buffers (the LMAX Disruptor pattern).
  • CPU pinning: dedicate cores to critical threads, isolated from the OS scheduler.
  • Cache-friendly data: arrays over pointer-chasing structures, data laid out in memory order.
  • Kernel bypass networking (DPDK, specialised NICs) to skip the operating system's network stack.
  • Batching the durable log writes without delaying individual acknowledgements beyond the budget.

Measure p99.9 and the maximum, not averages. See tail latency.

Around the core

  • Gateways accept client connections, authenticate, validate and rate-limit orders, then forward to the sequencer.
  • Risk checks (sufficient balance, position limits) happen before matching; they must also be fast, often in memory per account.
  • Market data publishers broadcast book updates and trades, frequently over multicast to many subscribers at once.
  • Clearing and settlement happen later, asynchronously, from the trade log. See payments and ledgers.

Fairness and colocation

Exchanges must treat participants equally: equal cable lengths in the data centre, randomised or batched processing in some venues, and deterministic sequencing. Some modern venues use frequent batch auctions (match every few milliseconds) to reduce the value of pure speed.

Beyond exchanges

The same ideas apply to other systems that need fast, consistent decisions on shared state: game servers (a single authoritative simulation loop per match), real-time bidding, matchmaking pools and in-memory leaderboards. See Design Matchmaking and Design a Leaderboard.

In the interview

For Design a Stock Exchange: gateways, then a sequencer writing to a replicated log, then a single-threaded in-memory matching engine per instrument partition, with hot standbys replaying the same log, and market data and clearing consuming the output. Then explain how you avoid pauses on the hot path.

Checklist

  • In-memory order book with price-time priority and fast cancels.
  • Single-threaded deterministic engine, partitioned by instrument.
  • Sequencer plus durable log for fairness, recovery and replication.
  • Hot standbys replaying the log.
  • No GC, locks or syscalls on the hot path; pinned cores; ring buffers.
  • Pre-trade risk checks, market data fan-out, asynchronous clearing.

Questions people ask

What is a low-latency system?

A low-latency system measures latency in microseconds rather than the milliseconds typical of web systems; exchanges, trading systems and some game servers are examples, and exchanges must also be perfectly fair and never lose an order. The architecture differs from a typical microservice stack: one thread does all the critical work, everything is kept in memory, and a log makes the whole system replayable. The same ideas apply to real-time bidding, matchmaking pools and in-memory leaderboards.

What is a matching engine?

A matching engine is the core of an exchange: it holds the in-memory order book for an instrument and matches each new order against the best opposite orders while prices cross, producing trades. Remaining quantity rests in the book for limit orders or is cancelled for market and immediate-or-cancel orders. Each engine runs on one thread and is deterministic, and one core can process millions of orders per second when it never waits for I/O.

How does an order book work?

An order book holds resting buy orders (bids) and sell orders (asks) for one instrument, sorted by price and then time, which is called price-time priority. It is built as a map from price level to a queue of orders, with the best price at the top (a sorted tree or an array indexed by price ticks), plus a hash map from order id to order for fast cancels. Everything is kept in memory.

Why are matching engines single-threaded?

A single thread processing orders strictly in sequence needs no locks and has no contention or race conditions, and it is deterministic: the same input sequence always produces the same output. Determinism is what makes replication and recovery simple, and one core can process millions of orders per second when it never waits for I/O. Scaling comes from partitioning by instrument, not from multithreading one order book.

What does a sequencer do in a stock exchange?

A sequencer assigns each incoming order a global sequence number and writes it to a durable, replicated log before matching, and the engine consumes that log in order. Defining arrival order once gives fairness, and replaying the log from a snapshot rebuilds the exact state after a crash. Standby engines consume the same log to stay in identical state, ready to take over within milliseconds, while market data, clearing, risk and audit consume the output event stream.

How do low-latency systems avoid latency spikes?

At microsecond scale the enemies are pauses and jitter, so the hot path avoids garbage collection, locks and syscalls. Preallocate and reuse objects, pass data between threads with lock-free ring buffers (the LMAX Disruptor pattern), pin critical threads to dedicated cores, prefer cache-friendly arrays over pointer-chasing structures, and use kernel bypass networking such as DPDK. Measure p99.9 and the maximum, not averages.