Hard · Finance and payments
Stock order matching
Brokers and traders send buy and sell orders, and you pair them fairly: the earliest order at the best price goes first. At the market open a rush of orders arrives in seconds. Every accepted order must be in one agreed sequence, and no trade may be lost or invented. Practise this hard system design problem for free with a Google or GitHub account.
The brief
Brokers and traders send buy and sell orders, and you pair them fairly: the earliest order at the best price goes first. At the market open a rush of orders arrives in seconds. Every accepted order must be in one agreed sequence, and no trade may be lost or invented.
Think of an exchange like Nasdaq.
Users, scale and requirements
- Who uses it
- 300,000 active traders and 300 broker systems
- Audience in plain numbers
- 300,000 users or devices
- Normal peak
- ≈ 10,000 requests per second
- Request mix
- Write: 45%; Read: 50%; Sign-in: 5%
- Speed for 99 of 100 requests
- Write: ≈ 250 ms; Read: ≈ 400 ms; Sign-in: ≈ 1,000 ms
- Successful requests
- 99.95%
- Data safety
- No acknowledged write may be lost.
- Monthly budget
- ≈ $35,000
- Data at rest
- ≈ 6 TB
- Where users are
- North America: 100%
- Home region
- North America
- Waiting deadlines
- Write: 3,600 seconds; Read: 900 seconds; Sign-in: 900 seconds
- Fresh reads
- People must see their own changes. At most 0.1% of reads may be stale in the model.
- Personal reads
- 50% of reads are private; a shared public copy cannot answer them.
- Required proofs
- Audit trail; Signed-in access; Tested backups
≈ marks an approximation, just as in the game. Waiting deadlines measure the age of queued work and are separate from interactive response time.
- Orders and cancels are both writes.
- Trading hours are 09:00 to 16:00.
Questions that shape the design
Who decides the order of two orders sent at the same instant?
One sequencer. Everything downstream follows its log, and that log is the truth.
Can we lose an order we already acknowledged?
Never. The acknowledgement means the order is safely in the sequence.
What if the network splits in two?
It is better to stop trading on the smaller side than to let two sides disagree about trades.
How are quotes served?
Quotes are copies. A quote a few seconds old is fine for a portfolio screen, not for matching.
The traffic day
Traffic concentrated around market hours. The clock covers 00:00 to 24:00 in the home region. Events add pressure on top of that normal traffic.
09:30 · Flash crowd
A scheduled launch brings a sudden rush of requests.
Traffic for Write, Read, Sign-in reaches 3 times the scheduled load; rises over 60 seconds, holds for 900 seconds and eases over 900 seconds. Other request types keep their scheduled load.
11:00 · Zone outage
An availability zone loses power or network access.
Duration: ≈ 3,600 seconds.
13:30 · Network partition
Your regions cannot talk to each other during the partition.
Duration: ≈ 300 seconds.
What a strong design has to handle
- Two machines cannot both decide who was first. What single thing can hold the agreed order?
- Preserve price-time priority through leader failure and order retries.
- An ordered log gives one agreed sequence and an audit trail for free. The cost is that one partition handles one stream of orders at a time, so orders are split by instrument, not shuffled.
- Acknowledging at the log means the sender hears "accepted" before the trade is matched. That is how real exchanges work: accepted is not the same as filled.
Make a prediction, run a full traffic day, then compare the result. The in-game editorial opens after a finished day and includes measured reference designs.
Concepts to practise
Related problems
- Easy
Clinic appointment booking
A group of 60 clinics lets patients find a free slot, book it and get a text reminder. Slots open at 8 am, and the phone lines used to melt. Two people must never get the same slot, and reminders must go out even when the text-message company has a bad hour.
- Easy
Invoicing and billing
Small businesses create invoices, email them to customers and take card payments online. On the last working day of the month everyone sends and chases invoices at once. Nobody may get the same invoice email twice, and no invoice may vanish.
- Easy
Payroll run
On pay day your system tells the payment rail to move salaries for two million employees, then publishes a payslip for each one. Employees open the app at breakfast to see if they were paid. Paying anyone twice is the one thing you must never do.
- Medium
Peer-to-peer wallet
People split dinner by sending each other money from their phones. A transfer takes money from one balance and adds it to another, and the sender must see the new balance right away. A flaky mobile connection often makes the app send the same transfer twice.