← All lessons

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.

26 min readUpdated November 2026Premium

“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

Non-functional

The point

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

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.

Premium

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.