Skip to main content
← PROBLEMS / SYSTEM-DESIGN
An end-to-end domain learning journey

Design a Double-Entry Payment
& Ledger System.

Design a payment and ledger system (like Stripe or PayPal) processing 5,000 TPS where no dollar can ever be double-charged, lost, or created out of thin air—even across network retries and PSP timeouts.
Start the journey2 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When a single bottleneck threatens end-to-end availability, zero double-charges, zero lost money, and full cryptographic/accounting auditability.

TL;DR30-second simplified summary
▾

The design partitions state, bounds tail latency, and isolates failure domains.

Zero double-charges, zero lost money, and full cryptographic/accounting auditability.

Full walkthrough · 2 stages
01
SYSTEM-DESIGN · STAGE 01L4 · FUNDAMENTALS

Clarify invariants, idempotency & scale.

Every payment state transition must be backed by immutable double-entry ledger rows (debit + credit summing to 0) inside ACID transactions. Clients send a UUID Idempotency-Key so mobile network retries never charge a card twice.

ChoiceWhat it buysWhat it costs
SQL ACID Ledger vs. NoSQLMulti-row atomic debit+credit in one transactionRequires shard-by-account-group past ~10K TPS

Insight 1

Never mutate a single balance column without an append-only ledger_entries audit trail.

Insight 2

Store idempotency_key with a unique constraint in PostgreSQL so duplicate retries return the original response.

02
SYSTEM-DESIGN · STAGE 02L5 · APPLIED DESIGN

Payment execution, PSP calls & Outbox.

Never hold a database transaction open while calling an external Payment Service Provider (Visa/Stripe). Instead, record PENDING locally, release the DB connection, call the PSP, and commit SETTLED + an outbox event for Kafka via the Transactional Outbox.

Insight 1

Lock accounts in sorted account_id order (SELECT ... FOR UPDATE) to eliminate deadlocks.

Insight 2

Run nightly three-way reconciliation comparing internal ledger, PSP settlement files, and bank statements.

PSP Timeout Ambiguity

PSP charges the card, but the HTTP response times out before reaching your server.

Put it together · Synthesis

Put the Payment Ledger together

Combine idempotency keys at the API edge, immutable double-entry rows in [SQL](/domains/sql), out-of-transaction PSP calls, and CDC outbox publishing to [Kafka](/domains/kafka).

A candidate architecture, not a universal answer

Reference Topology & Execution Flow

Step 1
Step 2
Step 3
  1. Validate Idempotency-Key before touching balances.
  2. Write immutable debit/credit pairs inside a short ACID transaction with sorted row locks.
  3. Publish settlement events via Transactional Outbox + CDC.
Zero double-charges, zero lost money, and full cryptographic/accounting auditability.
Interview Readiness Check

Zero double-charges, zero lost money, and full cryptographic/accounting auditability.

Walk through bottlenecks, back-of-the-envelope math, and failure recovery.

Practice · Explain the consequences

Practice: Make the trade-off yourself.

Practice Check

Test your understanding

Question 1 of 2

A user edits their bio and refreshes the page, only to see the old bio for 2 seconds because the read hit a lagging replica. Which consistency guarantee was violated?

Recommended Learning Flow