Build Stripe
The build where correctness beats availability, latency, and scale. Ledgers, idempotency where duplicates cost money, and reconciliation.
#The product
A merchant charges a customer's card. Money moves. Refunds work. Every movement is auditable years later.
This is the one build where scale is not the problem. 10M transactions/day is ~100 TPS average — a single tuned Postgres handles that comfortably. Diagnosing that early and redirecting to correctness is itself the senior move.
Full interview treatment in the Payment System case study.
#Phase 1 — The ledger, before anything else
Never store money as a mutable balance column. UPDATE accounts SET balance = balance - 50 has three problems. It overwrites the old value, so history is gone. Two concurrent updates can interfere and lose one of the changes (a race). And when a balance looks wrong, nothing can tell you why it is what it is.
Use double-entry bookkeeping instead: money never appears or disappears, it only moves. So every transaction is recorded as a set of entries — at least one account debited, at least one credited — and the entries always sum to zero.
Customer pays ₹500 for an order:
entry txn account direction amount
────────────────────────────────────────────────────
e1 t1 customer:4711 DEBIT 500.00
e2 t1 merchant:pending CREDIT 500.00
─────────
sum = 0 ✓What this buys you:
- Entries are immutable. A mistake is corrected with a reversing entry, never an update. History is the truth.
- Balance is derived —
SUM(credits) − SUM(debits). You can save a precomputed balance (a snapshot) so you don't re-add every entry on each read, but if the snapshot and the entries ever disagree, the entries win. - The invariant is continuously checkable. Every transaction sums to zero, and you can verify that on a schedule. Bugs become detectable rather than silent.
Store amounts as integers in the smallest unit. Never floats. Store the currency alongside every amount; never assume.
#Phase 2 — Idempotency
A duplicate here is a double charge. This is the most important mechanism in the build.
POST /charges
Idempotency-Key: 7f3a-...-9b2c
BEGIN;
INSERT INTO idempotency_keys (key, request_hash, status)
VALUES (:k, :h, 'in_progress');
-- the UNIQUE constraint IS the atomic gate
COMMIT;
duplicate key?
status = completed → return the STORED response verbatim
status = in_progress → 409, client retries
request_hash differs → 422 (key reused with different params)Details that separate a working implementation from a broken one:
- Store and replay the exact response, including the charge ID. A retry must return the same charge, not a fresh success.
- Hash the request body. A key reused with different parameters is a client bug that must fail loudly, not silently succeed.
- Pass the key through to the card network. Your retry must not create a second charge downstream either.
- Let the unique constraint do the check. "SELECT to see if the key exists, then INSERT" has a gap: two simultaneous retries can both see "not there" and both proceed. Inserting directly and letting the database's
UNIQUEconstraint reject the second one closes that gap — only one insert can ever win.
The ambiguity is fundamental. If you call the provider and time out, you cannot know whether the charge happened. Maybe your request was lost; maybe the charge went through and only the reply was lost — from your side the two look identical. That's the two generals problem: no protocol over an unreliable network can remove that uncertainty, so it's not a gap in your design. The resolution is to query by your idempotency key before retrying, and to reconcile afterwards. Saying this plainly beats pretending a retry is safe.
#Phase 3 — Ordering the saga
Payment spans systems you don't control, so 2PC is unavailable — two-phase commit needs every participant to agree to commit together, and a card network won't take part in your transaction. Instead you use a saga: a sequence of separate steps, each with an "undo" (a compensating action) in case a later step fails. Order the steps so irreversible ones come last:
1. Reserve inventory reversible (TTL expiry)
2. Create payment intent reversible (cancel)
3. CHARGE THE CARD ← PIVOT: irreversible-ish
4. Confirm order forward-only from hereThe pivot is the point of no return — the step that can't be cleanly undone. Everything before it is cheap to reverse, so it goes first. Before it, compensate (undo the earlier steps); after it, roll forward (keep retrying the remaining steps until they succeed). If step 4 fails after the card was charged, you do not silently drop it — you retry until the order is confirmed, or issue a refund as a deliberate, recorded action. "Money taken, nothing delivered, no record" is precisely the failure this ordering prevents.
#Phase 4 — Reconciliation
The safety net that catches everything the design missed. Every real payment system has one; mentioning it unprompted is a strong signal.
Daily, against the provider's settlement report:
in provider, not in ledger → we lost a record; write the entries
in ledger, not in provider → charge never happened; reverse
amount mismatch → investigate (FX? partial refund?)
Nothing auto-resolves silently. Unresolved → alert a human.Plus continuous internal invariant checks: every transaction sums to zero, no account that should be non-negative is negative, the sum of all accounts is zero.
No matter how careful the design, distributed systems produce edge cases. Reconciliation is how you find them before your customers or auditors do.
#Phase 5 — Webhooks in both directions
A webhook is the reverse of an API call: instead of you asking for updates, the other side posts them to a URL you registered. Payments need them in both directions.
Inbound (provider → you): assume the same event can arrive twice, so recognise repeats by the provider's event ID, and append every event to a log before acting on it. And don't rely on webhooks alone — poll for anything stuck in processing past a threshold, because a webhook that never arrives is silent.
Outbound (you → merchants): use the outbox pattern. Write the event into an outbox table in the same database transaction as the state change, and have a separate relay process read that table and send the webhooks. Either both the change and the event are saved, or neither is — so you can never charge a card and then forget to tell the merchant. Retry with exponential backoff, sign the payload so merchants can verify authenticity, and expose a replay endpoint for when their endpoint was down.
#The decision log
| Decision | Rejected | Because |
|---|---|---|
| Immutable double-entry ledger | Mutable balance column | Destroys history; races; unauditable |
| Integer minor units | Floats/decimals | Floating-point rounding on money is a known bug class |
| Idempotency key + unique constraint | Dedupe check then insert | Only the constraint is atomic under concurrency |
| Store and replay the response | Return fresh success | A retry must yield the same charge |
| Query provider before retry | Blind retry on timeout | Timeout means unknown, not failed |
| Irreversible step last | Charge first | Minimizes what must be compensated |
| Forward recovery after the pivot | Compensate | Money is taken; the obligation is real |
| Reconciliation | Trust the design | Every real payment system has edge cases |
| Relational DB with real transactions | Eventually consistent store | The invariants are exactly what ACID protects |
#What you'd cut for v1
Multi-currency, payouts, subscriptions, disputes, fraud scoring. Ship: charge a card idempotently, record it in a double-entry ledger, refund, reconcile daily. That's a complete, correct payments core.
#Explaining the design
"First: this isn't a scale problem. 10M transactions a day is about 100 TPS — one Postgres handles it. The problem is correctness and auditability, so that's where I'd spend the time.
I'd start with the data model: an immutable, append-only, double-entry ledger. Balances are derived, corrections are reversing entries, amounts are integers in minor units. That makes the core invariant — entries sum to zero — continuously verifiable.
Idempotency is the critical mechanism, gated by a unique constraint in the same transaction as the work, storing the response so a retry returns the identical charge. The honest part is that a provider timeout leaves the outcome genuinely unknown — that's the two generals problem — so I query by idempotency key before retrying rather than blind-retrying.
Steps are ordered so the card charge is last among reversible operations. After that pivot I roll forward, never back.
And regardless of all of it, daily reconciliation against the provider's settlement report — because distributed systems produce edge cases and I'd rather find them before an auditor does."
#Next
Level 5: Build an LLM Inference Platform — the same engineering principles with GPUs as the scarce resource.
#Check yourself
Why is Stripe "not a scale problem"?Show answer
What does a double-entry ledger buy?Show answer
How is charge idempotency gated?Show answer
Why can't you tell whether a timed-out charge happened?Show answer
What is the pivot in a payment saga?Show answer
How do inbound and outbound webhooks differ?Show answer
Why is reconciliation part of the design?Show answer
#What to carry forward
- Diagnose correctness, not scale.
- Lead with an immutable double-entry ledger in integer minor units.
- Idempotency gated by a unique constraint, replaying stored responses.
- Timeouts are unknowable: query, never blind-retry.
- Charge last, roll forward after the pivot, reconcile daily.