A payments ledger you can trust
Balances live in one column that gets overwritten. Design a ledger that survives retries, failures and audits.
To design a payments ledger, record every transfer as balanced double-entry rows in an append-only table and derive balances from them. Make requests idempotent with client keys, commit each transfer in one transaction, and reconcile with the external bank using an explicit state machine for unknown outcomes.
The system today: A wallet app keeps each user's balance in a column on the users table. Sending money subtracts from one row and adds to another in the same request. When a request times out the app retries, and support cannot say why some balances are wrong.
What has to change: Design the money-movement system: transfers between wallets, top-ups and withdrawals through an external bank, at 5,000 transfers a second. It must never create or destroy money, must be safe when a client retries, must be auditable, and must keep working when parts of it fail.
Questions to ask first
- Do we need strict consistency on every transfer, or can some flows settle later?
- What are the audit and regulatory requirements: history, immutability, retention?
- Is there more than one currency, and do we hold funds in external accounts?
- What happens when the bank accepts a request but our system never hears back?
Where the interviewer digs in
- An append-only double-entry ledger, with balances as derived values
- Idempotency keys and exactly-once effects across retries
- Reconciling with the external bank and handling unknown outcomes
What a strong answer covers
- Uses a double-entry, append-only ledger where every transfer is a set of entries that sum to zero; balances are computed or materialised from it and can be rebuilt.
- Makes every request idempotent with a client key stored with the result, so a retry returns the original outcome.
- Wraps each transfer in one database transaction, locks or orders accounts consistently to avoid deadlocks, and discusses hot accounts and sharding by account.
- Treats the external bank as unreliable: records intent first, uses a state machine and a reconciliation job for unknown outcomes, and produces an audit trail.
Common mistakes
- Overwriting a balance instead of appending entries, which loses the history.
- A debit and a credit in separate transactions, so a crash between them creates or destroys money.
- Retrying a bank call without an idempotency key.