← Back to Event sourcing and CQRS
📒 Event sourcing and CQRS: cheat sheet
🎨 The analogyA bank passbook where every deposit is a new line never rubbed out, plus a balance board in the window.
🎓 The regular explanationEvent sourcing stores every state change as an immutable event and rebuilds state by replaying them; CQRS splits the model that handles commands from the read models that answer queries, which are updated from those events.
- Ana pays in £100. The slate bank chalks "£100". The passbook bank writes a new line, "paid in £100".
- She takes out £30. The slate bank rubs out £100 and chalks £70. The passbook bank adds a second line and keeps the first.
- She pays in £50. The slate now says £120. The passbook has three lines.
- Ana asks the slate how she got to £120. It can only say "£120". The old numbers were rubbed out.
- The passbook bank adds up the lines, 100 take away 30 plus 50, and gets £120, with every step on show.
- Ana's passbook has six lines. The clerk doesn't know her balance yet.
- To find out, the clerk adds up every line from the top and gets £125.
- To save time next visit, the clerk writes the total on the subtotal card, "up to line 6, £125".
- Ana pays in £60 and takes out £35. Two new lines, and the card isn't changed.
- Next time, the clerk starts from the card's £125 and adds only lines 7 and 8. £150, without reading the whole book.
- The passbook has three lines adding up to £120, and the board in the window says £120 too.
- Ana hands the clerk a slip, "take out £40". The clerk adds up the passbook and sees there's enough.
- The clerk writes the new line and tells Ana it's done.
- Bo glances at the board straight away. It still says £120, because the keeper hasn't copied the new line yet.
- The keeper reads the new line and changes the board to £80. When Bo looks again, it's right.
- Ana asks for £500. The clerk adds up, sees only £80, and says no. Nothing is written, so the board has nothing to change.
- The shared passbook has three lines, £120 in total. The next line to write is line 4.
- At the same moment, Ana asks one clerk for £100 and Ben asks another for £80. Both add up the passbook and see £120.
- Ana's clerk writes "took out £100" as line 4. Nobody has written line 4 yet, so it's accepted.
- Ben's clerk tries to write line 4 too, but it's already taken. The line is refused, so the account isn't £60 short.
- Ben's clerk adds up again, sees only £20 left, and tells Ben no.
- Years ago, lines said only "paid in 100", with no currency.
- Now the bank's form has a currency box, so new lines say "paid in €20". The old lines stay as they were.
- When the clerk reads an old line, it goes through the translation card, "no currency means pounds", and comes out as "paid in £100".
- New lines need no card and go straight to the clerk. The clerk only ever sees the new style, and the passbook is unchanged.
- Ana takes out £40. In one go, the clerk writes the new passbook line and puts a "to phone" note on the pile.
- The clerk trips before phoning. With no note, head office would never have heard about the line. But the note is on the pile, so nothing is lost.
- The messenger finds the note on the pile and phones head office. The clerk is back on their feet, and head office now knows.
- The messenger phones a second time, as the note was not crossed off in time. Head office sees it has this line already and ignores the repeat.
- The passbook already has account 42's three lines, and the board says £120.
- A request to take out £40 arrives at the counter. The clerk reads the passbook and works out £120.
- The clerk writes line 4, but only if nobody else has written it yet, and tells the counter it's done.
- Half a second later, the board keeper wakes up, reads the new line and changes the board to £80.
- When the app asks for the balance, the counter looks at the board, not the passbook, and answers £80.
🧩 Key questions
- The clerk wrote "paid in £100" when Ana only paid in £10. How does the passbook bank put it right?
- Write a new line that takes the extra £90 back off. Nobody ever rubs a line out. The passbook shows both the mistake and the fix.
- The subtotal card falls behind the radiator and is lost. What happens?
- The clerk adds up from the top again; it just takes longer. The card was only a shortcut. The lines are the real record.
- Ana takes out £40, the clerk says done, and a second later the board in the window still shows the old amount. Is something broken?
- No: the board keeper hasn't copied the new line yet, and soon will. The board always catches up, but there's a short moment when it's behind.
- Two clerks both add up the shared passbook, see three lines and £120, and both try to write line 4. What stops the account going short?
- Only one can write line 4; the other must add up again and check once more. Writing a line only if nobody has written it yet means the second clerk sees the first one's line before deciding.
- The bank now writes which money each line is in. What does it do with the old lines that don't say?
- Leaves them alone and reads them through a translation card. The passbook stays as it was written; the card makes old lines look new to the clerk.