← Back to System Design, page 3

system-design-0054 · Hard senior

Design a payment system with a digital wallet that processes 50 million payments per day without losing or double-charging money.

Model money with a double-entry ledger: every payment writes balanced debit and credit entries in one database transaction, and balances are derived from or checked against those entries. Every external call uses an idempotency key, payment states move through a strict state machine, and a daily reconciliation compares the ledger with bank and card network reports. At 50 million payments per day the rate is only about 580 per second, so correctness, not throughput, drives the design.

Full answer

Requirements

Functional:

  • Users top up a wallet from a card or bank, pay merchants from the wallet or directly, and withdraw.
  • Merchants see settlements; users see a history of transactions.
  • Refunds and chargebacks.

Non-functional:

  • No money is created or lost; every movement is auditable.
  • A retried request never charges twice.
  • High availability for payment acceptance; strong consistency for balances.

Capacity estimates

Assumptions: 50M payments per day, peak factor 5, two ledger entries per payment at about 1 KB each including metadata.

  • Payments: 50M / 86,400 = about 580 per second; about 2,900 per second at peak.
  • Ledger growth: 50M x 2 x 1 KB = 100 GB per day, about 36.5 TB per year before replication.
  • A well-indexed relational database cluster sharded by account handles this; the challenge is correctness and retention (financial records are kept for years).

API

POST /v1/payments Idempotency-Key: 6f1c... {"payer_account": "acc_1", "payee_account": "acc_9", "amount": {"value": 49900, "currency": "INR"}} -> 201 {"payment_id": "pay_77", "status": "SUCCEEDED"} (same response if retried) GET /v1/accounts/{id}/balance GET /v1/accounts/{id}/entries?cursor=.. POST /v1/payments/{id}/refunds Idempotency-Key: ...

Amounts are integers in the smallest unit (paise, cents), never floating point.

Data model

  • accounts: account_id, owner_id, type (user_wallet, merchant, bank_clearing, fees), currency, balance, version.
  • ledger_entries: entry_id, payment_id, account_id, direction (debit or credit), amount, created_at. Insert only; never updated.
  • payments: payment_id, idempotency_key (unique), status, amount, created_at.

The rule: for every payment, total debits equal total credits.

High-level design

mermaid
flowchart LR
  C[Client] --> API[Payment API]
  API --> IK[(Idempotency store)]
  API --> PS[Payment service]
  PS --> L[(Ledger DB)]
  PS --> PSP[Card and bank adapters]
  PSP --> EXT[External networks]
  PS -- "outbox events" --> BUS[(Event bus)]
  BUS --> N[Notifications]
  REC[Reconciliation job] --> L
  REC --> EXT

Moving money

Wallet to merchant (internal):

  1. Look up the idempotency key; if it exists, return the stored result.
  2. In one transaction: lock the payer account row (SELECT ... FOR UPDATE) and check the balance; insert a debit entry for the payer and a credit entry for the merchant; update both balances; mark the payment SUCCEEDED; write an outbox event.
  3. Store the response under the idempotency key.

Card top-up (external): the payment goes CREATED -> PENDING -> SUCCEEDED or FAILED. The call to the card network carries our payment_id as its idempotency key. If the call times out, the status stays PENDING and a worker queries the provider until it gets a final answer. The wallet is credited only after a confirmed success.

Reconciliation

Every day, compare the ledger with settlement files from banks and card networks. Each mismatch (money received but no ledger entry, or the reverse) creates a case for automatic fixing or manual review. Also verify that all ledger entries sum to zero across accounts.

Bottlenecks and trade-offs

  • Hot merchant accounts. A big merchant receives thousands of credits per second; locking one row serialises them. Split the merchant's balance into several sub-accounts, or credit merchants asynchronously from a queue, since merchants do not need real-time balances.
  • Cross-shard transfers. If payer and payee are on different shards, use a saga with a clearing account: debit the payer into clearing, then credit the payee from clearing, each step idempotent.
  • Timeouts are not failures. Treating an unknown result as failed and retrying with a new key is the classic double-charge bug.
  • Strong consistency is chosen over availability for balance updates; accepting a payment request can still be queued during a short outage.

By level

Fresher: Explain why money is stored as integers and why each payment needs a unique key so retries are safe. A fresher should describe a simple transfer inside one database transaction.

Mid: Design the double-entry ledger, the payment state machine and idempotency at both the API and the provider. Do the capacity math and note that it is small.

Senior: Cover reconciliation, hot merchant accounts, cross-shard sagas through clearing accounts, retention and audit, and the consistency versus availability choice. Expect the timeout double-charge trap to be named.

Key points

  • Double-entry ledger: debits equal credits for every payment
  • Integer amounts in the smallest currency unit
  • Idempotency keys on every API and external call
  • Unknown results stay pending until confirmed
  • Daily reconciliation against external reports

Common mistakes

  • Storing only a balance column with no entries
  • Using floating point for money
  • Retrying a timed-out card charge with a new idempotency key
  • Ignoring hot merchant accounts that serialise on one row lock

Follow-ups

  • How would you handle currency conversion?
  • How do refunds and chargebacks appear in the ledger?
  • How would you move money between shards?

Tips

  • Start with the ledger invariant
  • Explain the pending state for timeouts
  • Point out that throughput is small and correctness is the focus

Evaluation rubric

  • Double-entry ledger with append-only entries
  • Idempotency at API and provider level
  • Correct handling of timeouts and reconciliation
  • Addresses hot accounts and cross-shard transfers