Skip to content

Payments · 4 of 6

Step 1 · The pattern

Subtract here, add there

To move £10, the service subtracts it from Alice's balance and then adds it to Bob's. Two updates. The total across both accounts never changes.

  • transfer
  • debit
  • credit

In short

Double-entry ledger: how it works and how it breaks

The naive way to move money is to subtract from one balance and add to another. Those are two updates, and a failure between them leaves money that has left one account and arrived nowhere. A balance column only shows the present, so afterwards you cannot tell what happened. A double-entry ledger changes the shape of the data. Every movement is one entry with at least two legs, a debit and a credit, that add up to zero, written in a single transaction. Entries are never edited or deleted; a mistake is corrected by adding a reversing entry. Balances are derived by summing the legs. Because every entry balances, the whole system must sum to zero, and a scheduled job can check that. A bug cannot create or destroy money without the books showing it.

How this comes up in interviews

"Design the data model for moving money between two accounts. What happens if the service fails half-way through a transfer?"

Then they ask

  1. An entry was written with the wrong amount. How do you correct it?
  2. If balances are never stored, how do you stop an account from being overdrawn by two concurrent transfers?
  3. How would you prove, every day, that no bug has created or destroyed money?

A strong answer covers

  • Two separate balance updates can fail between them, and a balance column cannot show what happened.
  • Record each movement as one entry with a debit leg and a credit leg that sum to zero, in a single transaction.
  • Entries are never edited or deleted. A mistake is corrected with a reversing entry.
  • Balances are derived by summing the legs.
  • The whole system must sum to zero, and a scheduled job can check that.

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.

Last updated: Engineering write-up

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.