PAYMENT LEDGER

Money is never created or destroyed, only moved

L318 MINSYSTEM DESIGN
PREDICT

You move both legs of every transfer (the debit and the credit) into one database transaction. A client sends a transfer, the server commits it, and the response is lost on the way back. The client retries. What happens?

A ledger has one law: money only moves. Every transfer debits one account and credits another by the same amount, so the sum of all balances never changes. Two things break that law in practice. A crash between the debit and the credit destroys money. A client that times out and retries applies the transfer twice. The fix is two mechanisms together: write both legs as journal entries in one transaction, so a crash leaves all or nothing, and commit an idempotency key in that same transaction, so a retry finds the key and returns the original result.

▶ SIMULATION
CLIENT
waiting on —
retries 0 · ok responses 0
LEDGER (balance rows, updated in place)
alice: $100
bob: $50
carol: $0
sum: $150 / $150
DB WORKER
running — · crashes 0
open txn: —
—
journal (0 entries)none: only the current balance is stored
legs that took effectT1: debits 0 · credits 0   T2: debits 0 · credits 0
✓ sum of all balances is constant ($150)✓ each transfer applied once
SCHEDULE
01[DB]DB: balances updated in place, one autocommit statement per leg, no key
02[CLIENT]CLIENT: send T1: alice → bob $30, key k-7f3a
03[DB]DB: T1 debit alice $30
04[DB]DB: T1 credit bob $30
05[NET]NET: T1 response lost; client times out and retries T1
06[CLIENT]CLIENT: send T2: bob → carol $50, key k-91c2
07[DB]DB: T2 debit bob $50
08[DB]DB: process crashes and restarts; client times out and retries T2
09[DB]DB: T2 credit carol $50 (if the process that started T2 is still alive)
10[DB]DB: respond to T2 (if anyone is still waiting)
11[NET]NET: report
Start of update in place scenario. 11 steps.
-- one autocommit statement per leg, no key
UPDATE accounts SET balance = balance - 30 WHERE id = 'alice';
-- crash here: $30 is gone
UPDATE accounts SET balance = balance + 30 WHERE id = 'bob';
-- response lost here: the client retries and both run again

Watch both invariants and the “legs that took effect” row. UPDATE IN PLACE double-applies T1 when the response is lost, then loses $50 when the crash lands between T2’s two statements. TRANSACTION, NO KEY survives the crash with the books balanced, but T1 still moves twice. FIX: JOURNAL + KEY rolls back the half-written T2, answers the T1 retry from its key, and ends with $150. Even the fault-free in-place run turns the sum red between each pair of UPDATEs: that is what any reader sees at that instant.

BREAK IT NOT YET

Balances updated in place, one statement per leg, a client that retries on timeout. Inject one fault so that money is created or destroyed, or a transfer is applied twice.

GOAL: the sum of balances changes, or a transfer is applied twice

FIX IT NOT YET

Rewrite transfer so that a crash at any point leaves the books balanced, a retry with the same key applies nothing new and returns the original result, and an overdraft is refused. Return { ok: true } or { ok: false, reason }. The grader crashes the process at every ledger call and retries, and runs two transfers at once in every order.

Mental model:

Store movements, not balances: an append-only journal where every transfer’s entries sum to zero, written in one transaction, and a balance is a sum you can always recompute. Then assume every request arrives twice, because a timeout tells the client nothing, and make the second arrival free with an idempotency key committed alongside the entries.

IN PRODUCTION

Wallets, payout systems, marketplace balances, loyalty points, ad-spend budgets, game currencies: anything where a number stands for money. Stripe-style Idempotency-Key headers, double-entry tables (journal, entries, accounts), and nightly jobs that assert the sum is zero.

IN INTERVIEWS

“Design the ledger for a payments app.” Draw accounts and a balance column, then break it: a crash between debit and credit, and a client retry after a timeout. Replace the column with an append-only entries table, each transfer a pair of rows summing to zero, written in one transaction. Add a unique idempotency key in that transaction so retries return the stored result. Mention the funds check under a row lock or serializable isolation, balances as a derived cache, and the reconciliation job that sums every account and pages someone when it is not zero.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: METRICS PIPELINE →