Design a Stock Exchange (NASDAQ, NYSE)
Role: Software Engineer
Category: System Design
Difficulty: hard
Interview Round: Other
Design a stock exchange such as NASDAQ or NYSE. The system must match and route orders, broadcast market updates to every participant as they happen, send private execution reports to the firms whose orders traded, and persist trades for overnight settlement.
Functional requirements:
1. Order entry: participants can submit orders (market and limit) and cancel existing orders.
2. Order matching: the system matches buy and sell orders strictly on price-time priority.
3. Execution reporting: participants are notified of fills, partial fills and cancellations.
4. Market data dissemination: real-time price updates go out to all participants at the same time.
5. Clearing and settlement: end-of-day processes permanently transfer ownership and funds.
Explain how the non-functional targets below shape the design, and cover the data model, the API, the high-level architecture, and the parts of the system that decide latency, fairness and correctness.
```hint Where the order of events is fixed
Ask where in the pipeline the order of inputs gets decided, and what would have to be true for a second copy of the matching logic to reach exactly the same state as the first.
```
```hint The message mix
Look at what participants actually send. Cancels far outnumber fills, so check what your order book costs to remove an order from the middle of a busy price level.
```
```hint Fan-out cost
Work out how long it takes to send one update to a thousand subscribers over a thousand separate connections, and which subscriber hears it last.
```
### Constraints & Assumptions
- Peak load is 1 million or more messages per second (new orders and cancels), with the heaviest bursts at the open and the close.
- The full order matching lifecycle inside the exchange should take under 50 microseconds.
- Orders are processed in exactly the order they arrive at the exchange's network boundary.
- No acknowledged order or trade may be lost. If the primary matching engine fails, a backup takes over without dropping an order.
- Settlement runs after the close, off the trading path.
### Clarifying Questions to Ask
- How many symbols are traded, and can different symbols be matched independently of each other?
- What happens to the unfilled part of a market order when the opposite side of the book runs out?
- Does the exchange run clearing and settlement itself, or hand finished trades to a separate clearing house?
- What depth of market data do participants need: best bid and offer only, aggregated depth per price level, or every individual order?
- Are participants co-located in the exchange's data center, and is fairness measured from the exchange's network edge?
### What a Strong Answer Covers
- The non-functional targets (latency, throughput, fairness, durability) and how the design trades availability against consistency.
- The order book's data structures and the cost of adding, cancelling and matching an order under price-time priority.
- How inputs get a single order, how the matching engine stays deterministic, and how a standby takes over with no lost or duplicated messages.
- How market data reaches every participant at nearly the same moment, and how a participant recovers after a dropped packet.
- How the hot path avoids locks, garbage collection and kernel overhead, how throughput grows past one matching thread, and how post-trade work stays off the matching path.
### Follow-up Questions
- How would you add self-trade prevention, so a firm's buy order never fills against its own resting sell order?
- How does an opening auction work, and how does the engine switch from collecting orders to continuous matching?
- How would you halt trading in one symbol, or enforce price bands, without breaking determinism across replicas?
- What changes if the venue is a crypto exchange that trades around the clock and holds customer balances itself?
Overview: Design a stock exchange that matches market and limit orders on price-time priority, broadcasts market data to every participant at once, and stores trades for end-of-day settlement. It tests order book data structures, sequencing and deterministic failover, multicast market data with gap recovery, and lock-free low-latency engineering.