Isolation Levels

How much of another transaction's mess can you see?

L218 MINDATABASES
PREDICT

Alice and Bob each run: count on-call doctors; if the count is at least 2, set my own on_call = false; COMMIT. They interleave. What is the lowest isolation level that guarantees someone stays on call?

A transaction promises that it runs as if it were alone. Isolation levels say how much of that promise the database keeps. Each level is a rule for what a read returns and when a commit is refused. READ UNCOMMITTED shows you other transactions’ unfinished writes. READ COMMITTED shows only committed data, but re-checks the latest version on every read. REPEATABLE READ, built on MVCC, reads one snapshot taken at BEGIN. SERIALIZABLE reads the same snapshot and also aborts a transaction whose reads and writes cross another’s in a cycle. Move the slider and the same schedule produces different results.

▶ SIMULATION
TXN A
status: idle
reads from: —
pending writes: —
—
COMMITTED VERSIONS (value@ts)
—
commit clock: 0
TXN B
status: idle
reads from: —
pending writes: —
—
isolationREPEATABLE READ (level 2)
✓ nobody acts on a value that was rolled back✓ a row read twice in one transaction has one value✓ a range query re-run in one transaction returns the same rows✓ at least one doctor stays on call
SCHEDULE
01[DB]setup: balance = 500 (committed)
02[Txn B]B: BEGIN
03[Txn B]B: UPDATE balance = 900 (not committed)
04[Txn A]A: BEGIN
05[Txn A]A: SELECT balance
06[Txn A]A: INSERT payout = the balance it read
07[Txn B]B: ROLLBACK
08[Txn A]A: COMMIT
09[DB]report
Start of dirty read scenario. 9 steps.
Isolation levels as this sim implements them (PostgreSQL-style MVCC)
===================================================================

Level             reads see                          commit rule
READ UNCOMMITTED  other txns' uncommitted writes     always commits
READ COMMITTED    latest committed, at each read     always commits
REPEATABLE READ   snapshot taken at BEGIN            abort if a row I write
                                                     changed since my snapshot
SERIALIZABLE      snapshot taken at BEGIN            same, plus abort on a
                                                     read-write dependency cycle

                 dirty read  non-repeatable  phantom   write skew
READ UNCOMMITTED  possible    possible        possible  possible
READ COMMITTED    no          possible        possible  possible
REPEATABLE READ   no          no              no*       possible
SERIALIZABLE      no          no              no        no

* The ANSI standard allows phantoms at REPEATABLE READ. Snapshot-based
  REPEATABLE READ (PostgreSQL) does not show them, because the re-run
  query reads the same snapshot. It still allows write skew.
  PostgreSQL treats READ UNCOMMITTED as READ COMMITTED; the RU row here
  shows what the standard permits.

Write skew: T1 and T2 both read {alice, bob}; T1 writes alice, T2 writes bob.
  T1 read what T2 wrote (rw edge T1 -> T2) and T2 read what T1 wrote
  (rw edge T2 -> T1). That cycle has no serial order, so SERIALIZABLE
  aborts whichever commits second. No row was written by both, so
  first-committer-wins never fires.

Drag ISOLATION and replay each scenario. The invariant strip has one rule per anomaly. At the default REPEATABLE READ only WRITE SKEW turns red. Drop to 1 and NON-REPEATABLE READ and PHANTOM turn red too. Drop to 0 and DIRTY READ joins them.

TUNE IT NOT YET

Alice and Bob are both on call. Each checks that at least two doctors are on call, then takes themself off. Start at 0 and raise ISOLATION one level at a time. Find the lowest level at which the hospital always keeps a doctor on call.

GOAL: at least one doctor is still on call after both transactions finish

FIX IT NOT YET

Implement read and commit for three levels. RC: read the latest committed version. RR: read the newest version with ts <= txn.start, and abort a commit if any key you write has a version newer than your start. SER: RR, plus abort if some transaction that committed after your start wrote a key you read and read a key you write. Return false to abort. The grader records reads into txn.reads for you.

Mental model:

each isolation level is a pair of rules: what a read sees and when a commit is refused. Snapshots fix everything about reads, so REPEATABLE READ looks safe. Write skew slips through because the conflict is between one transaction’s reads and another’s writes, and only SERIALIZABLE tracks reads.

IN PRODUCTION

PostgreSQL defaults to READ COMMITTED and implements REPEATABLE READ as snapshot isolation; its SERIALIZABLE is serializable snapshot isolation and raises serialization failures you must retry. MySQL InnoDB defaults to REPEATABLE READ, but a different design: plain reads use a snapshot, while UPDATE, DELETE and locking reads use the latest committed rows and do not abort on a concurrent write, so a lost update can commit. Oracle's “serializable” is snapshot isolation and allows write skew. The usual workarounds below SERIALIZABLE are SELECT ... FOR UPDATE on the rows you check, or a constraint the database enforces.

IN INTERVIEWS

“What is write skew and which isolation level prevents it?” Give the on-call example, say snapshot isolation allows it because the writes touch different rows, and name the fixes: SERIALIZABLE with retries, locking the rows you read with FOR UPDATE, or materializing the conflict into a row both transactions must write.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: MVCC →