Home›Learn›Lost updates and write skew
Course · TransactionsLesson 11 of 330 read
Isolation · two sessions, one row

Lost updates and write skew

Can two correct transactions produce a wrong result?

You will learn: the two concurrency bugs that isolation levels exist to stop, what each engine actually did with them at its default setting, and the fixes.

Isolation levels are usually taught as a table of anomalies. I find it easier to watch them. Every transcript on this page is from Notebook 4, run 2026-11-18 04:27 UTC: two real sessions, interleaved one step at a time, on PostgreSQL 18, MySQL 8.4 and MongoDB 8.0.

Anomaly 1

Lost update: two deposits, one survives

Two sessions read the same balance, 100, and each writes back its own result: 110 and 120. Both deposits should count, for 130. The read-then-write pattern is what loses one.

Which engines keep both deposits, at their default settings?

None of them. PostgreSQL at READ COMMITTED and MySQL at REPEATABLE READ silently kept 120. PostgreSQL at REPEATABLE READ and MongoDB in transactions refused the second write with an error, which keeps 110 and hands the retry to the application.

Lost update, step by step

PostgreSQL at READ COMMITTED and MySQL at REPEATABLE READ, the defaults. Both ran exactly this sequence in Notebook 4.

Session AThe row: balanceSession BSELECT balance -> 100100SELECT balance -> 100100UPDATE balance = 110; COMMIT110UPDATE balance = 120; COMMIT120final: 120, expected 130
Engine and levelWhat happenedFinal
PostgreSQL, READ COMMITTED (default)Second write succeeded on a stale read120, a deposit lost
PostgreSQL, REPEATABLE READ"could not serialize access due to concurrent update"110, retry needed
MySQL, REPEATABLE READ (default)Second write succeeded on a stale read120, a deposit lost
MySQL, SELECT ... FOR UPDATESecond session waited, then read 110130, correct
MongoDB, transactionsWrite conflict on the second update110, retry needed

PostgreSQL, READ COMMITTED

## PostgreSQL 18, lost update, READ COMMITTED (the default)
  A: SELECT balance -> 100
  B: SELECT balance -> 100
  A: UPDATE balance = 110; COMMIT
  B: UPDATE balance = 120; COMMIT -> ok
  final balance: 120  (expected 130 if both deposits count)

PostgreSQL, REPEATABLE READ

## PostgreSQL 18, lost update, REPEATABLE READ
  A: SELECT balance -> 100
  B: SELECT balance -> 100
  A: UPDATE balance = 110; COMMIT
  B: UPDATE balance = 120 -> ERROR: could not serialize access due to concurrent update
  final balance: 110  (expected 130 if both deposits count)

MySQL, REPEATABLE READ

## MySQL 8.4, lost update, REPEATABLE READ (the default)
  A: SELECT balance -> 100
  B: SELECT balance -> 100
  A: UPDATE balance = 110; COMMIT
  B: UPDATE balance = 120; COMMIT -> ok
  final balance: 120  (expected 130 if both deposits count)

MySQL, with SELECT ... FOR UPDATE

## MySQL 8.4, lost update fixed with SELECT ... FOR UPDATE
  A: SELECT balance FOR UPDATE -> 100
  B: SELECT balance FOR UPDATE -> still waiting after 1.5 s: True
  A: UPDATE balance = 110; COMMIT
  B: ... the SELECT FOR UPDATE returns -> 110
  B: UPDATE balance = 130; COMMIT -> ok
  final balance: 130  (expected 130 if both deposits count)

MongoDB, two transactions

## MongoDB 8.0, lost update, inside two transactions
  A: findOne -> balance 100
  B: findOne -> balance 100
  A: updateOne balance = 110; commitTransaction
  B: updateOne balance = 120 -> OperationFailure: Caused by :: Write conflict during plan execution and yielding is disabled. :: Please retry your operation or multi-document transaction.
  final balance: 110  (expected 130 if both deposits count)
Follow it

Two sessions, one row

The same collision in PostgreSQL and InnoDB, one step at a time. Then tick the box: PostgreSQL shows what REPEATABLE READ changes, and MySQL shows what one forgotten transaction does to everyone behind it.

Two sessions update the same account at the same moment, in PostgreSQL's default isolation level. What happens to the second one?

