Stock Exchange System Design
Explore LMAX Disruptor sequencing, pre-trade risk validations, FIFO Limit Order Book matching, UDP multicast publishers, and market circuit breakers.
Continue with the guided workspace
Use the paired workspace to connect this guide's mental model with live component state, visible signals, and the outcome each step or scenario is meant to prove.
A modern stock exchange is a low-latency pipeline that accepts orders, serializes them fairly, matches buyers against sellers by price-time priority, and broadcasts the results β all in microseconds. The hard parts are keeping order strictly fair under massive throughput, never losing an acknowledged order, and halting automatically when prices move too fast.
Mental Model
Think of a stock exchange like an airport security checkpoint combined with an auction. First, clients enter through the gate (order gateway), where their documents are validated (pre-trade risk engine). Once cleared, they are put into a single-file queue (the LMAX Sequencer) to ensure order fairness. Finally, they reach the auction room (matching engine), where buyers and sellers are matched based on price-time priority. To prevent stamps or crashes, there is a supervisor who halts the auction (circuit breaker) if the bidding price jumps or drops too rapidly.
Components and Why They Exist
Client Gateway
Manages external client connections, protocol translation, and rate limiting at the edge of the exchange. Accepts client order requests, validates protocol formats, and translates them into internal message representations. In the demo, observe client order packets entering the exchange and undergoing validation checks at the gateway.
Pre-Trade Risk Engine
Prevents erroneous or unauthorized orders from entering the matching engine, ensuring financial stability. Checks user credits, margin balances, and order size boundaries to prevent catastrophic βfat-fingerβ errors. In the demo, watch how orders are checked against risk parameters and either rejected or forwarded to the sequencer.
LMAX Sequencer
Provides a single-writer, lock-free ring buffer that sequences incoming orders deterministically. Assigns a monotonically increasing sequence number to each incoming order, establishing strict time-priority. In the demo, observe how orders from different gateways are serialized into a single queue before being processed.
Matching Engine (Limit Order Book)
The core of the exchange that maintains active buy/sell interests and matches them based on price-time priority. Organizes resting bids and asks, matches overlapping orders, and updates the order book state in memory. In the demo, watch incoming orders interact with the bids/asks queues and trigger matches, or rest in the order book.
Market Data Publisher
Disseminates the state of the market and trade notifications to all participants simultaneously. Converts matching events into UDP multicast packets and broadcasts them to ensure fair, low-latency market updates. In the demo, observe how trade and depth updates are broadcast to the network immediately following matching events.
Clearing & DB Ledger
Asynchronously records trade executions to database storage and processes post-trade settlement. Persists transactions to disk for audit logging and handles funds/stock settlement without blocking the matching loop. In the demo, observe matched execution records flowing asynchronously to the ledger database.
Edge Cases and Misconceptions
- Asynchronous Persistence is Required for Low Latency: A common misconception is that matching engines write to the database before returning a trade confirmation. Doing so would insert disk I/O latency (milliseconds) into the match loop. Instead, the match is completed in-memory first, and the execution is flushed to disk asynchronously by a separate ledger thread.
- UDP Multicast is Fast but Unreliable: Financial exchanges use UDP multicast for publishing market data because it is a one-to-many protocol with zero TCP handshaking or connection tracking overhead. However, UDP does not guarantee delivery. Exchanges run parallel retransmission servers (TCP gap fill servers) to allow clients to request missing packets.
- Circuit Breakers Must be Automated: When high-frequency algorithms malfunction, they can trigger massive market drops in milliseconds (e.g. Flash Crash). Manual intervention is too slow; exchanges enforce automated circuit breakers that detect volatility bounds in-memory and halt trading instantly.
Common Questions
Why is the LMAX Disruptor used instead of lock-based queues?
Traditional multi-threaded locking causes thread contention, context switching overhead, and cache-line invalidation. The LMAX Disruptor uses a single-writer pre-allocated ring buffer with lock-free memory barriers, allowing sequentially ordered events to be processed at maximum CPU throughput.
What is a Limit Order Book?
It is a data structure containing resting buy orders (bids) and sell orders (asks), sorted by price-time priority. Bids are sorted high-to-low and asks low-to-high to match incoming orders at the best available prices.
How does the Volatility Circuit Breaker work?
If a trade matches at a price that deviates from the reference price by more than a configured volatility threshold (e.g. 5%), the matching engine immediately halts all matching for a cooldown period (e.g. 15 seconds) to prevent runaway panic selling or algorithm malfunctions.
Why do low-latency matching engines process orders on a single thread rather than using multi-threading?
Multi-threaded matching engines suffer from thread scheduling latency, locking overhead, and CPU cache misses. By running matching on a single thread pinned to a dedicated CPU core, the engine can process the Limit Order Book sequentially without lock contention, achieving sub-microsecond matching latencies.
What happens if the UDP multicast network becomes congested?
If the market data network saturates, UDP packets can be dropped by network switches. While this does not affect the matching engine itself (which executes orders in-memory), it causes market data latency to spike for clients who must request TCP gap fills, creating an information disadvantage.
What work can be constant-time in a price-time matching engine?
The price-level map and per-level FIFO queues make lookup and head-of-queue operations constant-time on average. A match may still cross several price levels, so total work depends on how many levels and orders are consumed; the engine should not be described as universally O(1).
How does the Sequencer ensure lock-free execution?
The sequencer uses memory barriers (volatile writes and reads) on a pre-allocated array (ring buffer). The writer thread updates a sequence pointer indicating how many slots are filled, while the reader thread polls the sequence pointer to read completed data. This eliminates the need for OS mutexes or thread context switches.