Two transactions run at the same time on the same data. What does each one see of the other? The answer is set by the isolation level, and it is one of the least understood settings in a database. Most bugs it causes never show up in tests, because tests rarely run two transactions at exactly the wrong moment. They show up in production, as a double booking or a balance that does not add up.
This article walks through the anomalies that concurrent transactions can produce, the four standard isolation levels, and how real databases implement them.
Why not always run fully isolated?
The safest behaviour is serializable: every set of concurrent transactions produces the same result as some one-at-a-time order. Databases can provide that, but it costs concurrency: more waiting on locks, or more transactions aborted and retried. So the SQL standard defines weaker levels, each allowing some anomalies in exchange for throughput.
To choose a level, you need to know the anomalies.
The anomalies, one by one
We will use two transactions, T1 and T2, and an accounts table.
Dirty read. T1 reads data that T2 has written but not yet committed.
- T2:
UPDATE accounts SET balance = 0 WHERE id = 1;(not committed) - T1:
SELECT balance FROM accounts WHERE id = 1;returns 0 - T2:
ROLLBACK;
T1 acted on a value that never officially existed.
Non-repeatable read. T1 reads the same row twice and gets different values, because T2 committed a change in between.
- T1: reads balance of account 1: 100
- T2: updates it to 50 and commits
- T1: reads it again: 50
Within one transaction, the "same" data changed under you.
Phantom read. T1 runs the same query with a condition twice and gets a different set of rows, because T2 inserted or deleted matching rows.
- T1:
SELECT COUNT(*) FROM bookings WHERE room = 7 AND day = '2026-12-01';returns 0 - T2: inserts a booking for room 7 on that day and commits
- T1: the same count now returns 1
No existing row changed; a new one appeared. That is why locking individual rows does not prevent phantoms.
Lost update. Two transactions read the same value, compute a new value in application code, and both write it back. The second write erases the first.
- T1 reads stock = 10, T2 reads stock = 10
- T1 writes stock = 9 and commits; T2 writes stock = 9 and commits
Two items sold, stock dropped by one.
Write skew. Two transactions read overlapping data, each checks a rule, and each updates a different row. Each change is valid on its own; together they break the rule. The classic example: a hospital requires at least one doctor on call. Two doctors are on call. Each, in a separate transaction, checks "is someone else on call?", sees yes, and takes themselves off. Both commit, and no one is on call.
The four standard levels
The SQL standard defines levels by which of the first three anomalies they must prevent:
| Level | Dirty read | Non-repeatable read | Phantom read |
|---|---|---|---|
| Read Uncommitted | possible | possible | possible |
| Read Committed | prevented | possible | possible |
| Repeatable Read | prevented | prevented | possible |
| Serializable | prevented | prevented | prevented |
The standard says what each level must prevent, not exactly what it allows, so real databases often prevent more than the minimum. Lost updates and write skew are not in the table at all, and that is where many surprises come from.
What real databases do
PostgreSQL uses MVCC: each transaction reads from a snapshot.
- Read Uncommitted behaves exactly like Read Committed; PostgreSQL never shows dirty data.
- Read Committed (the default) takes a new snapshot for each statement. So two
SELECTs in the same transaction can see different committed data. - Repeatable Read takes one snapshot for the whole transaction. That also prevents phantoms for plain reads. If T1 tries to update a row that T2 changed and committed after T1's snapshot, T1 fails with a serialization error and must retry, which prevents the lost update. Write skew is still possible: this level is snapshot isolation.
- Serializable uses Serializable Snapshot Isolation, which tracks read and write dependencies and aborts a transaction when the pattern could produce a non-serializable result. Write skew is prevented, at the cost of occasional retries.
MySQL InnoDB defaults to Repeatable Read. Plain SELECTs read from a consistent snapshot taken at the first read. Locking reads (SELECT ... FOR UPDATE) and updates read the latest committed data and take next-key locks, which lock gaps in the index to block inserts of phantom rows. Serializable turns plain SELECTs into locking reads when autocommit is off.
Other databases differ again, so check the documentation for yours. The names are standard; the exact behaviour is not.
Choosing and coding defensively
You do not always need Serializable. You need to know which anomalies would hurt your code.
-- Lost update: let the database compute the new value UPDATE products SET stock = stock - 1 WHERE id = 42 AND stock > 0; -- Or lock the row you read and will update BEGIN; SELECT stock FROM products WHERE id = 42 FOR UPDATE; UPDATE products SET stock = 9 WHERE id = 42; COMMIT; - Read Committed is fine for most simple request handlers, if you write updates as single atomic statements or lock what you read.
- Repeatable Read is useful for reports and exports that must see one consistent picture across many queries.
- Serializable is the simplest correct option for complex check-then-write logic (bookings, on-call rules, limits) where locking every row by hand is error-prone. Your code must retry on serialization failures.
- Constraints are often the cleanest answer: a
UNIQUE (room, day)index makes double booking impossible at any isolation level.
BEGIN ISOLATION LEVEL SERIALIZABLE; -- check and write COMMIT; -- may fail with a serialization error: retry the whole transaction Common mistakes
- Not knowing your default. PostgreSQL and SQL Server default to Read Committed, MySQL InnoDB to Repeatable Read. ORMs usually keep the database default.
- Assuming a transaction prevents lost updates. At Read Committed, read-modify-write in application code can lose updates even inside a transaction.
- Raising the level without adding retries. Higher levels mean more aborted transactions. Without a retry loop, users see errors.
- Running long transactions at high levels. They increase conflicts and, in MVCC databases, keep old row versions alive.
In short
- Anomalies: dirty read, non-repeatable read, phantom read, lost update, write skew.
- Levels from weakest to strongest: Read Uncommitted, Read Committed, Repeatable Read, Serializable.
- Real databases implement the same names differently; read your database's documentation.
- Use atomic updates,
FOR UPDATE, constraints, or Serializable with retries, depending on which anomaly threatens your logic.
Originally published at abdeldjalilchougui.biz.

