India-scale design · senior
UPI payments, failed debits and a stock broker
Money systems never guess. Use idempotency keys, forward-only states, a double-entry ledger and reconciliation. A timeout is an unknown result, and the user must never be told to pay again.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- UPI parties: the payer app and its PSP bank, the NPCI UPI switch, and the payee bank. The app never holds the money. We own the app backend, the PSP integration, our records and notifications.
- States: created, pending (sent to the bank), then success, failed or unknown, and later reversed or refunded. States move forward only, stored with a version number.
- Idempotency: the app creates a key per payment attempt; the server stores (key, request hash, result) under a unique constraint, so a retry returns the stored result and the same key with a different body is rejected. The transaction id sent to the bank is created once and reused on every retry.
- Unknown outcome: never send a new debit. Mark it pending, call check-status with backoff, reconcile with bank and NPCI settlement files, and tell the user payment is pending, do not pay again.
- Ledger: double-entry, append-only journal rows (debits and credits sum to zero), balances derived from them. Never a mutable balance column with no journal. Never a distributed transaction across the banks.
- Money debited but the order shows failed: our timeout (10 s) was shorter than the gateway slow answers, we marked the payment failed and cancelled the order, and the late success webhook was rejected by the state check. Fix: a pending or unknown state, outcome only from the gateway (webhook plus status API), reconcile with the gateway report, revive or refund, with idempotent logged scripts.
- Stock broker: the exchange (NSE, BSE) matches orders. The broker runs order management (OMS) and risk checks (RMS: margin, limits), and sends orders over leased lines with a fixed orders-per-second capacity; extra orders queue at the broker.
- Flow: app, gateway, order service (client order id for idempotency), risk check with in-memory margin, a queue per exchange line, exchange gateway; acks and fills return as events. Partition by user so one user orders go in sequence. Never silently drop an order when a line is full; show a queued at broker state.
The red pen
Common traps: mistake → fix
Mistake: Retry a debit with a new transaction id after a timeout
Fix: double debit; reuse the id.
Mistake: Timeout means failure and the user can pay again
Fix: the money may be captured.
Mistake: The broker matches its own users orders
Fix: the exchange matches.
Mistake: Trust the app success callback
Fix: trust the gateway outcome.
Easy to hard
Questions interviewers ask, easy to hard
- Design a UPI payments app backend with idempotency.
- What do you do when the bank times out?
- Money was debited but the order shows failed. How do you repair and prevent it?
- Design a stock broker order system for market open.
- Why a double-entry ledger?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.