Transaction Isolation
Read committed · Snapshot · Serializable
Written by Darjus Kazakevic · reviewed
What one transaction may see of another, and which bugs each level lets through.
Many transactions run at once. Isolation decides how much they can see of each other's unfinished or just-finished work. Full isolation, called serializable, means the result is the same as if they had run one after another. Weaker levels let some interleavings through in exchange for less blocking and fewer aborts.
The SQL standard names four levels, but real engines implement them differently. Postgres defaults to read committed. MySQL's InnoDB defaults to repeatable read. Oracle's "serializable" is really snapshot isolation. The names are less useful than the question: which anomalies can my default produce, and which of my transactions care?
In essence — An isolation level is a list of anomalies the database promises to prevent. Know your engine's default, fix lost updates with atomic writes or locks, and retry when serializable aborts.
When to reach for it
Reach for it when
- Two requests can change the same row or the same rule at once
- Money, stock, seats or quotas, where a lost update is a real loss
- Reports that must read several tables at one point in time
Reconsider when
- Raising every transaction to serializable by default without retries in place
- Single-statement atomic updates, which are already safe at read committed
- Coordinating across services: isolation stops at one database
The anomalies and the levels
- Dirty read and non-repeatable read (Fuzzy read)
- A dirty read sees another transaction's uncommitted write, which may later roll back. A non-repeatable read reads the same row twice and gets two values, because someone committed in between.
- Phantom
- A query by condition returns a different set of rows the second time, because another transaction inserted or deleted a matching row. Locking the rows you read does not help, because the new row did not exist yet.
- Lost update
- Two transactions read the same value, each changes it in application code, and each writes it back. The second write silently replaces the first. This is the most common isolation bug in web apps.
- Write skew
- Two transactions read the same set of rows, check a rule, and each write a different row. Each write is fine alone; together they break the rule. Snapshot isolation allows it.
- Snapshot isolation and MVCC (Repeatable read in Postgres)
- The database keeps several versions of each row (MVCC, multi-version concurrency control). Each transaction reads from one consistent snapshot taken at its start, so readers never block writers. It prevents dirty reads, non-repeatable reads and phantoms, but not write skew.
- Serializable (SSI · Two-phase locking)
- Every anomaly is prevented. Postgres does it with SSI: transactions run on snapshots, and the database aborts one when it detects a dangerous pattern of reads and writes. MySQL and SQL Server do it mostly with locks, which block instead.
Design rules
- Know your default, and write it down
- Postgres, SQL Server and Oracle default to read committed; InnoDB defaults to repeatable read. Check the setting your driver or pool actually uses, and note in the code which transactions rely on something stronger.
- Fix lost updates in the write, not the read
- Prefer an atomic statement such as update stock set n = n - 1 where n > 0. When the logic must live in code, lock the row with select … for update, or add a version column and update where version matches.
- Guard invariants that span rows
- A rule like "at least one doctor on call" or "no two bookings overlap" needs serializable, a for update on every row the check reads, or a constraint. Postgres exclusion constraints stop overlapping ranges in the database itself.
- Retry the whole transaction on a serialization failure
- Serializable and Postgres repeatable read abort with SQLSTATE 40001; deadlocks give 40P01. Roll back, wait a jittered moment and rerun everything from the first read. Keep emails and API calls outside the retried block.
Common pitfalls
- Assuming repeatable read means the same thing in Postgres and MySQL. InnoDB still loses a read-then-write update.
- Retrying only the failed statement instead of the whole transaction, so the retry uses values read from the old snapshot.
- Sending an email inside a retried transaction, so a conflict sends it twice.
- Using for update to stop phantoms. It locks rows that exist, not the row that is about to be inserted.