Case study · Banking & fintech · complexity 4/5
How a bank or fintech records money so it is never created, lost or spent twice — from one Postgres database to a partitioned ledger with strict disaster recovery
This is an educational reference architecture, not the design of any one bank. Where we cite a company's own engineering material, it is marked as documented.
Move ₹500 with two UPDATEs and no transaction. If the app crashes between them, Asha loses ₹500, Ben never gets it, and no record says why.
Architecture evolution
One PostgreSQL database holds accounts, journal entries and postings. Each transfer is one database transaction that claims an idempotency key, locks both accounts in id order, writes a balanced entry and commits.
Components
What breaks next
Correctness, not load. A retry can post a transfer twice, two debits can race on one balance, and a single database with backups means losing the last hours of work if it dies.
App server
transfer code
Database
accounts table
Write-ahead log
on disk
Swipe the diagram sideways to see every column →
Where the time goes
8 ms total
Move ₹500 from Asha (balance ₹2,000) to Ben (balance ₹1,000). The app server crashes after the first UPDATE, restarts, and retries the transfer. Timings are an illustrative model.
Illustrative timings. Real numbers vary with distance, network quality and server load.
A ledger is the record of every movement of money, and double-entry bookkeeping, where each movement is written as equal debits and credits, is centuries older than computers. Stripe, Square and Modern Treasury have described in public how they build such ledgers in software, and they follow the same core rules.
Three documented ledgers, the same core rules
A ledger has one job that ordinary apps can fudge: the total must always add up, through crashes, retries, races, partners that disagree and the loss of a whole data center. Each of those threats breaks a naive design in a different way.
Six ways a naive ledger loses track of money
Every movement of money is a journal entry made of postings that sum to zero, and an account's balance is the sum of its postings. Entries are only ever added; a mistake is undone by a reversing entry, never by an edit.
A transfer and its reversal, both kept forever
A startup's ledger can be one PostgreSQL database and one transfer function, where each transfer is a single database transaction that claims the idempotency key, locks both accounts, writes a balanced entry and commits. This is our educational design, not any company's documented system.
Stage 1: one database, one careful function
The bottleneck moves from correctness on one database, to reads, partners and batch jobs competing with transfers, and finally to one database that cannot hold every account or survive a site loss. The traffic numbers are illustrative.
The bottleneck moves at each stage
Much of a bank's money is not settled yet: a card hold reserves funds before the merchant captures them, interest accrues every night, and outside networks settle hours or days later. A ledger models each of these explicitly and checks them against the outside world.
A card hold lowers available, not posted, balance
Three records of the same money must agree
Reads scale with replicas and statement read models, writes scale by splitting accounts across ledger shards, and surviving a site loss takes synchronous replication. Each step keeps the one rule: a transfer is recorded exactly once, balanced.
Writes and checks on the primary, reading elsewhere
A cross-shard transfer through a clearing account
The rules a modern ledger follows were written down long before the software. Merchants' double entry reached print in 1494, software patterns for it were described in the 2000s, and payment companies documented production ledgers from 2017 on.
From the method of Venice to five billion events a day
At bank scale, the money path is short and strict (gateway, fraud check, transfer coordinator, ledger shards with synchronous standbys), and everything that only reads hangs off an event stream. This is a typical educational design, not a diagram of any company's system.
A strict money path and relaxed read paths
Every protection in a ledger costs latency, throughput or complexity: locks queue transfers, synchronous replication slows every commit, inline fraud checks add delay, and read models add a second copy to keep right. The dials are set per account type and per payment path, not once for the whole bank.
Four dials for a ledger
On a bad day, five things typically go wrong: a client retries after a timeout, two transfers deadlock, a statement reads a lagging replica, a settlement file disagrees, and a whole site fails. Each needs a response designed before it happens.
A bad day for a ledger, five ways
Three simulations show the database behaviors this ledger depends on: an all-or-nothing transfer, lock order and deadlocks, and replicas that fall behind. All timings and loads are illustrative models.
App server
transfer code
Database
accounts table
Write-ahead log
on disk
Swipe the diagram sideways to see every column →
Where the time goes
8 ms total
Move ₹500 from Asha (balance ₹2,000) to Ben (balance ₹1,000). The app server crashes after the first UPDATE, restarts, and retries the transfer. Timings are an illustrative model.
Illustrative timings. Real numbers vary with distance, network quality and server load.
Transaction A
Asha → Ben
Database
lock manager
Transaction B
Ben → Asha
Swipe the diagram sideways to see every column →
Where the time goes
1011 ms total
Transaction A moves money from Asha to Ben while transaction B moves money from Ben to Asha. Each locks the account it debits first, then the one it credits. Timings are an illustrative model; PostgreSQL checks for deadlocks after waiting deadlock_timeout (1 s by default).
Illustrative timings. Real numbers vary with distance, network quality and server load.
Served
0/s
Errors
0%
p50 latency
0 ms
p95 latency
0 ms
Replica lag
—
Primary busy
0%
Two replicas serve reads while the primary takes writes. Each replica replays the primary's changes through one replay process that manages about 2,000 writes per second. Raise the share of writes and watch the lag. All numbers are an illustrative model.
Illustrative model: rates, queues and latency follow simple queueing rules, not a production benchmark.
The double-entry rules, append-only correction, idempotency keys, holds, clearing-account checks and the database behaviors are documented in the sources. The three stages, the traffic numbers, the bank's account layout and the cross-shard design are our educational assumptions.
Where the evidence stops