Question breakdowns
Design a payments ledger
A ledger is the one system where "eventually consistent" and "approximately right" are both unacceptable. Double-entry, immutability and idempotency are not best practices here; they are the design.
“Design a payments ledger” — or its cousins, “design a wallet”, “design the money movement behind a marketplace” — tests something different from most questions. Throughput matters less than in a feed or a chat app. What matters is that every rupee is accounted for, forever, and the books always balance, even when requests are retried, providers time out and services crash halfway.
Candidates who treat it like any CRUD service — a balance column, UPDATE ... SET balance = balance - 100 — are the ones who get asked “and how would you know if that was ever wrong?”
Requirements
Functional
- Record money movements between accounts: customer wallets, merchant balances, the platform’s fee account, and external money (cards, UPI, bank payouts).
- Show any account’s current balance and its full history.
- Support the flows: a customer pays a merchant, the platform takes a fee, merchants are paid out, refunds and chargebacks reverse money.
- Reconcile against the payment provider’s and bank’s records.
Non-functional
- Correctness above all: no money created or destroyed; every movement traceable.
- Immutability and auditability: history is never edited; corrections are new entries.
- Idempotency: retries never move money twice.
- Strong consistency for balances used to authorise spending (no overdrafts on wallets).
- Durability: an acknowledged entry is never lost.
- Scale: say 5,000 payments/s at peak — modest compared with a feed, which is why correctness can win every trade-off.
State the priority order early: correctness, then auditability, then availability, then latency. A ledger that refuses a write during an incident is an inconvenience. A ledger that accepts a wrong one is a regulatory event. Every later trade-off in the design follows from that order.
Back-of-the-envelope
- 5,000 payments/s at peak; each payment produces ~3–4 ledger entries (customer, merchant, fee, provider) → ~20,000 entry rows/s.
- ~200 bytes per entry → 4 MB/s, ~350 GB/day at sustained peak; far less on average. Years of history is tens of TB — fine for a partitioned relational database plus archival.
- Balance reads: every payment checks a balance, plus merchants refreshing dashboards — reads are a small multiple of writes here, not 100×.
The core idea: double-entry
Every movement of money is a transaction made of entries that sum to zero. Money is never “added” to an account; it moves from one account to another.
A customer pays ₹1,000 for an order; the platform keeps a ₹50 fee:
| Account | Entry |
|---|---|
| Provider clearing (money arriving from the card network) | −1,000 |
| Merchant balance | +950 |
| Platform fee revenue | +50 |
| Sum | 0 |
The invariant “every transaction sums to zero” is checkable at any time, over any range. If it is ever false, there is a bug, and you know exactly which transaction.
API
POST /v1/transactions
Idempotency-Key: pay_ord_812_capture
{
"type": "payment_capture",
"entries": [
{"account": "provider:razorpay:clearing", "amount": -100000, "currency": "INR"},
{"account": "merchant:m_42:available", "amount": 95000, "currency": "INR"},
{"account": "platform:fees", "amount": 5000, "currency": "INR"}
],
"metadata": {"order_id": "ord_812", "provider_payment_id": "pay_Nx81..."}
}
→ 201 {transaction_id, posted_at} | 409 duplicate key, different body | 422 entries don't sum to zero
GET /v1/accounts/{id}/balance → {available, pending, as_of}
GET /v1/accounts/{id}/entries?cursor= → paginated history
Amounts are integers in the smallest unit (paise), never floats. The ledger API is low-level and generic; the “payment”, “refund” and “payout” logic lives in services that call it.
High-level design
Checkout ──▶ Payment service ──▶ Provider (Razorpay/Stripe)
│ ▲ │
│ └── webhooks ◀─────┘
▼
Ledger service ──▶ Postgres (accounts, transactions, entries — append-only)
│
└──▶ outbox ──▶ Kafka ──▶ balances cache, analytics, notifications
Reconciliation job ◀── provider settlement files, bank statements
The members’ section covers the data model, how balances are computed without race conditions, idempotency across the provider boundary, holds and refunds, reconciliation, and scaling.
The rest of this lesson is ₹1
The append-only data model, race-free balance checks, idempotency across the provider boundary, pending vs available, refunds and corrections, reconciliation, sharding a ledger, and the follow-ups.
PhonePe, Google Pay, Paytm or any UPI app. Cards and net banking work too. One payment unlocks this lesson on this device.