Isolation Levels
How much of another transaction's mess can you see?
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.
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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- A ledger at scale: hot accounts, balances, shardingPayments
- Integration tests with a real databaseSoftware Testing and Quality