Design a payment ledger
depth · 45 min · senior/staff
Setting
You're building the internal ledger behind a payments product: money moves between a customer's balance, a merchant's balance, and the platform's own fee account on every transaction, and every one of those movements has to be auditable and exactly correct — no exceptions, ever.
The ask
Design the ledger service that records every balance-changing event, keeps balances always reconcilable, and survives a retried or duplicated request without double-moving money.
Constraints & scale
- ~50M transactions/day, each touching 2–3 accounts (customer, merchant, platform fee)
- A transaction must be atomic: all of its account movements happen, or none do
- The same transaction request retried after a timeout must never be applied twice
- At any point, the sum of all account balances must reconcile to zero (money is only ever moved, never created or destroyed by the ledger itself)
- Auditors must be able to reconstruct any account's balance at any past point in time from the recorded history alone
What a strong answer covers
Requirements → data model (double-entry accounting or equivalent, and why) → idempotency and atomicity mechanism → high-level diagram → 1–2 deep dives → trade-offs. Likely probes: how idempotency keys prevent double-application, how atomicity holds across multiple account updates, and how historical balance reconstruction actually works.
A 45-minute plan
- 0-5 min · Requirements
- 5-10 min · Estimates/API
- 10-25 min · High-level
- 25-40 min · Deep dive
- 40-45 min · Wrap
What to cover, in order
- Requirements
- Data model (double-entry accounting or equivalent, and why)
- Idempotency and atomicity mechanism
- High-level diagram
- 1–2 deep dives
- Trade-offs
Where the follow-up questions go
Pick one of these for your deep dive before the interviewer picks for you.
- How idempotency keys prevent double-application
- How atomicity holds across multiple account updates
- How historical balance reconstruction actually works
Key trade-offs
Decisions a strong answer weighs, and what pushes each one either way.
- Derived or maintained balances. Summing entries on every read is always correct and gets slower as history grows. Keeping a running balance is fast and can drift from the entries if any code path updates one without the other. Keep a maintained balance written in the same transaction as the entries, and reconcile it against the history on a schedule.
- One transaction or a saga. A single database transaction across the accounts gives atomicity with no extra design and limits you to one database and its contention. A saga with compensating steps scales across systems and exposes intermediate states you must handle. At 50M transactions a day, the question is whether accounts can be sharded so each transaction stays within one shard.
- Account sharding and hot accounts. Sharding by account spreads load, and a platform fee account touched by every transaction becomes a hot row. Splitting it into sub-accounts and summing them removes the hot spot and complicates balance reads.
- Isolation and locking. Serialisable transactions or explicit row locks prevent two debits from overdrawing a balance, at the cost of throughput on busy accounts. Optimistic checks with retries do better when conflicts are rare.
- Idempotency storage. A unique constraint on the client key in the same transaction as the entries is the strongest guarantee. Keeping the keys in a separate cache is faster and can disagree with the ledger after a crash.
Common mistakes
What goes wrong most often on this problem, and why it hurts.
- Updating a balance column in place. With no immutable history, you cannot audit, reconcile or rebuild a past balance.
- Editing or deleting a wrong entry. Auditors need to see both the error and the correction, so fix mistakes with a new offsetting entry.
- Storing money as floating point. Rounding errors accumulate, and the books stop adding up. Use integer minor units and store the currency with every amount.
- Retries without a key. A timeout leaves the caller unsure whether the first attempt succeeded, so retrying creates a second movement. The key has to come from the caller and be checked atomically.
- Treating reconciliation as optional. The zero-sum invariant only protects you if something checks it continuously and alerts when it breaks.
Graded on
Requirements & scoping · Estimation · API & data model · High-level architecture · Deep dive · Scalability & performance · Reliability & consistency · Trade-offs · Operability · Communication & drive