Design a Stock Exchange Order-Matching Platform
Design the core of an exchange: it takes orders from trading firms, matches buyers and sellers fairly and in order, publishes the same market data to everyone, and survives a machine failure without losing an order or repeating a trade.
Problem statement
Design the core of an exchange: the platform that takes buy and sell orders from trading firms, matches them as they arrive, tells each firm what happened to its orders, and publishes every trade and every change to the order books to all participants.
For this exercise, assume the exchange lists about 3,000 symbols and keeps one order book per symbol.
Trading firms connect through order gateways, each over a session, and send three kinds of request: a limit order (buy or sell up to a quantity at a given price or better), a market order (buy or sell a quantity at the best prices available right now, never left resting in the book) and a cancel of an order that is still resting.
A buy and a sell trade when the buy price is at least the sell price. Within a book, the best price goes first, and at the same price the order that arrived first goes first. A trade happens at the price of the order that was already resting, and one order may fill in several pieces.
Every request gets one explicit outcome: accepted, rejected with a reason, filled in whole or in part, or cancelled. A firm that loses its connection may send a request again with the same client order id, and that must never create a second order. Every order is for one symbol, so order matters only within a symbol.
Prices are whole ticks (the smallest price step) and quantities are whole shares. To change an order, a firm cancels it and sends a new one.
Participants also need market data: every trade and every change to a book, delivered in order and identical for everyone, with a way for a subscriber that joins late or misses messages to catch up.
If a machine fails, the exchange must carry on with exactly the same books, without losing an accepted order and without applying a fill twice. Assume one data center.
Clearing and settlement, opening and closing auctions, trading halts, market surveillance, margin, billing, and the trading firms' own systems are out of scope.
What to build
- A trading firm submits limit and market orders and cancels resting orders, and every request gets one explicit outcome, even when the firm sends it again after losing its connection.
- Orders in one symbol trade by best price first and earliest arrival at the same price, at the resting order's price, with partial fills, and each trade is reported once to both firms.
- Every participant receives the same ordered stream of trades and book changes for the symbols they follow, and a late or lagging subscriber can catch up to the current book.
- After a machine failure the exchange continues with exactly the same books, with no accepted order lost and no fill applied twice.
At scale
- For this exercise, assume about 3,000 symbols, up to 200,000 requests a second (new orders and cancels together) across the exchange at peak, and one hot symbol receiving up to 20,000 requests a second.
- For this exercise, assume that when the exchange is healthy, 99 percent of requests are acknowledged within 5 milliseconds, measured from the request reaching an order gateway to the acknowledgement leaving it.
- For this exercise, assume that if the machine running a book fails, trading in that book resumes within 5 seconds with no accepted order lost and no change to the book.
- For this exercise, assume about 1,000 market-data subscribers, each following any of the symbols, and that one slow subscriber must never slow matching or the other subscribers.