System design case study C12 · Ledger · Idempotency

Design a Payment System (UPI-style checkout)

Money systems have one non-negotiable rule: every rupee must be accounted for, exactly once, even when networks time out and banks reply late.

What you'll be able to explain
  • Model payments as a state machine with idempotent APIs
  • Record money movement in a double-entry ledger
  • Handle asynchronous bank and UPI responses with webhooks and polling
  • Reconcile against bank settlement files
  • Meet security and compliance requirements

Scope

Functional

  • Merchants create payment orders; customers pay via UPI (collect, intent/QR), cards, netbanking.
  • Status updates to merchants (API + webhooks); refunds; daily settlements to merchants.

Non-functional

  • No double charges, no lost payments: correctness over availability.
  • Auditability: every state change traceable.
  • p99 < 300 ms for our own APIs (bank latency is outside our control).
  • Compliance: PCI-DSS for cards, RBI data localisation.

Size

  • 20M transactions/day → ~230/s average; festival sales peak at 5–10k/s.
  • Each transaction generates ~5–10 ledger entries and several events.

APIs

POST /v1/orders     {amount, currency, merchant_ref}          Idempotency-Key: ...
POST /v1/payments   {order_id, method: upi_intent|card|...}  → {payment_id, action: {upi_uri | redirect}}
GET  /v1/payments/{id}
POST /v1/refunds    {payment_id, amount}                      Idempotency-Key: ...
Webhooks → merchant: payment.captured, payment.failed, refund.processed

The architecture

CLIENTMerchant / appSERVICEPayments APIidempotencyDATABASEPayment stateSQLSERVICEOrchestratorEXTERNALProvider adaptersUPI / card / bankWORKERCallbacks+ status pollerDATABASELedgerdouble-entryQUEUE / LOGOutbox → eventsWORKERWebhook senderWORKERReconciliationsettlement filesinitiateasync resultpost entriesverify
Payment platform: orchestration, provider adapters, ledger, and reconciliation.

The 30-second summary

Say it in the interview

"Payments are a strict state machine in a relational store, with idempotency keys on every inbound API, stable references on every outbound call, and deduplicated callbacks. Timeouts leave payments PENDING until a callback or status poll resolves them, never assumed failed. Money movements are recorded in an append-only double-entry ledger, events go out through an outbox to signed, retried webhooks, and daily reconciliation against bank files catches anything that slipped."

Follow-up questions the interviewer may ask

  1. How do you guarantee a customer is charged once?
  2. The gateway times out. Did the payment happen?
  3. How do you design the ledger?
  4. How do you handle refunds?

Model answers to each of these are in the book.