# Isolation levels and anomalies in MySQL

> Dirty reads, non-repeatable reads, phantoms and lost updates are what isolation levels are about. See what each level allows and what InnoDB does by default.

Published 2026-11-25 · Language: en · Tags: mysql, innodb, transactions

Transactions run at the same time, and the database must decide how much of one transaction's work another is allowed to see. That decision is the isolation level. It is a trade between correctness and concurrency, so it helps to know exactly which surprises each level permits.

 

## The four anomalies

 

Isolation levels are defined by which of these they prevent.

 

- **Dirty read:** you read data another transaction has not committed. It may still roll back, so you acted on something that never existed.
- **Non-repeatable read:** you read the same row twice in one transaction and get different values because another transaction committed a change in between.
- **Phantom read:** you run the same range query twice and the second run returns extra rows that another transaction inserted.
- **Lost update:** two transactions read the same value, each computes a new one and writes it back, and one write silently overwrites the other.

 

The lost update is the one that hurts applications most, and it is not part of the textbook definitions of the four levels. It is a pattern of read-modify-write that you must prevent yourself.

 

## The four levels

 

The SQL standard names four levels, from weakest to strongest.

 

- **READ UNCOMMITTED:** dirty reads are possible. Almost never a good idea.
- **READ COMMITTED:** each statement sees only data committed before that statement began. No dirty reads, but non-repeatable reads and phantoms can happen. This is the default in PostgreSQL and Oracle.
- **REPEATABLE READ:** all consistent reads in a transaction see the same snapshot, taken at the first read. This is the **default in InnoDB**.
- **SERIALIZABLE:** transactions behave as if they ran one after another. In InnoDB plain `SELECT` statements become locking reads, so concurrency drops and deadlocks become more common.

 

## What InnoDB does at REPEATABLE READ

 

InnoDB's REPEATABLE READ is stronger than the standard requires: ordinary snapshot reads do not see phantoms, and locking reads use gap locks to stop other sessions inserting into ranges you have scanned. The details of those locks are the subject of a later article.

 

One trap deserves attention now. The snapshot protects *reads*, but writes always operate on the latest committed data. A session can therefore read a stale value and then update a row that has already changed.

 

## A lost update, step by step

 

Two sessions each withdraw from a wallet with a balance of 100, in the default isolation level.

 

```sql
START TRANSACTION;                              -- A begins
SELECT balance FROM wallets WHERE id = 1;       -- A sees 100
START TRANSACTION;                              -- B begins
SELECT balance FROM wallets WHERE id = 1;       -- B also sees 100
UPDATE wallets SET balance = 70 WHERE id = 1;   -- A writes 100 - 30
COMMIT;                                         -- A commits
UPDATE wallets SET balance = 50 WHERE id = 1;   -- B writes 100 - 50
COMMIT;                                         -- B commits
```

 

Both sessions saw 100 and computed a new balance in the application. The final balance is 50, but two withdrawals of 30 and 50 should leave 20. Raising the isolation level does not reliably fix this pattern, because the problem is the read-then-write logic. The fix is to make the check and the change one atomic statement.

 

```sql
UPDATE wallets
SET balance = balance - 30
WHERE id = 1 AND balance >= 30;
```

 

The statement reads the latest committed value under a row lock, so the second session waits for the first and then subtracts from 70. The application checks the number of affected rows: zero means the balance was insufficient.

 

## Choosing a level

 

- Keep **REPEATABLE READ** as the default unless you have a measured reason to change it. It gives stable reads inside a transaction, which makes reports and multi-step logic easier to reason about.
- Consider **READ COMMITTED** for write-heavy workloads that suffer from gap-lock contention and deadlocks, if the business logic tolerates seeing newer data between statements. Note that binary logging with this level needs row-based format.
- Use **SERIALIZABLE** rarely and for short transactions, or take explicit locks where you need them.
- Whatever the level, **design read-modify-write steps** with atomic updates, `SELECT ... FOR UPDATE` or optimistic version checks.

 

```mysql
SELECT @@transaction_isolation;
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
```

 

## Takeaway

 

Isolation levels answer "which anomalies may I see". They do not remove the need to design safe updates. Learn the four anomalies by name, remember that InnoDB defaults to REPEATABLE READ, and treat every read-then-write as a place where a lost update can hide.

Source: https://tdphat.io.vn/blog/isolation-levels-and-anomalies-in-mysql
