Overview
Source: The System Design Newsletter — Neo Kim
A stock exchange is a marketplace that enables the fair and transparent buying and selling of financial instruments (stocks, ETFs, etc.). Think of it as a digital farmer's market: sellers set prices, buyers queue by willingness to pay, and the exchange matches them in real-time.
Key Concepts
Order Book — An electronic list of all BUY and SELL orders for a stock, organized by price level. Each side (buy/sell) is sorted by price and timestamp using a doubly linked list for O(1) insertion and removal.
Market Order — Executes immediately at the current market price. Gets priority in the order book.
Limit Order — Executes only at a specified price or better.
FIX Protocol — The industry-standard bidirectional communication protocol brokers use to communicate with the exchange gateway. Assigns unique sequence numbers to messages for integrity verification.
Core Components
- Broker — Allows clients to place BUY/SELL orders on the exchange.
- Gateway — Entry point for brokers. Validates orders, converts to internal format, and routes to the Order Manager.
- Risk Manager — Verifies sufficient funds, blocks unusual activity, and computes fees.
- Wallet — Stores user funds and assets.
- Order Manager — Assigns globally-increasing sequence numbers to all orders. Manages order state (open, filled, cancelled). Acts as a lightweight list of ALL orders.
- Matching Engine — Core of the exchange. Maintains in-memory order books per stock symbol, matches BUY/SELL orders, and generates executions and market data.
- Market Data Publisher — Distributes trade data and order book snapshots to clients in real time.
Critical Path (Trading Flow)
The trading flow is latency-sensitive. Every microsecond matters:
- Broker sends order via FIX protocol to Gateway
- Gateway validates with Risk Manager + Wallet
- Gateway sends to Order Manager (sequence number assigned)
- Order Manager routes to Matching Engine
- Matching Engine matches order and produces execution
- Execution sent back through Order Manager → Gateway → Broker
- Market data published (non-critical path)
Low-Latency Design Techniques
- Deploy all components on a single giant server (no containers, no network hops)
- Use shared memory as event bus (no disk I/O)
- Order Manager and Matching Engine are single-threaded, each pinned to a dedicated CPU (no context switches, no locks)
- Sequential task execution loop
- Co-location: brokers place servers physically near the exchange for reduced network latency
Scale Characteristics
- ~98% of messages are market data (quotes/prices)
- Executions represent only ~2% of messages
- Latency target: microseconds on the critical path
Key Trade-offs
Decision | Reasoning |
Single-threaded matching engine | Avoids locks and context switches |
Shared memory bus | Eliminates network and disk latency |
Sequence numbers on all orders | Ensures fairness and auditability |
In-memory order book | Speed over durability on critical path |