It waits for the first to commit, then re-reads the row. With an absolute value it still overwrites the first deposit, so one is lost. A relative update (SET balance = balance + 10) is what keeps both. In REPEATABLE READ it would get an error instead and have to retry.

Anomaly 2

Write skew: two doctors go off call

The rule: at least one doctor on call. Alice and Bob each check that two are on call, then each takes themselves off. They update different rows, so nothing conflicts at the row level, and the rule breaks.

Which engine and level stops this?

Only PostgreSQL at SERIALIZABLE. It tracks what each transaction read, notices the two depend on each other, and fails the second commit. Every other run ended with nobody on call.

Write skew, step by step

The rule: at least one doctor on call. Each session checks it, then updates a different row. Notebook 4 ran it in all three engines.

Session ADoctors on callSession Bcount on call -> 22 oncount on call -> 22 onUPDATE alice: off call1 onUPDATE bob: off call0 onboth commit: nobody on callSERIALIZABLE: B fails instead

PostgreSQL, REPEATABLE READ

## PostgreSQL 18, write skew, REPEATABLE READ
  A: SELECT count(*) WHERE on_call -> 2
  B: SELECT count(*) WHERE on_call -> 2
  A: UPDATE alice off call
  B: UPDATE bob off call
  A: COMMIT -> ok
  B: COMMIT -> ok
  doctors on call at the end: 0  (the rule was: at least 1)

PostgreSQL, SERIALIZABLE

## PostgreSQL 18, write skew, SERIALIZABLE
  A: SELECT count(*) WHERE on_call -> 2
  B: SELECT count(*) WHERE on_call -> 2
  A: UPDATE alice off call
  B: UPDATE bob off call
  A: COMMIT -> ok
  B: COMMIT -> ERROR: could not serialize access due to read/write dependencies among transactions
  doctors on call at the end: 1  (the rule was: at least 1)

MySQL, REPEATABLE READ

## MySQL 8.4, write skew, REPEATABLE READ (the default)
  A: SELECT COUNT(*) WHERE on_call -> 2
  B: SELECT COUNT(*) WHERE on_call -> 2
  A: UPDATE alice off call
  B: UPDATE bob off call
  A: COMMIT -> ok
  B: COMMIT -> ok
  doctors on call at the end: 0  (the rule was: at least 1)

MongoDB, two transactions

## MongoDB 8.0, write skew, inside two transactions
  A: countDocuments({on_call: true}) -> 2
  B: countDocuments({on_call: true}) -> 2
  A: updateOne alice off call
  B: updateOne bob off call
  A: commitTransaction -> ok
  B: commitTransaction -> ok
  doctors on call at the end: 0  (the rule was: at least 1)
The fixes

What I reach for, in order

  1. Make the write relativeUPDATE accounts SET balance = balance + 10 WHERE id = 1, or $inc in MongoDB. The engine applies it to the current value, so there is no stale read to lose. This fixes lost updates, not write skew.
  2. Lock what you readSELECT ... FOR UPDATE in PostgreSQL and MySQL. The second session waits, as the MySQL run above shows. For write skew, lock every row the rule depends on, here both doctors.
  3. Check a version on writeKeep a version column, and write UPDATE ... SET version = version + 1 WHERE id = 1 AND version = 7. Zero rows updated means someone else got there first. This works the same way in all three engines.
  4. Use SERIALIZABLE and retryIn PostgreSQL, SERIALIZABLE caught the write skew. The price is that the application must retry failed transactions, and it must have a retry loop before you switch it on.

Steps 1, 3 and 4 are the standard fixes, and only the FOR UPDATE fix in MySQL and SERIALIZABLE in PostgreSQL are part of the notebook run so far. For the theory behind these anomalies, the transactions chapter of Designing Data-Intensive Applications is where I would start, and the MVCC lab shows why the snapshot is taken at the first query.

Connected

Where this shows up

The same mechanism, concurrent writes and isolation, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 4 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. What is PostgreSQL's default isolation level?

2. MySQL at REPEATABLE READ, two read-then-write deposits on one row. What did the run show?

3. Which run stopped write skew?

4. What is the simplest fix for a lost update on a counter?

Part 1 of 4 · Learn · 23 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.