skip to content
jaeaeichsysdes

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

CHECKOUT & PSP (OUTBOUND IDEMPOTENCY)DOUBLE-ENTRY LEDGER (INBOUND IDEMPOTENCY)TWO-TIER RECONCILIATIONpaypendingcharge (idempotency_key=order_id)webhookWAL CDCeventsACID txpoll pendingdiff booksdaily CSValertBuyerGateway L710K TPSorder svcPOST /completeidem_key=order_idOrders PGpending -> completeWAL CDC outboxExternal PSPStripe / Adyenwebhook + CSVKafkaat-least-onceledger svcnet-zero entriesUNIQUE(tx, acct)Ledger PG (ACID)append-only + O(1) balreplica -> analyticspayment reconpolls pending >5mheals lost webhooksledger recondaily PSP CSV diffpenny-exact auditAlert ManagerPagerDuty / opshalts payouts

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 confirmation
Analytics: GET /accounts/:id/balance -> O(1) balance from Ledger PG / read replica

How 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 along pending -> complete | failed (never DELETE failed attempts).
  • Outbound idempotency: Pass idempotency_key = order_id to the PSP on /complete so 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: Each tx_id inserts immutable rows summing to 0 (e.g. psp_clearing -100, seller +95, platform_fee +5; refunds append reversing rows) and atomically updates accounts(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 recon polls orders stuck in pending > 5m against the PSP to heal dropped or failed webhooks.
  • Tier 2 (daily audit): ledger recon diffs the PSP’s daily settlement CSV against Ledger PG and fires Alert Manager on 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?

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?

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?

esc
⌘B sidebar[ ] prev/next1–5 flowsc copyt theme