Design a Payment System (UPI-style)
Design a payment flow that never loses or double-counts money: idempotency, a double-entry ledger, state machines, reconciliation and PSP or UPI integration.
Advanced · about 6 hours · difficulty 5/5
Learn first: Idempotency, Retries and Timeouts, Distributed Transactions: 2PC and Sagas, Security Basics for System Design
Payments are the case study where correctness beats everything. A slow feed is annoying; a double debit is a support ticket, a refund and a regulatory problem.
Context: how UPI flows work
In UPI, the payer's app (a PSP such as PhonePe or Google Pay) sends a request through NPCI, which routes it to the payer's bank to debit and the payee's bank to credit. Every transaction carries a unique transaction reference. A merchant usually integrates through a payment gateway (Razorpay, PayU, Cashfree) rather than talking to NPCI directly. Responses can be pending or delayed, so the design must handle uncertainty.
Core components (merchant or gateway side)
- Payment service: creates a payment intent with an idempotency key, calls the external PSP or bank, tracks state.
- Payment state machine: CREATED, PENDING, SUCCESS, FAILED, REFUNDED. Transitions are explicit and only move forward.
- Ledger service: a double-entry ledger, where every movement is two entries (debit one account, credit another) that sum to zero. Entries are append-only; corrections are new entries, never edits.
- Wallet or balance views derived from the ledger.
- Webhook and status poller: providers notify asynchronously; if no callback arrives, poll the status API.
- Reconciliation job: compare your records with bank and gateway settlement files daily and flag mismatches.
Correctness rules
- Idempotency at every hop: client to API, API to provider (use the provider's idempotency or reference IDs), and webhook processing.
- Strong consistency (ACID, serialisable or careful locking) for balances; a relational database is the usual choice.
- Never mark SUCCESS without confirmation; treat timeouts as unknown, not failed, and resolve them by status checks.
- Store money as integers in paise; record currency explicitly.
- Outbox pattern for events like PaymentSucceeded so order fulfilment is never missed.
Non-functional
Audit logs, PCI DSS for card data (tokenise, never store CVV), RBI data localisation, encryption, fraud and velocity checks, and high availability with careful failover.
Pitfalls
- Retrying a debit after a timeout without an idempotency key.
- Mutable balance columns updated in place with no ledger.
- Skipping reconciliation and discovering mismatches only through customer complaints.
You are ready to move on when you can walk through a payment that times out, then succeeds via webhook ten minutes later, and show that the user is charged once and the order is fulfilled once.
Key concepts
- UPI and PSP flow basics
- Payment intent and idempotency
- Payment state machine
- Double-entry ledger
- Handling pending and unknown states
- Webhooks and status polling
- Reconciliation
- PCI DSS and data localisation
Mini project
Write a design doc and build a mock payment system: a payment API with idempotency keys, a fake PSP that randomly times out or answers late via webhook, a double-entry ledger in Postgres and a reconciliation script that compares your ledger with the PSP's settlement file.
Check yourself
A call to the bank times out after 30 seconds. Why must you not mark the payment as failed and retry immediately, and what should you do instead?
Free resources
- NPCI (UPI operator) (NPCI)
- Idempotent requests (Stripe)
- Razorpay engineering blog (Razorpay)
- Zerodha tech blog (Zerodha)