System Design: Stock Exchange (Walnut Exchange: Matching Engine, Fair Access, Market Data, Clearing, T+1 Settlement)
We design a stock exchange. Our version is called Walnut Exchange. It matches buy orders with sell orders, publishes every price change, and makes sure each trade settles.
As in any system design interview, every number here is our own planning assumption. The design is our own too.
Rules and real events are marked with their year. All times are US Eastern time.
How to read this post. It takes about 70 minutes in full, so it is built to be read in parts:
1. Requirements
1.1 What we are building
Walnut Exchange does five jobs:
- It takes orders from member firms. Brokers and trading firms connect directly. People like Hannah reach the exchange through their broker.
- It matches orders fairly. The best price trades first. At the same price, the earliest order trades first. Every member sees the same rules and the same speed.
- It loses no order it has acknowledged. Once a member is told "accepted", that order survives any single failure.
- It publishes every change to the book. Every subscriber of the same class gets it at the same time, within the published cable differences.
- It clears and settles every trade. A clearing house stands between buyer and seller. On the next business day, money and shares change hands.
The story takes place on Thursday 3 December 2026:
- 09:30:00. The opening auction prices HRBX at $48.10 on 42,000 shares.
- 09:41:07. Hannah in Denver buys 100 HRBX at up to $48.20 through her broker, Pine Securities. Lakeview Capital sells her 60 shares at $48.19. The other 40 rest in the book.
- 09:47:22. MM-East, a market maker, sells 40 at $48.20. Hannah's order is complete.
- 10:31:04. The machine running partition P07's sequencer fails. A standby takes over 1.5 seconds later, and no acknowledged order is lost.
- 11:02:30. KSTQ falls fast and enters a 5-minute trading pause. It reopens at 11:07:30 at $12.38.
- 13:05:21. A trading algorithm at Ridgeline Trading starts sending orders in a loop. At 13:05:41 its risk officer, Luis, presses the kill switch. Every Ridgeline order is canceled.
- 13:31. Megan, in exchange operations, reviews the trades from Ridgeline's loop. They were all inside KSTQ's price band, so they stand.
- 14:10. Ava, at a member firm, sees a gap of 58 messages on one market data channel. Her feed handler fills it from the second copy of the feed.
- 16:00. The closing auction prices HRBX at $48.30.
- Friday 4 December. Every trade settles. Summit Brokerage misses a $3.1 million margin call, and at 10:30 the clearing house declares it in default.
The hard part is doing all of this in microseconds while staying fair and losing nothing. Thousands of orders a second for one stock must be put in a single sequence. That sequence must be the same for everyone and survive a machine failure.
1.2 Words we use
Words used in only one place are explained there. Each word below is used all through the post.
The market
| Word | Meaning |
|---|---|
| Member | A firm allowed to send orders directly to the exchange, such as a broker or a market maker. |
| Class | A group of members who buy the same connection, such as all firms with a 10 Gbps port in the main hall. |
| Order book | For one stock, every resting buy and sell order, sorted by price. |
| Bid, offer | A buy order's price, and a sell order's price. The best bid is the highest; the best offer is the lowest. |
| Price-time priority | The best price trades first. At the same price, the earliest order trades first. |
| Market maker | A member that keeps buy and sell orders in the book all day. |
| NBBO | The national best bid and offer: the best prices across all US exchanges. |
| Tick | The smallest price step: one cent for the stocks in this post. |
| Limit order | Buy or sell at this price or better. |
| Market order | Buy or sell now at the best price on offer. |
| Listing exchange | The exchange where a company is listed. It runs that stock's opening and closing auctions and declares its pauses. |
| Price band | A price range around the stock's average price over the last 5 minutes, often 5% or 10% wide. Trades outside it are refused. A stock that stays at the edge for 15 seconds pauses. The US rule is called LULD, Limit Up-Limit Down. |
| Reference price | The price a rule measures from, such as the last trade before an event. |
The engine
| Word | Meaning |
|---|---|
| Gateway | Our servers where members connect. Each member uses a numbered session. |
| Sequencer | The one program that puts every order for a group of stocks in a single sequence and numbers each one. |
| Journal | The list of every sequenced message, written to three halls. An order is acknowledged after two of them hold it. |
| Hall | A separate room with its own power, inside our one data center. |
| Pinned core | A CPU core that runs only this one program. |
| Epoch | A number that goes up each time a new sequencer takes over a partition. |
| Lease | A right to be a partition's sequencer for 300 ms. The sequencer renews it every 100 ms. If it is not renewed in time, a standby can take over. |
| Standby | A second sequencer and engine for a partition. It runs on another machine and follows the same journal. |
| Partition | One group of stocks with its own sequencer and matching engine. There are 32. |
| Matching engine | The program that applies each message to the order book and makes trades. |
| Deterministic | Given the same messages in the same order, the engine always makes the same trades. |
After the trade
| Word | Meaning |
|---|---|
| Clearing house | The firm that becomes the buyer to every seller and the seller to every buyer. Ours is Walnut Clearing. |
| Novation | The clearing house steps in as the other side of a trade. |
| Netting | Adding up a member's buys and sells so only the difference moves. |
| Margin | Cash a member leaves with the clearing house to cover losses on its open trades. |
| Default fund | A pool every member pays into. It covers a failed member's loss after that member's own margin is used up. |
| T+1 | Trades settle one business day later. |
Systems
| Word | Meaning |
|---|---|
| FIX | The Financial Information eXchange protocol, a standard text format for orders. |
| Binary protocol | A compact order format with fixed fields, like Nasdaq's OUCH. |
| Multicast | One network packet sent once and copied by the network to every subscriber. |
| PTP | The Precision Time Protocol, which keeps all clocks within well under a microsecond of each other. |
| ยตs | A microsecond: a millionth of a second. |
IDs in this post
Every ID below is made up for the story.
| ID | What it names | Example |
|---|---|---|
| HRBX, KSTQ | Stock symbols | Two made-up companies |
| PN-S03 | A member's session | Pine Securities, session 3 |
| GW-14 | A gateway | Pine's session PN-S03 connects to it |
| PN-S03-55102 | A member's order ID | Hannah's order |
| P07 | A partition | Holds HRBX |
| 18,004,118 | A sequence number in P07 | Hannah's order. Numbers start again at 04:00 each day, when the early session opens. The early session trades from 04:00. The opening auction at 09:30 sets the official opening price |
| T07-88120034 | A trade ID | Her first fill |
1.3 Every kind of request and change
The story follows a few people. Walnut Exchange handles much more than that. Each row names the part that handles it.
| Kind | What it is | How big or how often | Which part handles it | Outside party that takes part |
|---|---|---|---|---|
| New order | Buy or sell, limit or market | About a third of all order messages | Gateways, sequencers, matching engines | Members |
| Cancel or replace | Remove or change a resting order | About two thirds of order messages | The same | Members |
| Trade | A match between two orders | About 40 million on a busy day | The matching engines | None |
| Market data | Every change to every book | Up to 1 million messages a second | The Market data publishers | Members and data vendors |
| Auctions | Opening, closing and reopening after a pause | 3 to 4 times a day per stock | The matching engines | None |
| Pause or halt | Stop trading in one stock, or all | A few a day | The Market control service | Regulators, the listing exchange |
| Kill switch | Cancel everything for one member | A few a month | The gateways and sequencers | The member asks |
| Clearing | Novation, netting, margin calls | Every trade, every night | Walnut Clearing | Members, banks |
| Settlement | Shares and money change hands | Every trade, the next business day | Walnut Clearing and the depository | Members, banks |
| Reports to regulators | Every order and trade | Every day | The Reporting job | The Consolidated Audit Trail, CAT |
1.4 Functional requirements
- Order entry. Members send new, cancel and replace orders over a session with sequence numbers.
- Matching. Price-time priority, with trades at the resting order's price. Both limit and market orders.
- Order types. Day and immediate-or-cancel orders. Hidden, pegged and post-only orders.
- Market data. Every change to every book goes to all subscribers at once. Members have a way to fill gaps.
- Auctions. Open, close, and reopening after a pause.
- Controls. Price bands and trading pauses. Market-wide halts, and a kill switch per member.
- Fair access. Every member of the same class gets the same speed.
- Clearing. Become the other side of every trade. Net each member's trades and call margin.
- Settlement. Move shares and money on T+1, and handle a member that fails.
- Records. Keep every message, and report it to regulators.
Out of scope:
- listing new companies;
- the brokers' own systems, which the Robinhood post covers;
- the national price feed run by the industry, called the SIP. We only send our prices to it and read bands and the NBBO from it.
1.5 Non-functional requirements
- No acknowledged order lost. An order is acknowledged only after two of three halls hold it. This covers the loss of a machine or a hall, not of the whole data center.
- Fast. Inside the data center, the time from gateway in to acknowledgment out is under 50 ยตs at p99.
- Fair. The same rules and the same speed for every member of the same class.
- Deterministic. Replaying the journal gives the same trades, every time.
- Available. A failed sequencer or engine is replaced within 2 seconds. A lost data center is replaced within 2 hours. Reg SCI is the US rule that asks this of an exchange's critical systems.
- Exact clearing. Every trade is counted once in the buyer's position and once in the seller's position. Each stock's positions add up to zero across members.
1.6 The numbers
These are rough sums, the kind a candidate does at a whiteboard in a few minutes.
| Number | How we get it | Result | What it decides |
|---|---|---|---|
| Order messages a day | Our planning number for a busy day | 3 billion | 3 billion รท 23,400 seconds is about 128,000 a second on average |
| Peak | About 8 times the average, in the busiest second | 1 million a second | Split into partitions by stock |
| Messages per trade | 3 billion รท 40 million trades | 75 | A cancel must cost no more than an add |
| Per partition | 1 million รท 32 partitions | About 31,000 a second | One thread per partition is enough: at 5 ยตs a message, about a sixth of one core |
| Resting orders | Our planning number of 50 million at peak ร 80 bytes | 4 GB, 125 MB per partition | The book lives in memory |
| Journal | 1 million a second ร 100 bytes; 3 billion ร 100 bytes a day | 100 MB a second at peak, about 300 GB a busy day | Fast disks in three halls, then write-once storage |
| Market data | 1 million updates a second ร 40 bytes | 320 Mbps per feed copy | Multicast: one copy for all members |
| Latency budget | 50 ยตs from gateway to acknowledgment; matching measured at about 5 ยตs in our tests | About 5 ยตs of matching | Most time is network and copying to the halls, not matching |
What these numbers tell us: the book is small and fits in memory. One thread can easily handle 31,000 messages a second. The hard part is ordering.
Every message for one stock must pass through one point, one after another. Each one is copied to three halls.
It must come out identical for every member. All of that has to happen within 50 ยตs.