โ 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.
- Paying means two things. Money leaves your purse, and arrives in the till. They must both happen, or neither.
- The money leaves your purse. Then the till breaks before it can take it.
- Because it was one transaction, everything is undone. Your purse has its ยฃ100 back, as if nothing happened.
- Try again, both steps work, and the shop says "done". Now it's final, even if the power goes out a second later.
- Shopper 1 puts the last teddy in their basket but hasn't paid. Should shopper 2 see the shelf as empty?
- In the usual setting, no. Shopper 2 only sees what's been paid for, so the shelf still shows 1.
- Shopper 1 pays. If shopper 2 looks again during their own visit, they now see 0. The same question gave two answers.
- 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.
- Both shoppers check the shelf at the same moment, and both see one teddy left.
- Both buy it and both write "0 left". Two teddies sold, but there was only one.
- 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.