Volume 5 · Chapter 6 — Storage and Databases
Financial Transaction Integrity
6.1 Introduction
For a bank, no topic matters more than transaction integrity: money must never be created, lost, or double-counted, even under concurrency and failure. This is where ACID transactions, isolation levels, and idempotency come together, and it’s a favourite senior-interview area because it separates engineers who can casually build a CRUD app from those who can guarantee correctness of financial operations. This chapter covers the guarantees that keep a ledger correct - and why they’re non-negotiable for money movement.
6.2 ACID Transactions
A transaction groups operations so they succeed or fail as a unit, and the ACID properties define its guarantees. Atomicity means all-or-nothing: either every operation in the transaction commits, or none does, so a transfer that debits one account and credits another can never leave only the debit applied. Consistency means the transaction moves the database from one valid state to another, respecting all rules and constraints. Isolation means concurrent transactions don’t interfere with each other - the result is as if they ran in some serial order. Durability means once committed, the change survives crashes and power loss. For money movement these aren’t nice-to-haves: atomicity prevents half-completed transfers, isolation prevents concurrent transactions corrupting balances, and durability guarantees a confirmed transaction isn’t lost - which is exactly why a bank’s ledger runs on a relational database (PostgreSQL/Aurora) that provides full ACID.
sequenceDiagram
autonumber
participant App
participant DB as Database (ACID)
App->>DB: BEGIN transaction
App->>DB: debit account A
App->>DB: credit account B
alt both succeed
App->>DB: COMMIT (atomic, durable)
DB-->>App: committed - both applied
else any failure
App->>DB: ROLLBACK
DB-->>App: none applied - no half transfer
end 6.3 Concurrency and Isolation Levels
When many transactions run at once, isolation levels control how much they can affect each other, trading correctness against performance. Lower isolation allows anomalies - a transaction reading data another hasn’t committed (dirty read), or getting different results on re-read because another transaction changed the data (non-repeatable read), or new rows appearing (phantom read). Higher isolation prevents these but reduces concurrency. The classic financial hazard is a race condition: two transactions read the same balance simultaneously and both act on it, so an update is lost or a balance goes wrong. Banks address this with sufficiently high isolation and with locking or optimistic concurrency control - either locking the row so concurrent transactions serialize, or detecting a conflicting change and retrying - so concurrent operations on the same account can’t corrupt the balance. Choosing the right isolation and concurrency strategy is central to a correct ledger.
6.4 Idempotency
In a distributed system, requests can be retried - a client resends after a timeout, a queue redelivers a message - so an operation may be received more than once. Idempotency ensures that processing the same operation multiple times has the same effect as processing it once, which is essential for money movement: a retried “transfer $100” must not transfer $200. You achieve it with an idempotency key - a unique identifier for the operation that the system records when it processes it, so a duplicate carrying the same key is recognized and not applied again. This makes retries safe: the client can retry freely for reliability, and the system guarantees the transfer happens exactly once. Idempotency is what lets a bank combine at-least-once delivery and client retries (needed for reliability) with exactly-once financial effect (needed for correctness).
6.5 Reliable Messaging and Consistency Across Services
When a financial operation spans services - a payment that updates a ledger and notifies other systems - keeping them consistent is hard, because you can’t have a single database transaction across services. Patterns address this: the outbox pattern writes the state change and an event to publish in the same local transaction, then reliably publishes the event, so the database update and the event can’t diverge; consumers are made idempotent to tolerate duplicate deliveries; and where a multi-step process must stay consistent, sagas coordinate with compensating actions. The overarching principle for a bank is that the authoritative financial state lives in one ACID system of record, and cross-service propagation is done reliably and idempotently around it - so the ledger is always correct even as events flow to other services eventually.
6.6 Banking Production Scenario
A bank processes a transfer as a single ACID transaction in PostgreSQL: the debit and credit either both commit or both roll back (atomicity), the committed result survives any crash (durability), and row locking with appropriate isolation prevents two concurrent transfers on the same account from corrupting the balance. Each transfer request carries an idempotency key, so if a client retries after a timeout or a message is redelivered, the duplicate is recognized and the transfer is applied exactly once - never twice. To notify downstream fraud and notification services, the bank uses the outbox pattern, writing the event in the same transaction as the ledger update and publishing it reliably, with idempotent consumers. The authoritative balance always comes from this ACID system of record, never a cache. The result is money movement that is atomic, durable, concurrency-safe, and exactly-once.
6.7 Troubleshooting: Integrity Failures
Integrity bugs are the most serious, and they have specific causes. A balance that’s occasionally wrong under load is a race condition from insufficient isolation or missing locking - two transactions interleaving on the same row; the fix is proper locking or optimistic concurrency and adequate isolation. A duplicated transfer means the operation wasn’t idempotent - a retry or redelivery applied it twice; the fix is idempotency keys. A half-applied transfer (debit without credit) means the operations weren’t in one atomic transaction. Inconsistency between the ledger and downstream services means events and state diverged - the fix is the outbox pattern and idempotent consumers. A confirmed transaction lost after a crash means durability wasn’t guaranteed (or the confirmation was sent before commit). The discipline is to wrap money movement in ACID transactions, enforce idempotency on anything retryable, use proper concurrency control, and propagate cross-service changes reliably - correctness first, always.
How do you guarantee correctness of a money transfer under concurrency, failure, and retries?
Guaranteeing a correct money transfer requires three things working together: ACID transactions, proper concurrency control, and idempotency - because money must never be created, lost, or double-counted even when transactions run concurrently, systems crash, or requests are retried. First, I wrap the transfer in an ACID transaction in a relational system of record like PostgreSQL or Aurora. Atomicity makes the debit and credit all-or-nothing, so a transfer can never leave only the debit applied - either both commit or neither does. Durability guarantees that once the transaction commits, the result survives crashes and power loss, so a confirmed transfer is never lost - and I make sure the client is confirmed only after the commit, not before. Consistency ensures constraints and rules hold. Second, I handle concurrency with isolation and locking, because the classic financial hazard is a race condition where two transactions read the same balance at once and both act on it, losing an update or corrupting the balance. I use a sufficiently high isolation level and either pessimistic locking - locking the account row so concurrent transfers on it serialize - or optimistic concurrency control, where I detect that the row changed under me and retry. Either way, concurrent operations on the same account can’t interleave to produce a wrong balance. Third, I make the operation idempotent, because in a distributed system requests get retried - a client resends after a timeout, or a queue redelivers a message - so the same transfer may arrive more than once, and a retried “transfer $100” must not become $200. I attach an idempotency key, a unique identifier for the operation, and record it when the transfer is processed; a duplicate carrying the same key is recognized and not applied again, so the transfer happens exactly once no matter how many times it’s delivered. This lets me safely combine the at-least-once delivery and client retries that reliability requires with the exactly-once financial effect correctness requires. If the operation spans services - updating the ledger and notifying other systems - I keep them consistent with the outbox pattern, writing the event in the same transaction as the ledger update and publishing it reliably to idempotent consumers, so the authoritative state and the events can’t diverge, while the authoritative balance always lives in the one ACID system of record. Put together: ACID transactions give atomic, durable money movement; isolation and locking prevent concurrency corruption; idempotency keys make retries safe; and reliable, idempotent cross-service propagation keeps everything consistent - which is how you guarantee a transfer is correct under concurrency, failure, and retries. Correctness comes first, and every one of these is non-negotiable for financial operations.
What is idempotency, and why is it essential for financial operations?
Idempotency means that performing the same operation multiple times has the same effect as performing it once - a duplicate execution doesn’t change the outcome beyond the first. It’s essential for financial operations because of a fundamental reality of distributed systems: requests can and do get delivered or processed more than once. A client that doesn’t get a response due to a timeout will retry, even though the original request may actually have succeeded; a message queue guarantees at-least-once delivery, so a message can be redelivered; a network hiccup can cause a resend. Without idempotency, these duplicates are catastrophic for money: a retried “transfer $100” would transfer $200, a redelivered “charge the customer” would charge them twice - the operation would be applied as many times as it’s received, creating or moving money incorrectly. Idempotency prevents this by ensuring the operation is applied exactly once regardless of how many times it arrives. The standard mechanism is an idempotency key: the client assigns a unique identifier to the operation, and the server records that key when it processes the request; if a request arrives with a key it has already processed, the server recognizes it as a duplicate and does not apply it again - instead it returns the original result. This makes retries safe, which is the crucial point: reliability in distributed systems depends on being able to retry (because you often can’t tell whether a failed request actually succeeded), and idempotency is what lets you retry freely without risking duplicate effects. So it reconciles two needs that would otherwise conflict - the at-least-once delivery and client retries that reliability requires, and the exactly-once financial effect that correctness requires. For a bank this is non-negotiable: every operation that moves money or has financial side effects must be idempotent, so that the inevitable retries and redeliveries in a real distributed system never cause a transfer, charge, or payment to be applied more than once. It’s one of the defining differences between a toy CRUD system and a correct financial system, which is why interviewers probe it - understanding that duplicates are inevitable and that idempotency keys make processing exactly-once is essential to building anything that handles money reliably.
Chapter Revision Notes
- ACID: atomicity (all-or-nothing), consistency (valid states), isolation (no interference), durability (survives crashes) - non-negotiable for money.
- Concurrency hazard: race conditions on the same balance; fix with adequate isolation plus pessimistic locking or optimistic concurrency control.
- Idempotency (via idempotency keys) makes retried/redelivered operations apply exactly once - essential because duplicates are inevitable.
- Cross-service consistency: outbox pattern (event in the same transaction) + idempotent consumers; authoritative state in one ACID system of record.
- Never authorize money movement from a cache; read the authoritative balance from the ACID source of truth.