System DesignSep 14, 2026⏱️ 3 min read
Design a Stock Exchange: Microsecond Sequencers, L1/L2/L3 Order Books & Determinism

Design a Stock Exchange: Microsecond Sequencers, L1/L2/L3 Order Books & Determinism

Where 10 Milliseconds Is an Eternity

A typical web app celebrates 50ms p99 latency. An electronic stock exchange (NASDAQ, NYSE, high-frequency crypto/equities venues) matches billions of orders per day with strict tens-of-microseconds ($\mu s$) latency, 100% deterministic fairness (price-time priority), and zero tolerance for corrupted balances.
Achieving microsecond determinism requires stripping away network hops, database locks, and garbage collection from the critical matching path.
Electronic Stock Exchange Matching Engine Architecture

Electronic Stock Exchange Matching Engine Architecture


1. The Ultra-Low-Latency Critical Path

Broker (FIX Protocol) ──► [Client Gateway + Risk Checks]
                                   │
                                   ▼
                      [Inbound Sequencer (Order ID)]
                                   │ (Memory-Mapped Ring Buffer / mmap)
                                   ▼
               [Single-Threaded Deterministic Matching Engine]
                 ├──► Updates In-Memory Order Book (FIFO Price Levels)
                 └──► Outbound Sequencer ──► Market Data (UDP) & Clearing

Why the Core Matching Engine Is Single-Threaded!

Locking a shared order book across multiple CPU threads (mutex, rwlock) introduces unpredictable OS context-switch stalls and cache-line bouncing (10–100µs jitter). Instead, run an application loop pinned to dedicated isolated CPU cores (1 engine per ticker symbol group, like AAPL), processing ordered events sequentially from a lock-free mmap ring buffer (similar to the LMAX Disruptor pattern)—zero locks, zero heap allocations, deterministic execution!

2. Inside the Order Book: $O(1)$ Price-Time Priority

The matching engine stores resting limit orders (Bid buy book sorted descending, Ask sell book sorted ascending) optimized for three $O(1)$ hot-path operations (New, Cancel, Match):
  • Price Level Map (Hash Map / Tree): Maps price -> PriceLevel.
  • Orders at Each Price (Doubly Linked List): Enforces strict FIFO time priority; maintaining an auxiliary hash map order_id -> node_ptr allows cancelling any resting order in $O(1)$ without scanning lists!

Market Data Feed Levels:

  • L1 Data: Best Bid and Best Ask prices/quantities only.
  • L2 Data: Aggregated order quantities per price level (top 5–20 depth tiers) broadcast via UDP Multicast for ultra-fast broker updates.
  • L3 Data: Full individual resting orders and queue positions.

3. Determinism & Hot-Standby Failover (Zero Data Loss)

How do you replicate an ultra-fast in-memory engine without slowing down every match with SQL commits?
  1. Inbound & Outbound Sequencers assign strict monotonic sequence numbers (seq_id) to every incoming order and outgoing execution event.
  2. Event Sourcing + Raft: Because the matching engine is a 100% deterministic pure state machine (same input sequence $\equiv$ exact same order book state + fills!), Replicated Hot-Standby nodes simply consume the identical sequenced input stream via Raft / Aeron Cluster in real time.
  3. If the primary matching host experiences a hardware fault, the Hot-Standby takes over in milliseconds with zero lost orders or phantom trades!

Share this article

Comments