Skip to main content
BLUEPRINT #03Financial & Transactional

Design a Stock Exchange Matching Engine

Target AWS Architecture:AuroraSQS
10-Stage Structure:1. Requirements→2. Sizing→3. Topology→4. Data Model→5. AWS Topology→6. Deep-Dive→7. Failures→8. SRE Playbooks

1. Problem Statement & Scope Clarification

System Mission

Design an ultra-low latency, deterministic electronic exchange matching engine (similar to NASDAQ, CME, and LMAX) capable of executing hundreds of thousands of limit and market orders per second with strict Price-Time Priority (), sub-50 microsecond (<50 μs< 50\,\mu\text{s}) matching latencies, zero lock contention, and zero data loss.

Functional Requirements

  1. Order Ingestion (PlaceOrder): Ingest Limit, Market, and Cancel orders with sub-microsecond parsing using binary protocols (SBE / FIX / ITCH-OUCH).
  2. Deterministic Price-Time Priority ( Matching):
    • Orders matched first by Best Price (Highest Bid, Lowest Ask).
    • Equal price orders matched strictly by arrival sequence timestamp.
  3. Order Cancellation & Modification (CancelOrder / ModifyOrder): Execute cancellations in O(1)O(1) constant time.
  4. Market Data Feed Dissemination: Broadcast real-time Level 2 (Aggregated Price Depth) and Level 3 (Full Order State) feeds to market participants.
  5. Asynchronous Clearing & Settlement: Decouple the ultra-low latency matching core from downstream settlement, risk reporting, and regulatory clearing.

Non-Functional Requirements (SLAs & SLOs)

  • Ultra-Low Latency: End-to-end matching latency P50<15 μsP50 < 15\,\mu\text{s}, P99<50 μsP99 < 50\,\mu\text{s}, P99.9<150 μsP99.9 < 150\,\mu\text{s}.
  • 100% Determinism: Replaying the sequenced input log on a backup node MUST produce an identical trade execution tape.
  • High Availability: Sub-millisecond failover to a hot-standby replica with zero uncommitted state loss.
  • Throughput: Support >200,000Β sustainedΒ orders/sec> 200,000\text{ sustained orders/sec} per matching engine partition.

2. Capacity & Scale Estimation (Back-of-the-Envelope Math)

Ingestion Scale & Latency Budget

  • Listed Tradable Symbols: 10,000Β instruments10,000\text{ instruments} (Equities, Futures, Options).
  • Peak Order Ingestion Rate: 200,000Β orders/sec200,000\text{ orders/sec} per active partition.
  • Daily Order Volume: 500,000,000Β orders/day500,000,000\text{ orders/day} (500M orders/day).
  • Average Trades Executed: β‰ˆ50,000,000Β executions/day\approx 50,000,000\text{ executions/day} (10%10\% fill ratio).

Microsecond Latency Budget Breakdown (50 μs50\,\mu\text{s} Target)

Subsystem ComponentTarget LatencyImplementation Technique
Network Ingress & Kernel Bypass10 μs10\,\mu\text{s}AWS Direct Connect 100 Gbps + DPDK / ENA Kernel Bypass
Monotonic Sequencer & Wire Parse5 μs5\,\mu\text{s}Hardware FPGA / Pinned Sequencer with Simple Binary Encoding (SBE)
LMAX Lock-Free Ring Buffer2 μs2\,\mu\text{s}Cache-line padded memory ring buffer (Zero CPU context switching)
In-Memory Order Matching8 μs8\,\mu\text{s}Single-threaded B-Tree / Intrusive Doubly-Linked List in CPU L3 Cache
Synchronous NVMe SSD Journaling15 μs15\,\mu\text{s}SPDK (Storage Performance Development Kit) append-only
Market Data UDP Multicast10 μs10\,\mu\text{s}Binary Level 2 UDP Multicast Feed
Total End-to-End 50 μs50\,\mu\text{s}Achieved via zero-allocation, zero-garbage collection C++/Rust

