LOAD-BEARINGfree
Architecture labTier 13 · unit 7 of 31

Payment ledger

Move money between accounts. Every movement must be auditable, balanced, and impossible to duplicate — including when the network fails mid-request and the client retries.

EnvelopeCorrectness over throughput · full audit trail · zero tolerance for lost money

What the design must do

  • Every transfer recorded as balanced double entries
  • Retried requests must not charge twice
  • Ledger is append-only, never updated in place
  • External settlement handled asynchronously

The worked envelope

Assumed. 10M transactions/day, 50M accounts, double-entry so two journal rows per transaction, nothing ever deleted.

Worked envelope
Transactions10M/day ≈ 116/s average, peak ~600/s
Journal writes2 entries each → 1,200 rows/s at peak
Row sizeaccounts, amount, currency, references ≈ 200 B
Journal growth20M rows × 200 B = 4 GB/day = 1.5 TB/yr, forever
Balances50M accounts × 100 B = 5 GB — fits in memory
Naive balance readfolding a year of entries per query: impossible

What the numbers decide. Six hundred writes a second is nothing. A single well-tuned Postgres instance handles it without breathing hard, and it will still handle it after ten years of growth. This lab is the one where the envelope tells you that scale is not the problem — correctness is — and the honest design is therefore the boring single-writer one.

The only number that does force a mechanism is the last pair: the journal is unbounded and append-only, so deriving a balance by summing history gets slower every day forever. That is what checkpoints are for, and it is the one piece of machinery this system genuinely needs.

The decisions, and what each one costs

Append-only double-entry journal as the source of truth

Because
Money requires an audit trail that can be re-derived and independently checked; an updatable balance column destroys the evidence of how it got there
The cost
Balances must be derived rather than read, and the table grows forever at 1.5 TB/yr
Rejected
A mutable balance with an audit log alongside — two sources of truth that will eventually disagree, with no way to tell which is right

Periodic balance checkpoints, with reads folding only the tail

Because
A fold over a year of entries is unbounded work per read; a nightly checkpoint bounds it to one day of rows
The cost
A second structure to maintain and reconcile, and a checkpoint bug is silent until someone recomputes from zero
Rejected
Caching balances in Redis as the source of truth, which is the mutable-balance mistake with a shorter fuse

Single-writer relational store, no sharding

Because
600 writes/s is comfortably within one instance, and the invariant "debits equal credits" is a multi-row transaction that sharding would break
The cost
A write ceiling and a visible failover; growth eventually needs partitioning by account range
Rejected
Sharding for headroom, which trades the ACID transaction this system is entirely built on for capacity it does not need

Idempotency keys on every money-moving endpoint, stored with the result

Because
A timeout is an unknown, not a failure, and the retry that follows is indistinguishable from a second genuine payment
The cost
A key store to size and expire, and a fingerprint check so a reused key with a different amount is refused rather than replayed
Rejected
Relying on the database transaction, which makes the first charge atomic and does nothing about the second request

Components in the reference design

Client
Browser or mobile app
Load Balancer
L7 routing, TLS termination, health checks
API Gateway
Auth, routing, quota enforcement
App Service
Stateless business logic
SQL DB
Relational, ACID, joins
Queue
Work distribution, one consumer per message
Worker
Async consumer, background jobs

What not to draw

  • NoSQL Store — The core ledger needs multi-row ACID transactions so a debit and its matching credit commit together. That is exactly what a relational engine is for.

The reference design

Double-entry, append-only. Every transfer writes at least two rows: a debit from one account and a credit to another, in the same transaction, summing to zero. Nothing is ever updated in place. If money seems to have gone missing, the journal shows exactly what happened and when — which is the entire point, and why a mutable balance column is the wrong data model.

Balances. Summing an account's entire history on every read gets slow. Keep periodic checkpoint rows — balance as of a timestamp — and sum only the entries after the latest checkpoint. Derived, verifiable, fast.

Idempotency is not optional. The client generates a key per logical payment. The server stores key → result and returns the stored result on any repeat. Enforce it with a unique index on the key so that even a race between two concurrent retries can only produce one entry — the database rejects the second insert.

Concurrency. Two simultaneous withdrawals from the same account must not both pass the balance check. Either use a conditional atomic update, or lock the account row with SELECT ... FOR UPDATE, ordering locks consistently by account ID to avoid deadlocks in transfers.

External settlement is a saga. Your ledger commits locally, then an outbox row drives an async call to the card network or bank. That call can fail, time out, or succeed-then-time-out. The payment sits in a pending state until confirmed, and every failure path needs a compensating entry — a reversal, never a deletion.

Reconciliation. A scheduled job compares your ledger against the provider's statement and flags mismatches. Assume drift will happen; the design goal is that it is detected the same day, not that it is impossible.