Stock Exchange
5 min read- What it is: electronic stock exchange: match buyers/sellers; NYSE does billions of matches/day
- Scale: 100 symbols, 1B orders/day → ~43,000 QPS avg, 215,000 peak (open/close surges)
- Requirements: limit orders only, place + cancel (no replace), normal hours only, risk checks, wallet withholding
- Latency: ms-level round-trip, 99th percentile; availability ≥99.99%
- Critical path: client gateway → order manager (risk + wallet) → sequencer (fairness, replay, exactly-once) → matching engine (deterministic!)
- Non-critical: market data flow (MDP → order books/candles) + reporting flow (accuracy-focused)
Design an electronic stock exchange system. Exchanges match buyers and sellers efficiently. NYSE: billions of matches/day; HKEX: ~200B shares/day.

Figure 1: Largest stock exchanges
Step 1 of 4: Understand the problem and establish design scope
Goal: lock in requirements and scale.
Requirements:
- Trade stocks only (not options/futures)
- Order operations: place new order, cancel order (not replace)
- Order type: limit order only (not market or conditional)
- Normal trading hours only (no after-hours)
- Tens of thousands of concurrent users, 100+ symbols, billions of orders/day
- Risk checks: e.g. max 1M shares of Apple per user per day
- Wallet management: verify sufficient funds, withhold funds for open orders
Non-functional:
- Availability: ≥99.99% (downtime harms reputation)
- Fault tolerance: fast recovery from incidents
- Latency: millisecond-level round-trip (order entry → fill return). Focus on the 99th percentile
- Security: KYC for account opening, DDoS protection for public resources
Back-of-the-envelope estimation:
- 100 symbols, 1B orders/day
- Trading hours: 9:30 AM – 4:00 PM ET (6.5 hours)
- QPS: 1B / 6.5 / 3600 ≈ 43,000
- Peak QPS: 5× ≈ 215,000 (market open/close surges)
Step 2 of 4: Propose high-level design and get buy-in
Goal: learn the domain, then the three flows.
Business knowledge 101
- Broker: intermediary for retail clients (Charles Schwab, Robinhood, Etrade, Fidelity). Provides UI for trades + market data
- Institutional client: trades large volumes via specialized software. Pension funds (infrequent, large volume, need order splitting). Hedge funds/market makers (low latency, commission rebates)
- Limit order: buy/sell with a fixed price. May not match immediately or partially
- Market order: executed at the prevailing price immediately. Sacrifices cost for guaranteed execution
- Market data levels: L1 (best bid/ask + quantities), L2 (more price levels), L3 (price levels + queued quantity at each)

Figures 2-4: Level 1, 2, 3 data
- Candlestick chart: open, close, high, low for a time interval (1min, 5min, 1hr, 1day, etc.)

Figure 5: Candlestick chart
- FIX protocol: Financial Information eXchange (1991). Vendor-neutral protocol for securities transactions
High-level design

Figure 6: High-level design
Three flows:
Trading flow (critical path: latency-sensitive):
- Client places an order via the broker's app
- Broker → exchange
- Client gateway: gatekeeping (input validation, rate limiting, authentication, normalization). Forwards to the order manager 4-5. Order manager: risk checks (e.g. <$1M daily volume)
- Verify sufficient wallet funds 7-9. Order → sequencer (stamps with a sequence ID) → matching engine. Match found → two executions (fills, one each for buy/sell) 10-14. Executions returned to the client
Market data flow (non-critical path):
- M1: matching engine → stream of executions → market data publisher (MDP)
- M2: MDP builds order books + candlestick charts from executions
- M3: MDP → data service (saved in specialized real-time analytics storage). Brokers subscribe → relay to clients
Reporting flow (non-critical path, accuracy-focused):
- R1-R2: Reporter collects fields (client_id, price, quantity, order_type, filled_quantity, remaining_quantity) from orders + executions → consolidated records → database
- Merges attributes from both sources for trading history, tax, compliance, settlements
Trading flow components
Matching engine (cross engine):
- Maintains an order book per symbol (list of buy/sell orders)
- Matches buy and sell orders → produces two fills per match
- Distributes the execution stream as market data
- Must be deterministic: given the same input sequence → produces the same output sequence. Foundation of high availability
Sequencer:
- Stamps every incoming order + outgoing execution with sequential IDs. Functions as a message queue + event store
- Two instances (inbound, outbound), each with its own sequences

Figure 7: Inbound and outbound sequencers
- Purposes: (1) timeliness/fairness, (2) fast recovery/replay, (3) exactly-once guarantee
- Analogous to two Kafka event streams (orders in, executions out). Must be low-latency
Order manager:
- Receives orders from the client gateway → risk check → wallet check → sends to sequencer (only necessary attributes to reduce message size)
- Receives executions via the sequencer → returns to brokers
- Maintains an order state machine (tens of thousands of state transitions in real systems). Event sourcing ideal for this design
Client gateway:
- Gatekeeper, on the critical path. Functions: input validation, rate limiting, authentication, normalization. Must stay lightweight

Figure 8: Client gateway components
- Different gateway types for retail vs institutional
- Colocation (colo) engine: broker's trading engine on rented servers in the exchange's data center. Latency = speed of light between servers

Figure 9: Client gateway
Market data flow

Figure 10: Market Data Publisher
MDP receives executions → builds order books + candlestick charts → publishes to the data service → brokers subscribe.
Reporting flow
Reporter merges order attributes + execution attributes → consolidated reports (trading history, tax, compliance, settlements). Not latency-sensitive; accuracy and compliance are key.