Quick revision
The short answer
I would make an immutable double-entry ledger the source of truth, because it gives a full audit trail, replay and reconciliation, and add a balance table updated in the same database transaction as each entry, locked per account, so reads are fast and overdraft checks are exact. A reconciliation job compares the two and alerts on drift, and busy accounts are handled with sub-accounts or buffered entries. Statements use window running totals from the last snapshot, while a balance check reads one row.
What to remember
In this answer, we compared an immutable double-entry ledger, which gives a full audit trail, replay and reconciliation but makes balance checks expensive and racy, with a mutable balance column, which gives fast reads and exact overdraft checks under a row lock but becomes a hot row and a poor audit record. The hybrid keeps the ledger as source of truth and updates a balance table in the same transaction with SELECT ... FOR UPDATE in a fixed order, uses sub-accounts or buffered entries for hot accounts, and reconciles the two with a job that alerts on drift. Statements use window running totals from the last snapshot, while balance checks read one row.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would make an immutable double-entry ledger the source of truth, because it gives a full audit trail, replay and reconciliation, and add a balance table updated in the same database transaction as each entry, locked per account, so reads are fast and overdraft checks are exact. A reconciliation job compares the two and alerts on drift, and busy accounts are handled with sub-accounts or buffered entries. Statements use window running totals from the last snapshot, while a balance check reads one row.
Why this question is asked
Balance handling is where a payments platform earns or loses trust. A balance that is wrong by one cent, or a double spend that slipped through two concurrent requests, is an incident with regulators and customers watching. The two proposals in this question represent a real trade-off between auditability and speed, and interviewers ask it to see whether we can reason about correctness, concurrency and audit together and end up with a design that keeps the strengths of both. In this answer, we will recap double-entry ledgers, look at each proposal on its own, combine them, handle concurrency and hot accounts, and show how the SQL for statements and balance checks changes.
1A Quick Recap of a Double-Entry Ledger
A ledger is a list of entries that record money moving. In double-entry bookkeeping, every movement is written as at least two entries that sum to zero: a transfer of 50 from Ana to Ben is a debit of 50 on Ana's account and a credit of 50 on Ben's. Because every transaction sums to zero, the sum of all entries across all accounts is always zero, which is a powerful check that no money was created or lost by a bug.
Immutable means entries are never updated or deleted. A mistake is fixed with a new reversing entry, so the ledger always shows what happened and when, which is what auditors need.
2Proposal 1: Only Immutable Entries
The first proposal stores only the ledger and derives every balance as a sum of entries. It is excellent for audit: the balance at any moment in the past can be recomputed, any figure can be explained entry by entry, and the system can be replayed after a bug fix. There is no second copy of the truth that can disagree.
The weakness is reads and checks. The current balance is SUM(amount) over all of an account's entries, which grows forever. Worse, a no-overdraft check, "does Ana have at least 50?", needs that sum, and two concurrent payments can both compute 60, both decide that 50 fits, and both insert, leaving -40. Preventing that needs a lock on something per account or SERIALIZABLE isolation, and plain inserts give us nothing to lock.
3Proposal 2: A Mutable Balance Column
The second proposal keeps a balance column per account and updates it in the same transaction as each entry. Reads are one row. The overdraft check can be part of the update itself: a conditional UPDATE only changes the row if the balance is high enough, and the row lock it takes makes a second concurrent payment wait until the first one commits, then re-check against the new balance.
The weakness is the hot row. Every payment for a busy account, say a large merchant receiving 300 payments per second, must update the same row, and each update holds the row lock until its transaction commits. If a transaction takes 3 ms, the account can handle at most about 330 updates per second, and everything behind it queues. And on its own, a mutable column is a poor audit record: it shows the current number, not how it got there.
4The Hybrid: Ledger Plus a Transactional Balance
The design most payment systems arrive at combines the two. The ledger is the source of truth. The balance table is a derived value maintained in the same database transaction as the entries, so both commit or neither does. For a transfer, we lock both balance rows in a fixed order, by account id, so two opposite transfers cannot deadlock, check the balance, update both rows and insert both entries.
6 messages in order. Play them, or step through them one at a time.
Entries and balances commit together, so they can only drift through a bug, which reconciliation catches.
Strict ordering per account comes from these row locks: two payments from the same account are applied one after the other. An alternative is optimistic locking with the version column, retrying on conflict, which suits low contention. SERIALIZABLE isolation also works, with retries on serialization failures, but explicit row locks are easier to reason about for money.
5Hot Accounts and Reconciliation
For the few accounts that become hot, we have two tools. Sub-accounts split one logical account into, say, 16 balance rows; credits go to a random sub-account, so 16 transactions can proceed in parallel, and the balance is the sum of the 16 rows. Debits with an overdraft check are harder, because they need the total, so this works best for accounts that mostly receive money, such as merchants. Buffered entries go further: credits to a hot account are written to the ledger immediately but applied to its balance row in batches every second, which is safe because adding money cannot cause an overdraft.
Because the balance is now a second copy of the truth, we check it. A reconciliation job recomputes each account's balance from the ledger, from the last snapshot plus later entries, compares it with the balance table, and alerts on any difference. It also checks that every transfer's entries sum to zero. Drift should never happen; if it does, it means a bug, and the ledger tells us which entries to look at.
| Design | Audit | Read cost | Concurrency |
|---|---|---|---|
| Ledger only | full history, replay | sum of all entries | needs extra locking for checks |
| Mutable balance only | current value only | one row | row lock, hot rows |
| Ledger + transactional balance | full history, reconciled | one row | row lock, sub-accounts if hot |
6How the Queries Change
With the hybrid, the two common queries become simple. A balance check or an account page reads one row. A statement uses window running totals over the ledger, starting from the last balance snapshot before the period, exactly as with any large ledger.
Notice that the ledger query orders by posted_at and id with a ROWS frame, so two entries in the same instant still show their own running balances. The snapshot keeps the work proportional to the period, not to the account's lifetime.
7Follow-up questions
How would you design reconciliation between the ledger and the balance table?
A scheduled job, per account or in chunks of accounts, computes the last snapshot plus the sum of later entries and compares it with the balance row, reading both in one REPEATABLE READ snapshot so concurrent transfers do not cause false alarms. It also verifies that each transfer's entries sum to zero and the global sum is zero. Any difference raises an alert with the account and the entries since the last good check, and nothing is auto-corrected without review.
Why lock the accounts in id order?
Because a transfer from A to B and one from B to A at the same moment would otherwise each lock one row and wait for the other, a deadlock. Locking in a fixed order means both try A first, so one simply waits for the other.
Can the balance be eventually consistent instead?
For credit-only hot accounts, buffering balance updates is fine. For any account where we check for overdraft, the balance used for the check must be updated in the same transaction, or we risk double spending.
8Conclusion
In this answer, we compared an immutable double-entry ledger, which gives a full audit trail, replay and reconciliation but makes balance checks expensive and racy, with a mutable balance column, which gives fast reads and exact overdraft checks under a row lock but becomes a hot row and a poor audit record. The hybrid keeps the ledger as source of truth and updates a balance table in the same transaction with SELECT ... FOR UPDATE in a fixed order, uses sub-accounts or buffered entries for hot accounts, and reconciles the two with a job that alerts on drift. Statements use window running totals from the last snapshot, while balance checks read one row.
Open with
I would not choose one or the other; I would combine them. The source of truth is an immutable double-entry ledger: every transfer writes entries that sum to zero, mistakes are fixed with reversing entries, and that gives auditors a full history, lets us replay, and lets us reconcile.
Then, if they want more
Deriving balances only from the ledger makes reads grow forever and makes overdraft checks racy, because two payments can read the same sum and both pass. So I add a balance table updated in the same database transaction as the entries.
For a transfer I lock both balance rows with select for update in account id order, which serializes payments per account and avoids deadlocks, check the balance, update both rows and insert the entries, and commit. A conditional update with balance greater than or equal to the amount works for single-account debits.
The cost is hot rows: a busy merchant is limited by how fast one row can be locked and committed, so for those I would split into sub-accounts or buffer credits, which cannot cause overdrafts.
Since the balance is a second copy, a reconciliation job recomputes it from snapshots and the ledger, checks every transfer sums to zero, and alerts on drift. Queries change accordingly: a balance check reads one row, and a statement takes the last snapshot plus a window running total with a ROWS frame over the period's entries.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You are designing balance handling for a payments platform. One proposal stores only...": https://scrivio-interview-library.netlify.app/databases/sql-coding-problems/7b466c5105/