Design a payment processing system
A payment system (Stripe) moves money correctly, exactly once, with a complete audit trail. Correctness beats latency: the design centers on idempotency, a double-entry ledger, and resilient integration with external processors.
Storage & Data, 12 components, about 45 min
1. Scope
In scope
- • Charge a card
- • Idempotent retries
- • Double-entry ledger
- • Webhooks
- • Reconciliation
Out of scope
- • PCI/tokenization internals
- • Fraud ML deep-dive
- • Multi-currency FX
2. Functional requirements
Must have
- • Process a payment exactly once
- • Never lose or duplicate money
- • Complete audit trail
- • Handle processor failures gracefully
Nice to have
- • Refunds
- • Payouts
- • Subscription billing
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • 10K payments/sec peak
- • Each payment = several ledger entries
- • Idempotency keys cached 24h
- • Retention 7 years
Derived numbers
5. API design
Charge with Idempotency-Key header → { chargeId, status }
Refund a charge (also idempotent)
Async event to merchant (at-least-once)
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
Idempotency
Networks retry. The client sends an Idempotency-Key; the first request does the work and stores the result keyed by it; retries with the same key return the stored result instead of charging again. Write the key into the ledger in the same transaction as the charge too, so losing the Redis cache can never cause a double charge. This makes "charge once" safe even when the client can't tell whether the first attempt succeeded.
Double-entry ledger
Every movement is recorded as balanced debit + credit entries, so the books always sum to zero and you can audit any balance. Use SERIALIZABLE isolation (or careful row locking) so concurrent transactions can't corrupt balances. The ledger is append-only — corrections are new compensating entries, never edits.
Handling processor failures
External processor calls can time out ambiguously (did it go through?). Wrap them in a circuit breaker and, on uncertainty, mark the charge "pending" rather than "failed", then reconcile against the processor's settlement file. Never assume failure and double-charge.
Reliable webhooks (outbox)
To notify merchants reliably, write the event to an outbox table in the same DB transaction as the ledger write, then a relay reads the outbox and delivers (with retries) at-least-once. Consumers must be idempotent. This avoids the dual-write problem of updating the DB and a queue separately.
9. Bottlenecks
- Ledger write contention → partition by account, careful locking
- Processor latency/outages → circuit breaker + pending state
- Exactly-once delivery → outbox + idempotent consumers
10. Trade-offs
Ledger design
Choose: Double-entry, append-only ledger: every movement is balanced debit and credit entries; balances are derived.
Instead of: Mutable balance column: simple, but no audit trail and race-prone.
Money systems must explain every cent; append-only entries make reconciliation and audit possible.
Synchronous or asynchronous capture
Choose: Authorize synchronously in the checkout request; capture, settlement and payouts asynchronously.
Instead of: Everything synchronous: longer checkout latency and tighter coupling to banks.
Only authorization needs the customer waiting; the rest tolerates minutes or days.
Retries to card networks
Choose: Retry only with the same idempotency key, and on timeouts query status before retrying.
Instead of: Blind retries: double charges.
A timeout does not mean failure; the charge may have succeeded.
11. Failure modes
Timeout from the acquirer after sending an authorization
Unknown state: charged or not.
Mitigation: Mark the payment pending, query the acquirer, and let reconciliation settle it; never auto-retry a new charge.
Webhook lost
Order never marked paid.
Mitigation: Webhooks are idempotent and retried by the sender; a poller re-checks pending payments older than N minutes.
Ledger and processor disagree
Books do not balance.
Mitigation: Daily reconciliation against settlement files; mismatches go to a breaks queue with alerting.
12. What interviewers probe
Answer each one yourself before opening it.
How exactly does the idempotency key work?Show a strong answer
The client sends a key per logical payment. The server stores (key, request hash, response) atomically with the first attempt; repeats return the stored response, and a different body with the same key is rejected.
Why not store balances directly?Show a strong answer
Concurrent updates race, and a wrong balance has no history. Derive balances from entries, optionally cached with the last entry id for fast reads.
How do you stay out of PCI scope?Show a strong answer
Card data goes straight from the client to a tokenization vault or the processor's hosted fields; your services only see tokens.
How do you handle currencies?Show a strong answer
Store integer minor units with an ISO currency code; never floats. FX conversion is its own ledger entry with the rate recorded.
Sign in to track this guide in your curriculum progress.