Design a Payment System & Ledger (Stripe)
Exactly-once checkout and auditable double-entry accounting at 10K TPS: outbound PSP idempotency keys, Postgres WAL CDC transactional outbox, append-only net-zero ledger with O(1) balances, and two-tier reconciliation.
The whiteboard
Trace checkout & PSP, double-entry ledger, and reconciliation below — applying Idempotency & Outbox and Concurrency & Failure Modes.
whiteboard · press 1–4 to trace flows
Two-layer idempotency + zero dual-writes: outbound PSP idempotency key, WAL CDC outbox to Kafka, append-only net-zero ACID ledger, and two-tier reconciliation.Consistency beats availability in money movement — never double-charge a buyer, never double-credit a seller, never DELETE ledger rows.
Scale & APIs
10K TPS
peak payment write throughput
sum = 0
double-entry invariant per tx_id
2-tier
real-time poller + daily CSV recon
Constraints: Consistency >> Availability · strict ACID · append-only audit trail · zero dual-writes
Checkout: POST /book/order -> { order_id } (UUID, status=pending) POST /book/order/{id}/complete -> charge PSP (idempotency_key=order_id)PSP Callback: POST /webhook/psp -> async idempotent confirmationAnalytics: GET /accounts/:id/balance -> O(1) balance from Ledger PG / read replicaHow it works
1. Idempotent checkout (order svc -> Orders PG + External PSP)
- Two-step order creation: Persist
(order_id, user_id, seller_id, cost, status = pending)before touching money, and transition strictly alongpending -> complete | failed(neverDELETEfailed attempts). - Outbound idempotency: Pass
idempotency_key = order_idto the PSP on/completeso double-clicks or timeout retries return the original result instead of charging the card twice.
2. Atomic event publishing (Orders PG WAL -> CDC -> Kafka)
- Transactional outbox pattern: Tail Postgres’s WAL via CDC (e.g., Debezium) into Kafka so a crash can never leave a paid order without a ledger event or a ghost event for a rolled-back order.
3. Double-entry ACID ledger (Kafka -> ledger svc -> Ledger PG)
- ACID Postgres over ClickHouse: OLAP stores lack multi-row ACID transactions and strict uniqueness; keep the source of truth in Postgres and serve heavy dashboards from a read replica or async OLAP sync.
- Net-zero +
O(1)balance: Eachtx_idinserts immutable rows summing to0(e.g.psp_clearing -100,seller +95,platform_fee +5; refunds append reversing rows) and atomically updatesaccounts(account_id, balance)in the same tx. - Inbound idempotency: Enforce
UNIQUE(tx_id, account_id)in Postgres so at-least-once Kafka redelivery fails harmlessly instead of double-crediting a seller.
4. Two-tier reconciliation (payment recon + ledger recon)
- Tier 1 (real-time):
payment reconpolls orders stuck inpending > 5magainst the PSP to heal dropped or failed webhooks. - Tier 2 (daily audit):
ledger recondiffs the PSP’s daily settlement CSV againstLedger PGand firesAlert Manageron any penny discrepancy so ops can halt payouts.
lesson
Two layers of idempotency is the line interviewers listen for: outbound idempotency_key = order_id protects the buyer’s card at the PSP on retries, while inbound UNIQUE(tx_id, account_id) protects the seller’s balance in Postgres on Kafka redelivery.
Check yourself
interactive · stress-test this payment design against classic Stripe/fintech probes
01 · Why pair PSP webhooks with a reconciliation poller?
dropped webhooks · deploy restarts · pending > 5m
Your PSP already sends POST /webhook/psp whenever a charge succeeds or fails. Why build a separate payment status recon worker that polls the PSP for orders stuck in pending?
Select an option to check the trade-off.
02 · Why use ACID Postgres instead of ClickHouse for the ledger?
net-zero tx · UNIQUE(tx_id, account_id) · O(1) balances
An engineer suggests writing double-entry rows straight into ClickHouse so analytics queries are fast. Why must the ledger live in ACID Postgres instead?
Select an option to check the trade-off.
03 · Why CDC from Postgres WAL instead of publishing to Kafka in order svc?
dual-write bug · process crash · 10K TPS
When order svc marks an order complete in Postgres, why let a CDC connector tail the WAL to publish to Kafka instead of calling kafka.produce() right after the SQL UPDATE?
Select an option to check the trade-off.
idempotencyreliability
Property where repeating the exact same request multiple times produces the same final state as executing it once.
outbox patternmessaging
Writing a domain change and its outgoing event to the same DB transaction, then relaying the event asynchronously to avoid dual writes.
WALstorage
Write-Ahead Log: an append-only disk log written before mutating in-memory structures so crashes never lose committed writes.