How Stock Exchanges Work: Low-Latency Trading System Design
The Need for Speed
Stock exchanges are among the most demanding systems in existence. The NYSE processes over 3 billion messages daily with latency requirements in microseconds - not milliseconds, microseconds. Understanding this architecture reveals the extreme end of system design.
Core Components
1. Order Gateway
- Entry point for orders
- Validation and normalization
- Risk checks
- Rate limiting
2. Matching Engine
- Heart of the exchange
- Matches buy and sell orders
- Executes trades
- Maintains order books
3. Market Data Publisher
- Distributes price updates
- Multicast for efficiency
- Sub-millisecond delivery
4. Risk Management
- Pre-trade risk checks
- Position limits
- Circuit breakers
The Matching Engine
Order Book Structure:
Each security has an order book:
- Bid side: buy orders (highest price first)
- Ask side: sell orders (lowest price first)
- Price-time priority within price levels
Matching Algorithms:
1. Price-Time Priority (FIFO)
- Orders matched by best price
- Same price: first order wins
- Most common in equity markets
2. Pro-Rata
- Orders at same price get proportional fills
- Common in futures markets
3. Price-Time with Size Priority
- Larger orders get priority
- Encourages liquidity provision
Low-Latency Techniques
1. Kernel Bypass
- Standard network stack too slow
- Direct NIC access via DPDK or custom drivers
- Saves microseconds per message
2. Lock-Free Data Structures
- Locks cause latency spikes
- Lock-free queues and order books
- Careful memory ordering
3. Memory-Mapped Files
- Avoid system calls for persistence
- Direct memory access
- Operating system handles flushing
4. CPU Pinning
- Critical threads on dedicated cores
- Avoid context switching
- Disable hyperthreading on these cores
5. FPGA Acceleration
- Hardware-accelerated order processing
- Nanosecond latency for hot paths
- Common at top exchanges
Network Architecture
Multicast Market Data:
- One-to-many distribution
- UDP multicast for efficiency
- Sequence numbers for gap detection
- Redundant feeds for reliability
Co-location:
- Trading firms place servers in exchange data centers
- Minimize physical distance
- Equal cable lengths for fairness
- Measured in feet, not miles
Network Hardware:
- Cut-through switches (forward before fully received)
- 10/25/100 Gbps links
- Optimized network topology
Order Types
Market Order:
- Execute immediately at best available price
- Guaranteed execution, not price
Limit Order:
- Execute only at specified price or better
- May not fill if price never reached
Stop Order:
- Becomes market order when trigger price hit
- Used for risk management
Complex Orders:
- Iceberg (hidden size)
- IOC (immediate or cancel)
- FOK (fill or kill)
- Pegged orders
Fault Tolerance
High Availability Requirements:
- 99.999% uptime (5.26 minutes/year downtime)
- Seamless failover
- No lost orders or trades
Strategies:
1. Hot Standby
- Synchronized backup matching engine
- Sub-second failover
2. Order Replay
- All orders journaled
- Can rebuild state from journal
- Used for disaster recovery
3. Circuit Breakers
- Halt trading on extreme moves
- Prevent cascade failures
- Required by regulators
Regulatory Considerations
Audit Trail:
- Every order and trade logged
- Nanosecond timestamps
- Retained for years
- Available for regulatory review
Market Surveillance:
- Real-time manipulation detection
- Pattern analysis
- Insider trading detection
Best Execution:
- Obligation to get best price for customers
- Requires robust market data
- Documented execution quality
Scaling Challenges
Peak Processing:
- Opening/closing auctions
- Major news events
- Market volatility
Solutions:
- Over-provision capacity (10x normal)
- Graceful degradation
- Rate limiting (throttling)
- Queue management
Interview Application
When designing trading systems:
Key Requirements:
- Latency (microseconds)
- Throughput (millions of messages/second)
- Correctness (no lost orders, accurate matching)
- Availability (five 9s)
- Auditability (complete history)
Common Questions:
- Design a matching engine
- How to handle market data distribution
- Implementing price-time priority
- Handling system failures
Trade-offs:
- Latency vs throughput
- Consistency vs availability
- Feature richness vs simplicity
Stock exchange architecture represents the extreme of low-latency, high-reliability system design - techniques here apply (in diluted form) to many other systems.
Ready to Build Your Perfect Resume?
Let IdealResume help you create ATS-optimized, tailored resumes that get results.
Get Started Free