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 Transactions and isolation

๐Ÿ›’ Transactions and isolation: cheat sheet

๐ŸŽจ The analogyPaying at a till, where money leaves your purse and enters the till together or not at all.
๐ŸŽ“ The regular explanationGrouping database changes so they happen all together or not at all (ACID), and controlling what concurrent transactions can see of each other's unfinished work through isolation levels.
All or nothingStep 4 of 4
๐Ÿง’Shopper๐Ÿ‘› Your purseยฃ70๐Ÿงพ Shop tillยฃ30
  1. Paying means two things. Money leaves your purse, and arrives in the till. They must both happen, or neither.
  2. The money leaves your purse. Then the till breaks before it can take it.
  3. Because it was one transaction, everything is undone. Your purse has its ยฃ100 back, as if nothing happened.
  4. Try again, both steps work, and the shop says "done". Now it's final, even if the power goes out a second later.
What others can seeStep 4 of 4
๐Ÿง’Shopper 1๐Ÿ‘งShopper 2snapshot๐Ÿ“ฆ Shelf count0
  1. Shopper 1 puts the last teddy in their basket but hasn't paid. Should shopper 2 see the shelf as empty?
  2. In the usual setting, no. Shopper 2 only sees what's been paid for, so the shelf still shows 1.
  3. Shopper 1 pays. If shopper 2 looks again during their own visit, they now see 0. The same question gave two answers.
  4. A stricter setting gives each shopper a photo of the shop as it was when they walked in, so their view doesn't change mid-visit.
Two shoppers, one teddyStep 3 of 3
๐Ÿง’Shopper 1๐Ÿ‘งShopper 2๐Ÿ“ฆ Shelf count0
  1. Both shoppers check the shelf at the same moment, and both see one teddy left.
  2. Both buy it and both write "0 left". Two teddies sold, but there was only one.
  3. The fix is to reserve the shelf while checking, or to say "take one, only if there's one left".

๐Ÿงฉ Key questions

The till breaks after money has left your purse but before it reaches the till. What should happen?
Both steps are undone, so your money comes back. Either both happen or neither does.
A stricter shop gives each shopper a photo of the shop as it was when they walked in. What does that give them?
A view that never changes mid-visit. Usually shoppers see only what's been paid for; the photo goes further.
Two shoppers both buy the last teddy. What's the fix?
Say "take one, only if there's one left", or hold the shelf while you check. Otherwise the shop sells two of one.