Transaction isolation levels
Two transactions touch the same bank balance at the same moment. The four isolation levels decide what the reader sees, from anything at all to a strictly ordered view.
In 30 seconds
When transactions run at the same time, one can see another’s half-finished or freshly changed data. The isolation level sets how much of that is allowed.
SQL defines four. Read uncommitted hides nothing. Read committed hides uncommitted changes. Repeatable read also keeps the rows you have read from changing. Serializable behaves as if transactions ran one at a time.
Stricter levels cost speed: more waiting, more retries. That is why most databases default to a middle level.
Transaction isolation levels, step by step
The table is the SQL standard’s minimum; engines are often stricter. PostgreSQL never allows dirty reads, and its repeatable read also blocks phantoms. Oracle offers only read committed and serializable.
- Step 1: Two transactions at once. A transaction is a group of statements that takes effect all together (commit) or not at all (rollback). Here T1 reads the accounts table while T2 is changing it. The isolation level decides how much of T2’s work T1 is allowed to see.
- Step 2: Read uncommitted. The loosest level hides nothing. T2 changes Ana’s balance to 40 but has not committed. T1 reads 40 anyway, and then T2 rolls back. T1 is left holding a number that was never really in the database: a dirty read.
- Step 3: Read committed. One level up, T1 sees only committed data, so the pending 40 stays invisible and the dirty read is gone. But each statement sees the latest commits: once T2 commits, the same query inside the same transaction returns a different answer.
- Step 4: Repeatable read. At repeatable read, a row T1 has read keeps its value until T1 ends. Many engines do this by giving T1 a snapshot: a frozen view of the data as it was when T1 started reading. T2’s commit is real, but T1 does not see it.
- Step 5: Phantoms remain. The standard’s repeatable read protects rows T1 has already read, not rows that do not exist yet. T1 counts accounts holding at least 50 and gets 3. T2 inserts Dee with 90 and commits. T1 counts again: 4. The newcomer is a phantom.
- Step 6: Serializable. The strictest level guarantees the outcome is one that could have come from running the transactions one after another. Here T2’s insert is held back until T1 commits. Other engines let both run, then abort one, which must be retried.
- Step 7: The ladder and its price. Each level rules out one more anomaly, and costs more: locks are held longer, and transactions wait or are aborted and retried more often, so fewer get through per second. Most databases therefore default to a middle level, not the strictest.
Try it yourself
Learn this first
Learn next
- Dirty reads, phantom reads and anomaliesFour ways two overlapping transactions can show each other wrong data. Intermediate
- Consistency models (strong vs eventual)What a read may return after a write when the data lives on three machines. Intermediate
- Two-phase commitA coordinator asks two databases to vote, then commit or abort together. Intermediate
Spotted a mistake on this page?