Design a Stock Exchange
Understanding the Problem
📈 What is a stock exchange? A stock exchange is a marketplace where brokers send buy and sell orders for listed securities, and the exchange matches them into trades. Its core is the matching engine: for every symbol it keeps an order book of resting buy orders (bids) and sell orders (asks), and when a new order crosses the book it produces a trade at a fair price. The exchange then publishes what happened (trades and book changes) to everyone at the same time. NYSE, Nasdaq and the London Stock Exchange all work this way.
This question is different from most system design problems. There's no huge user base, no feed and no petabytes of storage. The participants are a few hundred member firms (brokers, banks, market makers), and the hard parts are latency measured in microseconds, strict ordering, and never losing or double-counting an order. The design ends up looking more like a carefully engineered single-threaded program with a replicated log than like a fleet of stateless web servers.
Functional Requirements
Start by agreeing on who the users are. Retail investors don't talk to the exchange directly; their broker does. So our clients are member firms connecting over dedicated sessions.
Core Requirements
- Members should be able to submit, cancel and modify orders (limit and market orders), and get an acknowledgment for each.
- The exchange should match orders using price-time priority and report fills (execution reports) to both sides of every trade.
- Members and data subscribers should be able to receive real-time market data: trades and changes to the order book.
Below the line (out of scope)
- Clearing and settlement (moving the shares and the cash). A separate clearing house does this after the trade, typically a day later.
- Listing companies, corporate actions, and the regulatory reporting pipeline in detail.
- Opening and closing auctions. We'll mention them, but focus on continuous trading.
- Retail-facing features like accounts, portfolios and charts (that's a broker; see /system-design/stock-brokerage).
Non-Functional Requirements
Ask your interviewer for a scale. A reasonable one for a large equities exchange: 10,000 symbols, around 1 billion order messages per day (new orders, cancels and modifications, most of which never trade), and a few hundred member firms.
Core Requirements
- The system should match orders with very low and very predictable latency: tens of microseconds inside the matching engine, and well under a millisecond from the order arriving at the gateway to the acknowledgment leaving it. Tail latency (p99, p99.9) matters as much as the median.
- The system must be strictly ordered and deterministic: every order is processed in one agreed order, and replaying the same input produces exactly the same trades.
- The system must never lose an acknowledged order or trade, and must recover from a server failure in seconds, not minutes.
- The system should be fair: no member gets market data or order access earlier than another on the same connection type.
- The system should handle bursts of 10-20x the average rate at the open, the close and around news.
Below the line (out of scope)
- Multi-region active-active matching. Exchanges match in one data center (with a disaster-recovery site), and members co-locate next to it.
- Elastic autoscaling. Capacity is provisioned for the worst day, not adjusted live.
The math changes the design. US regular trading hours are 6.5 hours, or 23,400 seconds. One billion messages over 23,400 seconds is about 43,000 messages per second on average. With a 20x burst at the open, that's on the order of 1 million messages per second across the whole exchange. The busiest few symbols can see a large share of that.
That sounds like a lot, until you notice it's spread over 10,000 independent order books. A well-written single-threaded matching loop can process a few million simple book operations per second on one core, because each operation is a handful of memory reads and writes. So we don't need a distributed database. We need to partition symbols across a small number of matching engines and make each one a tight, single-threaded program.
Storage is modest too. At roughly 100 bytes per message, 1 billion messages is about 100 GB per day of input journal. One NVMe drive absorbs that easily. The constraint is never throughput or space; it's latency and ordering.
The instinct to reach for Kafka, Postgres and a microservice per step is the wrong one here. Every network hop costs tens of microseconds and adds jitter. Say early that the hot path is a single process holding the order book in memory, and that durability comes from a replicated sequenced log, not from a database write per order.