Step 1 · The pattern
A balance is a sum
The ledger never stores a balance. To answer what is this account's balance? it reads every entry for the account and adds them up. With a short history that is instant.
- balance read
- answered
In short
Precomputed balances: how it works and how it breaks
In an append-only ledger a balance is not stored anywhere. It is the sum of every entry for the account. That is easy to trust, but the read grows with the account's whole history. An index does not help, because the query really does need every row. Faster hardware only delays the problem. The way out is to read less. Entries in the past never change, so the sum of any finished run of them never changes either. Group consecutive entries into blocks, store each block's total, and a balance becomes the stored totals plus the few entries written since the last block closed. Bucketing entries by time has the same effect, because a read can skip the old buckets entirely. The entries stay the source of truth, and the stored totals can always be rebuilt and checked against them.
How this comes up in interviews
"Balances are computed by summing every ledger entry for an account. Reads get slower each month. How do you fix it without losing trust in the numbers?"
Then they ask
- Why does adding an index or a bigger machine not solve this?
- Why is it safe to reuse a stored total for old entries?
- How would you detect that a stored total has become wrong?
A strong answer covers
- A balance is the sum of every entry for the account, so the read grows with the account's whole history.
- An index does not help because the sum needs every row, and faster hardware only delays the problem.
- Past entries never change, so the sum of a finished block never changes either.
- Store each block's total, then a read is the stored totals plus the entries since the last block closed.
- The entries stay the source of truth, so totals can be rebuilt and checked against them.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: Monzo: Speeding up our balance read time, the planning phase ↗ · Monzo: How we calculate balances ↗ · PostgreSQL documentation: materialized views ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.