Skip to content

Stock Exchange / Matching Engine — Detailed#

flowchart TB
  subgraph Traders
    HFT[HFT firms]
    BROK[[Retail brokers]]
    INST[Institutional]
  end

  subgraph Connect[Connectivity]
    COL[Colocation racks]
    FIX[FIX / proprietary protocols]
    GW[Order Gateway]
    THROT[Per-firm throttles]
  end

  subgraph PreTrade[Pre-trade]
    AUTH[AuthN per session]
    RISK[Risk checks<br/>position limits, fat-finger]
    NORM[Order normalize]
    SEQ[Sequencer]
  end

  subgraph Engine[Matching Engine]
    OB[(Order book per symbol)]
    MATCH[Price-time priority match]
    EVT[[Event log]]
    SHARD[Sharded by symbol]
    REPL[Replicas - HA]
    FAILOVER[Active-active or hot standby]
  end

  subgraph Market[Market Data]
    LV1[Level 1 feed BBO]
    LV2[Level 2 depth]
    PROP[Proprietary multicast feed]
    CONS[Consolidated SIP feed]
  end

  subgraph PostTrade[Post-trade]
    DROP[Drop copy]
    CLEAR[Clearing house CCP]
    SETTLE[Settlement T+1 / T+2]
    REG[Surveillance / regulators]
    BIL[Billing]
  end

  subgraph Surveil
    AUD[Spoofing / wash trading detection]
    HIST[Historical event store]
  end

  Traders --> Connect --> PreTrade --> Engine
  Engine --> Market
  Engine --> PostTrade
  Surveil --- Engine

    classDef client fill:#dbeafe,stroke:#1e40af,stroke-width:1px,color:#0f172a;
    classDef edge fill:#cffafe,stroke:#0e7490,stroke-width:1px,color:#0f172a;
    classDef service fill:#fef3c7,stroke:#92400e,stroke-width:1px,color:#0f172a;
    classDef datastore fill:#fee2e2,stroke:#991b1b,stroke-width:1px,color:#0f172a;
    classDef cache fill:#fed7aa,stroke:#9a3412,stroke-width:1px,color:#0f172a;
    classDef queue fill:#ede9fe,stroke:#5b21b6,stroke-width:1px,color:#0f172a;
    classDef compute fill:#d1fae5,stroke:#065f46,stroke-width:1px,color:#0f172a;
    classDef storage fill:#e5e7eb,stroke:#374151,stroke-width:1px,color:#0f172a;
    classDef external fill:#fce7f3,stroke:#9d174d,stroke-width:1px,color:#0f172a;
    classDef obs fill:#f3e8ff,stroke:#6b21a8,stroke-width:1px,color:#0f172a;
    class GW edge;
    class HFT,INST,COL,FIX,THROT,AUTH,RISK,NORM,SEQ,MATCH,SHARD,REPL,FAILOVER,LV1,LV2,PROP,CONS,DROP,CLEAR,SETTLE,REG,BIL,AUD service;
    class OB,HIST datastore;
    class BROK,EVT queue;

Matching engine#

  • One process per symbol (or small group); single-threaded for determinism.
  • Order book = two priority queues (buy / sell), price-time priority.
  • Lock-free hot data structures; everything in memory; event log persists.
  • Latency target: low microseconds at the engine; nanoseconds for HFT colocation.

HA via state machine replication#

  • Sequencer assigns monotonic seq IDs.
  • Engine + 2 hot standbys consume same event stream → deterministic state.
  • Failover by re-pointing gateway to standby.

Market data#

  • Direct feeds from engine to subscribers via multicast.
  • Conflated snapshots for non-HFT clients.

Clearing & settlement#

  • Trades sent to CCP; netted across firms; settlement T+1/T+2 via DTCC / equivalents.

Glossary & fundamentals#

Concepts referenced in this design. Each row links to its canonical page; the tag column shows whether it is a high-level (HLD) or low-level (LLD) concept.

Tag Concept What it is Page
HLD Sharding horizontal partitioning across nodes database-sharding
HLD Pub/Sub & message brokers topics, consumer groups, delivery semantics pub-sub-pattern
HLD Leader/follower replication sync/semi-sync/async replication, failover replication-leader-follower
HLD Event sourcing + CQRS commands -> events; separate read model event-sourcing-cqrs
HLD Multi-region & DR RTO / RPO, active-active, failover multi-region-dr
LLD State machines FSM, HSM, transitions, guards state-machines
LLD Behavioural patterns Strategy, Observer, State, Command, Chain behavioral-patterns