System design · like Stripe
Design a payments system
Take people’s money. Exactly once, even when everything goes wrong.
The interview question
Design a payment system: a shop charges a card, the bank approves it, the money moves.
Here is the simplest version that works. An interviewer doesn’t want the finished drawing. They want to watch you keep it standing while it breaks, one problem at a time.
Break 1 of 4 · 🔁 Messages repeat
The reply gets lost
The bank charged the card, but the reply never reached the shop. The shop tries again.
Payments charged exactly once: 2 charges for 1 coffee
How do you make trying again safe?
- Each payment carries a key; a repeat key returns the first result
- Never try again
- A faster server
Show the fix
Each payment carries a key; a repeat key returns the first result. The second try finds the key it has already seen and answers with the first result. Nobody pays twice.
Payments charged exactly once: 1 charge, however many tries
Why not the others?
- Never try again. Then a lost reply is a lost sale, and the customer doesn’t know if they paid.
- A faster server. Speed doesn’t stop a reply getting lost on the way.
The idea behind it: Idempotency keys.
Break 2 of 4 · 💥 Things break
The bank is down for a minute
The card network stops answering. Every payment in that minute fails.
Payments charged exactly once: 1,200 payments failed
What should happen to payments while the bank is down?
- Keep them in a queue, and retry after a short, random wait
- Retry instantly, in a loop
- Tell the customer it failed
Show the fix
Keep them in a queue, and retry after a short, random wait. Payments wait safely, and the random wait stops everyone retrying at the same instant and knocking the bank over again.
Payments charged exactly once: All 1,200 went through a minute later
Why not the others?
- Retry instantly, in a loop. Thousands of instant retries hit the bank together the moment it comes back, and knock it over again.
- Tell the customer it failed. Most would have worked a minute later. You’d lose the sale for nothing.
The idea behind it: Exponential backoff with jitter.
Break 3 of 4 · 🤝 Must agree
Two servers change a balance at once
Two payments for the same shop land at the same moment. Each reads the balance, adds its amount, and writes it back.
Payments charged exactly once: The shop is missing $40
How do you keep balances exactly right?
- Never edit a balance: add an entry for each payment, and add them up
- Whoever writes last wins
- Lock the whole database for each payment
Show the fix
Never edit a balance: add an entry for each payment, and add them up. Adding an entry can’t overwrite someone else’s. The balance is the sum of the entries, and every cent has a record. That’s a ledger.
Payments charged exactly once: Every cent accounted for
Why not the others?
- Whoever writes last wins. That’s exactly the bug: the second write silently erases the first payment.
- Lock the whole database for each payment. It’s correct, but one payment at a time won’t survive a busy day.
The idea behind it: The append-only log.
Break 4 of 4 · 👥 More people
Black Friday: 100× the payments
Shops everywhere have a sale. One Payments server can’t keep up and requests start timing out.
Payments charged exactly once: 38% of payments time out
How does Payments handle 100× the traffic?
- Many identical copies, behind a load balancer that shares the work
- One much bigger server
- Cache the payments
Show the fix
Many identical copies, behind a load balancer that shares the work. Payments keeps nothing in its own memory (keys and the ledger live elsewhere), so any copy can take any payment. Add copies as traffic grows.
Payments charged exactly once: Every payment through in under a second
Why not the others?
- One much bigger server. There’s always a biggest server, and when it dies, everything stops.
- Cache the payments. Every payment is new. There’s nothing to reuse, and money can’t be a stale copy.
Say it in the interview
- Start simple: app → Payments → bank, with balances in a database.
- Retries are safe because every payment has an idempotency key.
- If the bank is down, payments wait in a queue and retry with jitter.
- Balances are a ledger: entries are added, never edited.
- Payments keeps no state, so it scales by adding copies.
The six forces behind every design
Every break above is one of these. Learn the forces and the moves that answer them, and a design you have never seen stops being new.
- Many copies behind a load balancer
- A cache for what’s asked again and again
- A waiting line, and a fair limit each
- Do it later, from a queue
- Send results as they’re made
- Ask twice, take the faster
Break it yourself.
The diagram is live: press Break it, watch what goes wrong, and pick the fix. Free, nothing to install.