System DesignSep 16, 2026⏱️ 3 min read
Design a Payment System: Double-Entry Ledgers, Idempotency Keys & Reconciliation

Design a Payment System: Double-Entry Ledgers, Idempotency Keys & Reconciliation

The Golden Rule of FinTech Architecture: Zero Silent Money Loss

In social media, dropping one status update out of a million is a minor UI glitch. In a payment system processing $1M+ per day across Stripe, Adyen, or UPI/bank rails, losing or double-charging even 0.001% of transactions triggers regulatory audits and destroyed customer trust.
Scalable Payment System Architecture

Scalable Payment System Architecture


1. Pay-In Flow vs. Pay-Out Flow

Never process credit cards directly inside your primary database servers (huge PCI-DSS compliance scope!). Delegate card vaulting and 3DS challenges to a Payment Service Provider (PSP):
  1. Pay-In (Buyer → Platform):
    • User clicks Pay → API creates a PaymentEvent (NOT_STARTED) → Payment Executor calls PSP (Stripe/Adyen) using a Hosted Card Token → PSP confirms → Executor records completion in Ledger + Wallet asynchronously via Kafka!
  2. Pay-Out (Platform → Seller):
    • Triggered on scheduled settlement windows or payouts, routing funds via bank transfer / PSP payout APIs after passing fraud/risk checks.

2. Double-Entry Ledger: Accounting That Always Balances

Never update a single balance = balance - 50 column in place! Store an immutable append-only Double-Entry Ledger where every transaction writes balanced opposing rows whose sum must equal zero:
AccountDebitCreditEntry Meaning
Buyer Clearing Account$50.00—Buyer owes/pays $50
Seller Merchant Account—$48.50Seller receives $48.50
Platform Fee Revenue—$1.50Platform takes $1.50 fee
Every transaction satisfies $\sum \text{Debits} = \sum \text{Credits}$, making financial tampering, rounding bugs, or orphaned transfers mathematically impossible to hide!

3. Exactly-Once Execution: Idempotency Keys & Retries

Mobile networks drop right after the user taps "Pay Now", causing retries.
  • Idempotency Header: Client generates a unique UUIDv4 key per checkout session (Idempotency-Key: 9f8a-...) and sends it to the Payment Service.
  • Atomic Lock + Cached Result: Server stores idempotency_key in a UNIQUE indexed SQL/Redis table:
    • First call inserts IN_PROGRESS (returns 409 Conflict if another thread races concurrently!), completes PSP charge, saves response (200 OK), and forwards the exact same idempotency_key to the PSP!
    • Retries immediately return the saved response without hitting the card twice.
  • Transient Network Failures: Retry failed internal steps via Kafka + Dead-Letter Queues (DLQ) with exponential backoff.

4. The Last Line of Defense: Automated Reconciliation

Even with idempotency and queues, external bank/PSP webhooks can drop or go out of order.
  • Every night, each PSP generates an immutable Settlement File listing every cleared charge and refund.
  • The Reconciliation Service compares the PSP Settlement File line-by-line against your internal Double-Entry Ledger—automatically flagging discrepancies (PSP captured money, internal DB missed webhook → auto-heal or route to Finance Ops!).

Share this article

Comments