System Design: Robinhood (Walnut Trade: Stock Trading App, Order Lifecycle, Buying Power, Live Prices, T+1 Clearing)
We design a stock trading app, the kind Robinhood runs. Our version is called Walnut Trade, and it is a broker.
A broker takes a person's order and checks they can pay. It sends the order to a place that trades it. It holds the person's cash and shares for them.
Every number here is our own planning assumption, as in any system design interview. The design is our own too.
Facts about real companies, rules and markets 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 Trade does five jobs:
- It takes each order once. A phone can lose signal and send a tap twice. That tap must still buy once.
- It never spends money the person does not have. Before an order is sent to be traded, the money for it is set aside.
- It sends orders to be traded, and records every fill. A fill is a report that some shares traded, at a price.
- It shows live prices to millions of open phones at once.
- It settles every trade with the clearing house one business day later. Every cent and share in its records stays correct.
The story takes place on Monday 30 November 2026:
- 09:28:40. Emily is a nurse in Columbus. On a bus, she taps "Buy 20 QLTX at up to $50.00". Her signal drops, and the app sends the order again at 09:28:43. She gets one order. $1,000 of her $1,200 is set aside.
- 09:28:52. Emily tries to buy 8 more on her iPad. Only $200 is free, so it is refused.
- 09:29:54. Her order is released to the market maker MM-B to trade at the open.
- 09:30:01. It fills: 12 shares at $49.9875 and 8 at $50.00. She paid $999.85.
- 09:31:05. Sofia buys $25 of QLTX at $49.99, which is 0.500100 shares.
- 10:00:30. Marcus sends a market order for 50 ZORB. $2,100 is set aside. The best offer rises before his order reaches the venue, and it fills at $40.12. The $94.00 left of the hold is released.
- 10:02:11. David cancels his order to sell 300 ZORB. 100 shares fill 50 milliseconds after his tap, and the other 200 are canceled.
- 10:05. Priya's AI agent sends 40 orders a second through its own sub-account. Walnut Trade lets it send only 2 a second.
- 10:41:15. ZORB's exchange pauses trading in it for 5 minutes. New ZORB orders wait, and go out at 10:46:15.
- 20:00. No more changes to the day's trades are allowed. Walnut Trade starts comparing its records with the venues' and banks' records.
- Tuesday 05:11. The clearing house sets Walnut's deposit at $1.6 billion. The gap to what is already there is paid by 09:15, and Emily's shares settle that day.
The hard part is this: every cent and share must stay correct when many requests arrive at the same moment. Retries, cancels and fills arrive in any order. All of it peaks in the same few seconds at 09:30.
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 |
|---|---|
| Broker | A firm that holds people's accounts and sends their orders to be traded. Walnut Trade is one. |
| Venue | A place where orders trade: an exchange, or a market maker. |
| Market maker | A firm that buys and sells from its own account all day. Some market makers fill most orders from apps like ours; these are called wholesalers. |
| NBBO | The national best bid and offer: the highest price anyone will pay, and the lowest anyone will sell for, across all exchanges. |
| Price improvement | A fill at a better price than the NBBO, such as $49.9875 when the best offer is $49.99. |
| Payment for order flow | Money a market maker pays a broker for sending it orders. It is legal in the US, and must be disclosed. |
| Fill | A report that some or all of an order traded, at a price. One order can have many fills. |
The order
| Word | Meaning |
|---|---|
| Limit order | "Buy at up to $50.00": it never pays more than the limit. |
| Market order | "Buy now at any price". We still add a hidden price limit called a collar, so the order cannot fill at any price. |
| Stop order | "Sell if the price falls to $45": it becomes a market order when that price trades. |
| DAY, GTC | How long an order stays open: until the day ends, or until it is canceled, up to 90 days. |
| Hold | Money set aside for an open buy order, so it cannot be spent twice. |
| Buying power | The money a person can use now: settled cash plus unsettled cash, minus holds. |
| Idempotency key | A random ID the app puts on each order tap. When the app sends the same tap again, Walnut Trade still makes only one order. |
The money
| Word | Meaning |
|---|---|
| Ledger | The list of every money and share movement. Each movement is written as plus and minus lines that add up to zero. |
| T+1 | Trades settle one business day after the trade: money and shares change hands then. |
| Settled cash | Cash from trades that have settled. Unsettled cash is the net of trades that settle tomorrow: plus for sales, minus for buys. |
| NSCC | The National Securities Clearing Corporation. It stands between brokers until each US stock trade settles. If one broker fails, the other broker's trade still completes. |
| Clearing deposit | Cash a broker must leave with NSCC every day. The more risk its unsettled trades carry, the bigger the deposit. |
Systems
| Word | Meaning |
|---|---|
| Cell | One shard of accounts: a PostgreSQL database and its services, holding about 1.9 million accounts. There are 16 cells. |
| Cell map | A small store that says which cell holds each account, such as acc_31845 in cell c07. Every order reads it first. |
| Controls store | One place for the rules every cell must obey: halted symbols, margin rules and limits per symbol. A halt reaches every cell within 5 seconds. |
| PostgreSQL | A relational database with transactions. |
| Kafka | A log that keeps messages in order, split into partitions, until readers take them. |
| FIX | The Financial Information eXchange protocol: the standard messages brokers and venues use for orders and fills. |
| WebSocket | One connection that stays open, so the server can send prices whenever they change. |
| p99 | 99 of every 100 requests finish faster than this time. |
IDs in this post
Every ID below is made up for the story.
| ID | What it names | Example |
|---|---|---|
| acc_31845 | An account ID | Emily's |
| ik-7f3a9c | An idempotency key | Emily's tap at 09:28:40 |
| c07-o-5521890 | An order ID, starting with its cell | Emily's order in cell c07 |
| MMB-E-88124 | A fill ID from the venue | Her first fill, 12 shares |
| QLTX, ZORB | Stock symbols | Two made-up companies |
1.3 Every kind of request and change
The story follows a few people. Walnut Trade serves many more kinds of request than the story shows. Each row names the part that does that work.
| Kind | What it is | How big or how often | Which part handles it | Outside party that takes part |
|---|---|---|---|---|
| Place an order | Buy or sell, limit or market, shares or dollars | Up to 50,000 order messages a second at the open | The Order service and the account cells | None at this step |
| Cancel or replace | Stop or change an open order | About 2 of every 3 order messages | The Order service and the Router | The venue |
| Route and fill | Send an order to a venue; record each fill | About 5 million orders a day | The Router and the Fill handler | Market makers and exchanges |
| Live prices | Prices for symbols on screen | 6 million open apps, 5 million price messages a second in | The Feed handlers, Quote bus and Stream gateways | Exchanges sell the price feeds |
| Account page | Cash, buying power, positions, orders | About 1.5 million requests a second at the open | The Account service and the cells | None |
| Fractional order | Buy by dollars, such as $25 | About 1 in 5 orders | The Fraction service | None |
| Deposit and withdraw | Move money from or to a bank | About 2 million a day | The Money service | Banks, through ACH, the US system for bank transfers |
| End of day | Stop changes to the day's trades, compare records, settle | Once a day, from 20:00 | The Clearing job and the Reconciler | NSCC, DTC, which holds the shares, venues and banks |
| Controls | Halt a symbol, raise a margin rule | A few a day | The Controls store | Regulators and exchanges halt symbols |
| Statements and tax forms | Confirms, monthly statements, yearly tax forms | Every trade, every month, every year | The Records job | None |
This post follows Emily's order end to end. Marcus shows a market order and David a cancel.
Sofia shows a dollar order and Priya an AI agent. The other kinds use the same parts.
1.4 Functional requirements
- Accounts. Open a cash or margin account, link a bank, deposit and withdraw.
- Live prices. The app shows live prices and charts for symbols the person watches or holds.
- Place orders. Market, limit and stop orders. By shares or by dollars. DAY or GTC.
- Follow orders. The person sees each order's state and every fill. Cancel or replace an open order.
- Never overspend. An order is accepted only if its worst-case cost fits the person's buying power.
- Positions and buying power. The shares a person holds and their buying power always match the ledger.
- Route for the best result. Send each order to the venue that gives the best result. Save the reason that venue was chosen.
- Settle every trade on T+1, and reconcile with venues, the clearing house and banks.
- Records. Trade confirmations, statements, tax lots and yearly tax forms.
- Controls. Halt trading in a symbol, or change a rule, within seconds.
Out of scope:
- options, crypto and the overnight market, beyond a short note;
- matching orders, which venues do;
- how a market maker prices its fills.
1.5 Non-functional requirements
- One order per tap. A retry with the same idempotency key returns the first order. A repeated fill report is applied once.
- No overspend. Two orders can never use the same dollars, even when they arrive at the same moment.
- Nothing is lost. An accepted order, a hold or a fill survives the loss of a data center zone.
- Fast. An order is accepted within 200 ms at p99. A fill shows in the app within 1 second of the venue's report.
- Fresh prices. Prices on screen update every second. A price over 2 seconds old shows "delayed", and orders never use the phone's price.
- Books balance. Every ledger posting adds up to zero, and every difference with a venue or bank has an owner by 22:00.
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 |
|---|---|---|---|
| Accounts | Our planning number | 30 million | Split accounts into 16 cells of about 1.9 million each |
| Order messages a day | 5 million orders × 3 messages each, with cancels and replaces | 15 million, about 640 a second over 6.5 hours | The average is small. The peak at the 09:30 open sets how big the system must be |
| Peak order messages | Our planning number for 09:30 | 50,000 a second | 50,000 × 4 transactions ÷ 16 cells = 12,500 transactions a second per cell, inside one PostgreSQL primary |
| App requests at the open | 6 million open apps, each asking about once every 4 seconds | About 1.5 million a second | Cache the account page, and drop non-essential requests first |
| Prices in | Exchange feeds for 12,000 symbols at the open | 5 million messages a second | Combine them into 10 updates a second per symbol before sending them to phones |
| Prices out | 6 million phones × about 300 bytes a second | About 15 Gbps | One update a second per phone, only for symbols on screen |
| Orders waiting for the open | 1.5 million queued ÷ 20,000 a second of venue room | 75 seconds of venue room | Release them from 09:20, so venues still have space for new orders at 09:30 |
| Records | 15 million messages × about 2 KB, with fills and ledger lines | About 30 GB a day | Keep them for years in write-once storage, which SEC Rule 17a-4 allows |
What these numbers tell us: the average is easy, and the open is not. In the same few seconds, 50,000 order messages and 1.5 million requests a second arrive. At the same time, 1.5 million queued orders want to go out.
Every one of those orders must still set money aside exactly. That is why accounts are split into cells, each one a normal PostgreSQL database.