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.
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.
| Choice | What it buys | What it costs |
|---|---|---|
| SQL ACID Ledger vs. NoSQL | Multi-row atomic debit+credit in one transaction | Requires 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.
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 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).
Reference Topology & Execution Flow
- Validate
Idempotency-Keybefore touching balances. - Write immutable debit/credit pairs inside a short ACID transaction with sorted row locks.
- Publish settlement events via Transactional Outbox + CDC.
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: Make the trade-off yourself.
Practice Check
Test your understanding
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?