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
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 --> EXTMoving money
Wallet to merchant (internal):
- Look up the idempotency key; if it exists, return the stored result.
- 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 paymentSUCCEEDED; write an outbox event. - 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