Design a payment system with a double-entry ledger
Design the payment system behind a marketplace. Customers pay, money is held, sellers are paid out, refunds happen, and the books must balance exactly. An external payment provider holds the real money.
About this problem
“Design a payment system with a double-entry ledger” is a hard-level problem in the money and correctness family. Being fast is worthless if the number is wrong, and every mistake is somebody's actual money.
The key challenge: A person is waiting to find out whether their payment worked, and that is the only part with no slack. Everything behind it (capture, ledgers, payouts, reconciliation) has hours, and the design uses those hours to be correct.
Time budgets: Authorisation: seconds. Capture: minutes. Settlement and payout: hours to days.
The worked solution for Payment ledger goes through why those budgets shape the design, stage by stage.
Before you start
You will work this in five timed stages. Here is what each one asks you to think about. None of it is the answer; the answers unlock in the workspace below once you have written your own.
- Stage 1 · Requirements · 5 min
- Are you holding the money, or is a provider? That changes most of the design.
- One currency or many? Multi-currency is a whole extra system.
- What is the worst acceptable outcome — losing money, or refusing a valid payment?
- Stage 2 · Actors and lifecycle · 6 min
- The provider is an actor with its own outages and its own opinions.
- What does the system decide silently on everyone's behalf?
- What happens when you call the provider and never get a reply?
- Stage 3 · Numbers · 6 min
- Payment volume is small compared to almost anything else. Say so.
- How many ledger rows does one payment create?
- What does one incorrect transaction cost to resolve?
- Stage 4 · High-level design · 12 min
- What does a double-entry ledger look like as a table?
- How does one action change our database and the provider together?
- What runs nightly, and what does it compare?
- Stage 5 · Deep dive and tradeoffs · 14 min
- The provider call timed out. Walk through what happens next.
- Reconciliation finds 14 mismatches this morning. What now?
- How do you deploy a change to this safely?
Work it through
Five stages, gated. You write your answer for each stage before its model answer unlocks. That is the core of the method.
Done all five? The diagrams, numbers, trade-offs and follow-ups for this problem are on the worked solution page, to check your own design against.
How it is really built
Write your own answer first. Then read how teams who run this at scale describe it. Their constraints were real, and the write-ups often say what their choices cost.
- Stripe — Idempotent requests ↗How a production payments API stops you charging someone twice.
- PostgreSQL — Transaction Isolation ↗The isolation guarantees a ledger depends on.