Learn Labs
Transactions & Concurrency

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

Lost 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:

1. what ORMs often do: read, compute in the app, write backn = SELECT n FROM counter WHERE id = 1UPDATE counter SET n = <n + 1> WHERE id = 12. let the database do the arithmeticUPDATE counter SET n = n + 1 WHERE id = 13. lock the row while decidingSELECT n FROM counter WHERE id = 1 FOR UPDATE4. same as 1, but REPEATABLE READ, retrying on serialization errors
read, then write (READ COMMITTED)50 of 1,000 kept, 950 lostUPDATE n = n + 11,000, 0 retries, 1.19 sSELECT … FOR UPDATE1,000, 0 retries, 1.23 sREPEATABLE READ + retry1,000, 8,635 retries, 2.94 s

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 + 1 is atomic: the second UPDATE waits 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 UPDATE keeps 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 0

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

alice’s transactiondatabasebob’s transactioncount on call → 2count on call → 2alice off call; COMMITbob off call; COMMITREPEATABLE READ: both commit (50/50)SERIALIZABLE: 40001 for one (50/50)
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 error

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

On this page