Memory Sizing for In-Memory Order Books

  • 10,000Β symbolsΓ—5,000Β openΒ orders/book=50,000,000Β activeΒ orders10,000\text{ symbols} \times 5,000\text{ open orders/book} = 50,000,000\text{ active orders}.
  • Order struct size in memory: 64Β bytes64\text{ bytes} (fits exactly in 1 CPU cache line). TotalΒ ActiveΒ OrderΒ BookΒ Memory=50Γ—106Γ—64Β bytesβ‰ˆ3.2Β GBΒ RAM\text{Total Active Order Book Memory} = 50 \times 10^6 \times 64\text{ bytes} \approx \mathbf{3.2\text{ GB RAM}} The entire global order book easily fits into the physical RAM and L3 CPU cache of a single bare-metal server.

3. High-Level Architecture & AWS Component Mapping

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

4. In-Memory Order Book Data Structures

To achieve sub-microsecond price lookup and O(1)O(1) order cancellation:

  1. Price Levels: Indexed via a Sparse Direct-Indexed Flat Array or Red-Black Tree ordered by price.
  2. Time Priority at Price Level: Intrusive Doubly-Linked List of Order structs ().
  3. Order Lookup Map: Cache-padded Flat Hash Map (order_id -> Order*) enabling O(1)O(1) cancellation without tree traversal.
text
Symbol OrderBook (e.g. AAPL):
BIDS (Highest Price First):
  Price: $185.50 -> [Order 101 (100 sh)] <-> [Order 104 (500 sh)] <-> [Order 109 (50 sh)]
  Price: $185.49 -> [Order 102 (200 sh)] <-> [Order 105 (100 sh)]
  Price: $185.48 -> [Order 108 (1,000 sh)]

ASKS (Lowest Price First):
  Price: $185.51 -> [Order 103 (300 sh)] <-> [Order 106 (150 sh)]
  Price: $185.52 -> [Order 107 (400 sh)]

Cache-Line Friendly C++ Order Struct (64 Bytes Aligned)

cpp
struct alignas(64) Order {
    uint64_t order_id;        // 8 bytes
    uint64_t sequence_num;    // 8 bytes
    uint64_t timestamp_ns;    // 8 bytes
    uint32_t account_id;      // 4 bytes
    uint32_t symbol_id;       // 4 bytes
    int64_t  price_cents;     // 8 bytes
    uint32_t remaining_qty;   // 4 bytes
    uint8_t  side;            // 1 byte (0 = BUY, 1 = SELL)
    uint8_t  order_type;      // 1 byte (0 = LIMIT, 1 = MARKET)
    uint8_t  padding[18];     // 18 bytes padding to fill exactly 64 bytes
    Order*   prev;            // Intrusive list pointer
    Order*   next;            // Intrusive list pointer
};

5. The LMAX Disruptor Lock-Free Concurrency Engine

Multi-threaded matching engines suffer from thread context switching, OS kernel scheduling , and mutex lock contention (10βˆ’50 μs10 - 50\,\mu\text{s} penalties). The system uses the LMAX Disruptor pattern:

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

Why Single-Threaded CPU Pinning Wins

  • Zero Mutex Locks: No CPU spinlocks, condition variables, or kernel context switches.
  • L1/L2 Cache Warmth: Order book memory stays pinned in CPU L1/L2 cache lines with zero cache eviction from context switches.
  • Predictable Execution: Eliminates the long-tail latency distribution (P99.9P99.9 is nearly identical to P50P50).

Part 2: Production Deep-Dive Locked1 Coin = 24 Hours

Unlock Complete Architecture & Production Runbooks

Your Balance:40 Coins

You have explored the free architectural preview (~48%). Spend 1 Coin to unlock the remaining 6 production deep-dive sections for a full 24 hours.

Sections Included in This 24-Hour Pass:
6. Detailed Order Execution Workflow
7. Matching Engine Architecture Trade-Off Matrix
8. Failure Modes, Resiliency & Critical Edge Cases
9. Production Pitfalls & Anti-Patterns (The "Gotchas")
10. Production Runbook & Observability Guide
11. Interview Strategy & System Design Rubric
Keeps page unlocked for exactly 24 hoursSpend coins to fund LLM & compute infrastructure