TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About · Labs · Cheat sheets · Learning paths

← 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.
Events instead of current stateStep 5 of 5
Ana's passbook+10key paid in £100: +1000-30key took out £30: -301+50key paid in £50: +50234🙋‍♀️Ana🪧 Chalk slate£120🧮Adding up= £120
  1. Ana pays in £100. The slate bank chalks "£100". The passbook bank writes a new line, "paid in £100".
  2. She takes out £30. The slate bank rubs out £100 and chalks £70. The passbook bank adds a second line and keeps the first.
  3. She pays in £50. The slate now says £120. The passbook has three lines.
  4. Ana asks the slate how she got to £120. It can only say "£120". The old numbers were rubbed out.
  5. The passbook bank adds up the lines, 100 take away 30 plus 50, and gets £120, with every step on show.
Replay and snapshotsStep 5 of 5
Ana's passbook+10key paid in £100: +1000-30key took out £30: -301+50key paid in £50: +502-20key took out £20: -203+40key paid in £40: +404-15key took out £15: -155+60key paid in £60: +606-35key took out £35: -3578read🧑‍💼Clerk£150🗂️ Subtotal cardline 6 = £125
  1. Ana's passbook has six lines. The clerk doesn't know her balance yet.
  2. To find out, the clerk adds up every line from the top and gets £125.
  3. To save time next visit, the clerk writes the total on the subtotal card, "up to line 6, £125".
  4. Ana pays in £60 and takes out £35. Two new lines, and the card isn't changed.
  5. Next time, the clerk starts from the card's £125 and adds only lines 7 and 8. £150, without reading the whole book.
Commands write, read models answerStep 6 of 6
Passbook+10key paid in £100: +1000-30key took out £30: -301+50key paid in £50: +502-40key took out £40: -4034🙋‍♀️Ana🧑‍💼Clerkonly £80🧑‍🎨Board keeper🪧 Balance board£80🧍Bo
  1. The passbook has three lines adding up to £120, and the board in the window says £120 too.
  2. Ana hands the clerk a slip, "take out £40". The clerk adds up the passbook and sees there's enough.
  3. The clerk writes the new line and tells Ana it's done.
  4. Bo glances at the board straight away. It still says £120, because the keeper hasn't copied the new line yet.
  5. The keeper reads the new line and changes the board to £80. When Bo looks again, it's right.
  6. Ana asks for £500. The clerk adds up, sees only £80, and says no. Nothing is written, so the board has nothing to change.
Two writers, one streamStep 5 of 5
Shared passbook+10key paid in £100: +1000-30key took out £30: -301+50key paid in £50: +502-10key took out £100: -100345next🧑‍💼Clerk for Anasaw 3 lines👩‍💼Clerk for Ben£20: refused
  1. The shared passbook has three lines, £120 in total. The next line to write is line 4.
  2. At the same moment, Ana asks one clerk for £100 and Ben asks another for £80. Both add up the passbook and see £120.
  3. Ana's clerk writes "took out £100" as line 4. Nobody has written line 4 yet, so it's accepted.
  4. 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.
  5. Ben's clerk adds up again, sees only £20 left, and tells Ben no.
Upcasting old eventsStep 4 of 4
Ana's passbook · old and new100key v1: paid in 100: 100050key v1: paid in 50: 501€20key v2: paid in 20 EUR: €2023🔤 Translation card🧑‍💼Clerknew style only
  1. Years ago, lines said only "paid in 100", with no currency.
  2. Now the bank's form has a currency box, so new lines say "paid in €20". The old lines stay as they were.
  3. When the clerk reads an old line, it goes through the translation card, "no currency means pounds", and comes out as "paid in £100".
  4. New lines need no card and go straight to the clerk. The clerk only ever sees the new style, and the passbook is unchanged.
A crash between the write and the phone callStep 4 of 4
Passbook-40key took out £40: -4001234To-phone pileph1key tell head office about -£40: ph101234🧑‍💼Clerk🛵Messenger🏢Head officeignored copy
  1. Ana takes out £40. In one go, the clerk writes the new passbook line and puts a "to phone" note on the pile.
  2. 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.
  3. The messenger finds the note on the pile and phones head office. The clerk is back on their feet, and head office now knows.
  4. 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.
Event sourcing in a Spring appStep 5 of 5
Passbook+10key Deposited 100, v1: +1000-30key Withdrawn 30, v2: -301+50key Deposited 50, v3: +502-40key Withdrawn 40, v4: -4034🪟Counter🧑‍💼Clerk£120, 3 lines🧑‍🎨Board keeper🪧 Balance board£80
  1. The passbook already has account 42's three lines, and the board says £120.
  2. A request to take out £40 arrives at the counter. The clerk reads the passbook and works out £120.
  3. The clerk writes line 4, but only if nobody else has written it yet, and tells the counter it's done.
  4. Half a second later, the board keeper wakes up, reads the new line and changes the board to £80.
  5. 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.