Isolation Lab: Lost Updates and Write Skew
Twenty threads, one counter, two doctors on call: what each isolation level actually prevents
Isolation levels are easy to recite and hard to feel. labs/isolation.py
runs the classic anomalies against the primary and counts how often they
happen.
docker compose -f compose_prod.yaml run --rm labs python isolation.pyLost updates: 20 threads, one counter
Twenty threads each add 1 to a counter 50 times. The right answer is 1,000. Four ways to write the increment:
Read-then-write under READ COMMITTED kept 50 of 1,000. That's not a rounding error: the threads start together and run in lockstep, so in each round all 20 read the same value, all 20 write value + 1, and 19 increments vanish. Fifty rounds, 50 increments survive. Real traffic is less synchronized, so the loss is smaller and much harder to notice, but it's the same bug.
The three fixes all reach 1,000, at different costs:
n = n + 1is atomic: the secondUPDATEwaits for the first to commit, then re-reads the row and adds to the new value. Fastest (1.19 s) and the right default.FOR UPDATEkeeps the read-then-write shape but makes the readers queue. Same correctness, about the same speed (1.23 s).- REPEATABLE READ made 8,635 transactions fail with
could not serialize access due to concurrent update, which the application had to retry. Correct, but 2.5 times slower and only correct because the lab retries. An application that doesn't retry turns these into errors for users.
A non-repeatable read
READ COMMITTED first read 0, second read 42
REPEATABLE READ first read 0, second read 0Between two SELECTs in the same transaction, another session committed
n = 42. READ COMMITTED takes a new snapshot for every statement, so the
second read sees it. REPEATABLE READ keeps the snapshot from the first
statement for the whole transaction.
Write skew: nobody on call
The rule: at least one doctor must be on call. Alice and Bob are both on call, and both try to go off call at the same moment. Each transaction checks "is anyone else on call?" (yes, 2), then takes itself off.
REPEATABLE READ 50/50 trials left nobody on call; 0 transactions failed
SERIALIZABLE 0/50 trials left nobody on call; 50 transactions failed with a serialization errorUnder REPEATABLE READ the two transactions update different rows, so
there's no conflict for Postgres to detect: both commit, every time, and
the rule is broken in 50 of 50 trials. SERIALIZABLE tracks what each
transaction read as well (serializable snapshot isolation) and sees
that each one's decision depended on a row the other changed. One of the
two fails with SQLSTATE 40001 in every trial; a retry then sees only one
doctor on call and correctly refuses.
SERIALIZABLE is not free (it tracks reads and aborts more transactions),
but it's the only level that makes "check, then act" logic safe without
thinking about every interleaving. If you use it, every transaction
needs a retry loop for 40001. If you don't, enforce the rule with a
lock (SELECT ... FOR UPDATE on the rows the check reads) or a
constraint.