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.
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.
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.
| Engine and level | What happened | Final |
|---|---|---|
| PostgreSQL, READ COMMITTED (default) | Second write succeeded on a stale read | 120, 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 read | 120, a deposit lost |
| MySQL, SELECT ... FOR UPDATE | Second session waited, then read 110 | 130, correct |
| MongoDB, transactions | Write conflict on the second update | 110, retry needed |
## 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 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 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 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 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)
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.
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.
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.
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.
## 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 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 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 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)
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.
The same mechanism, concurrent writes and isolation, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. What is PostgreSQL's default isolation level?
MySQL's InnoDB defaults to REPEATABLE READ.
2. MySQL at REPEATABLE READ, two read-then-write deposits on one row. What did the run show?
An InnoDB UPDATE reads the latest committed row, not the snapshot, so the stale value overwrote the first deposit.
3. Which run stopped write skew?
It failed the second commit with a serialization error. The application must retry.
4. What is the simplest fix for a lost update on a counter?
The engine applies it to the current value, so there is no stale read to lose.
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.