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

All concepts ยท databases ยท sql ยท concurrency

Transactions and isolation

Grouping 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.

12 min3 animations

๐Ÿ–จ๏ธ Cheat sheet

All or nothing

Paying for something is two steps: money leaves your purse, and arrives in the till. If the till breaks halfway, you want your money back, not lost in between. Either both happen or neither does. And once the shop says "done", it stays done, even if the lights go out.

ACID

A transaction groups statements between BEGIN and COMMIT:

  • Atomicity: all changes apply, or on ROLLBACK or error, none do.
  • Consistency: constraints (keys, checks) hold before and after.
  • Isolation: concurrent transactions don't see each other's half-done work (to a degree you choose).
  • Durability: once committed, changes survive crashes, thanks to the write-ahead log.

In Spring, @Transactional on a service method opens a transaction and commits or rolls it back for you.

All or nothing4 steps
๐Ÿง’Shopper๐Ÿ‘› Your purse๐Ÿงพ Shop till
Press play and watch it happen!

What other shoppers see

While you've got the last teddy in your basket but haven't paid, should other shoppers see the shelf as empty? Usually they see only what's been paid for. A stricter shop gives each shopper a photo of the shop as it was when they walked in, so their view never changes mid-visit.

Isolation levels

LevelPreventsStill allows
READ UNCOMMITTED(almost nothing)dirty reads
READ COMMITTED (Postgres, Oracle default)dirty readsnon-repeatable reads, phantoms
REPEATABLE READ (MySQL default)+ non-repeatable readssome anomalies (write skew)
SERIALIZABLEeverything(may abort transactions to retry)

Most databases use MVCC: writers create new row versions, and readers see a consistent snapshot without locking.

What others can see4 steps
๐Ÿง’Shopper 1๐Ÿ‘งShopper 2๐Ÿ“ฆ Shelf count
Press play and watch it happen!

One teddy, two shoppers

Two shoppers see one teddy, both buy it, both write "none left". The shop sold two of one. The fix: say "take one, only if there's one left", or hold the shelf while you check.

Lost updates

Read-then-write in application code races even inside transactions at READ COMMITTED. Fixes:

  • an atomic update: UPDATE stock SET qty = qty - 1 WHERE id = ? AND qty > 0, then check the row count,
  • a pessimistic lock: SELECT ... FOR UPDATE holds the row until commit,
  • optimistic locking: a version column (JPA @Version), so a stale update affects 0 rows and is retried,
  • or SERIALIZABLE isolation with retries.
Two shoppers, one teddy3 steps
๐Ÿง’Shopper 1๐Ÿ‘งShopper 2๐Ÿ“ฆ Shelf count
Press play and watch it happen!

๐Ÿ—๏ธ Design it in CloudArch

Build the architecture on a canvas, push load through it and see what breaks.

  • ๐ŸŽŸ๏ธDesign ticket bookingSell every seat exactly once while a million fans hit refresh.practice problem

๐Ÿงฉ Quick quiz

Quick quiz! 3 questions about Transactions and isolation, told the same way as the story.
  1. The till breaks after money has left your purse but before it reaches the till. What should happen?
  2. A stricter shop gives each shopper a photo of the shop as it was when they walked in. What does that give them?
  3. Two shoppers both buy the last teddy. What's the fix?
Answer every question to check.Passing marks Transactions and isolation as done.
Got the hang of Transactions and isolation? Mark it as done, and your paths show how far youโ€™ve got.

Saved in this browser.

๐ŸŽฏ Backend interview prepStep 2 of 9

โ† Previous on the path๐Ÿ“‘ Database indexesNext on the path โ†’๐Ÿ”˜ Idempotency

๐Ÿ—ƒ๏ธ Spring and databasesStep 2 of 7

โ† Previous on the path๐ŸŒฑ Spring and Spring BootNext on the path โ†’๐Ÿ“‘ Database indexes

๐Ÿ‘‰ Read next

  • ๐Ÿ›๏ธAmazon RDS and AuroraKeeping your records at a council-run records office that fixes the building, copies the files and keeps a twin office.
  • ๐Ÿš•Connection poolingA taxi rank outside a station: riders take a waiting taxi, and it comes back to the rank afterwards.
  • ๐Ÿฝ๏ธThe N+1 query problem (JPA and Hibernate)A waiter who walks back to the kitchen once per diner for each side dish, instead of carrying one tray.
  • ๐ŸงณSagas and distributed transactionsBooking a flight, hotel and car with three companies, and cancelling the earlier bookings if a later one falls through.
  • ๐Ÿ“Spring transactions (@Transactional)A notary's clerk who opens a contract folder before the work and seals or shreds it at the end.