Databases / Dirty reads, phantom reads and anomalies

Dirty reads, phantom reads and anomalies

Two transactions share a three-row stock table. Step by step, four classic ways they can trip each other up: dirty read, non-repeatable read, phantom read and lost update.

Intermediate7 steps~7 min
T1 — one transaction, on the top lane
T2 — the other, on the bottom lane
Uncommitted — written, but could still be rolled back
Read — the rows a transaction looked at
// THE SHORT VERSION

In 30 seconds

When two transactions run at the same time, one can see or overwrite the other’s work in ways that could never happen if they took turns. These surprises are called anomalies.

A dirty read sees a change that was never committed. A non-repeatable read sees a row change between two looks. A phantom read finds a new row in a repeated search. A lost update overwrites someone else’s write.

Databases prevent them with isolation levels: the stricter the level, the more anomalies it stops.

Dirty reads, phantom reads and anomalies, step by step

STEPS01 / 07

Levels as the SQL standard defines them; engines are often stricter. PostgreSQL’s repeatable read stops phantoms and lost updates; MySQL’s stops phantoms in plain reads but not lost updates.

STEP 01 / 07 Two transactions, one table
THE MATH
T1 and T2 overlap in time items: pen 5, ink 2, cup 0
  1. Step 1: Two transactions, one table. A transaction is a group of statements that takes effect all together (commit) or not at all (rollback). When two run at once on the same rows, one can catch the other half-way. A read anomaly is a result that could not occur if they ran one after the other.
  2. Step 2: Dirty read. T2 sets the pen stock to 0 but has not committed. T1 reads the row and sees 0. Then T2 rolls back, and the stock is 5 again. T1 has read a value that, officially, never existed. That is a dirty read.
  3. Step 3: Non-repeatable read. T1 reads the pen stock: 5. T2 changes it to 3 and commits, so this time the change is real. T1 reads the same row again and gets 3. Nothing dirty was read, yet one transaction saw two values for one row: a non-repeatable read.
  4. Step 4: Phantom read. T1 asks for every item in stock and gets two rows, pen and ink. T2 inserts a new item, cap, and commits. T1 runs the same search again and gets three rows. No row it read has changed; a new one has appeared. That newcomer is a phantom.
  5. Step 5: Lost update. Both transactions read the pen stock, 5. T1 sells one and writes 4. T2 sells two, still working from the 5 it read, and writes 3. Three pens were sold, so the stock should be 2. T1’s update has been silently overwritten: a lost update.
  6. Step 6: Keeping both updates. The lost update came from reading first and writing later. Let the database do both in one statement, stock = stock - 1, and it locks the row while it works. T2 waits for T1 to commit, then subtracts from 4, not from the stale 5. SELECT … FOR UPDATE gives the same protection.
  7. Step 7: Which level stops which. Isolation levels are defined by the anomalies they rule out. Read committed stops dirty reads, repeatable read also stops non-repeatable reads, and serializable stops them all. Lost updates are not in the SQL standard’s table; at repeatable read the answer depends on the database.
VALUES SO FAR
penstock 5start
dirty····anomaly
repeat····anomaly
phantom····anomaly
lost····anomaly
atomic····fix
safest····level

Spotted a mistake on this page?