Skip to content

Transactions & Isolation

A database that one person uses at a time is easy to trust. A database that many requests use at once has to decide what each of them may see, and what happens when two of them touch the same row. The answer is the transaction, and the promises it makes are among the most commonly misunderstood parts of working with data.

This module starts with what a transaction promises. It then shows five ways two transactions can disturb each other, the isolation levels that stop them, the two ways a database enforces those levels, and what happens when transactions wait for each other forever. Along the way you will see where PostgreSQL and MySQL’s InnoDB differ, using only what their documentation says.

You will step through concurrent transactions on a timeline, change the isolation level and the mechanism and watch the result change, and read a transaction run on a tiny in-memory store.

  1. ACID and the transaction (15 min). What a transaction bundles, what each letter of ACID promises, and why the C is a promise of a different kind.
  2. Five anomalies (22 min). Dirty read, non-repeatable read, phantom read, lost update and write skew, each on a timeline you can step through.
  3. Isolation levels (24 min). What each level stops with locks and with row versions, and what PostgreSQL and MySQL document under the same names.
  4. Locks and row versions (20 min). Shared and exclusive locks, snapshots, and what each mechanism costs.
  5. Deadlocks (20 min). How a cycle of waiting forms, what the database does, and why a fixed order prevents it.
  6. Recap and quiz (12 min). The module in one page, a checklist, and a quiz that covers all five lessons.
  • Say what a transaction does and does not guarantee, and what the C of ACID asks of the application.
  • Recognise each of the five anomalies from the order of the statements, and say which isolation level stops it.
  • Explain how locks and row versions keep transactions apart, and predict who waits.
  • Handle a deadlock or a serialization failure the right way: expect it, retry the whole transaction, and take rows in a fixed order.

All the numbers in the labs are illustrative. They are for comparing choices, not for forecasting any real product.

Built by GhaziAlibi.

© ShardCraft. All rights reserved.