Isolation Levels

Two transactions, one schedule, four levels. Step through and watch the same read return different answers.

Isolation Levels

This module needs JavaScript: it steps through two transactions rather than showing a finished picture.

Worth knowing

Isolation is a dial between correctness and concurrency. Higher levels forbid more anomalies and permit less parallelism.
Dirty read: seeing uncommitted data. Non-repeatable read: the same row changing mid-transaction. Phantom: new rows appearing.
Defaults differ. PostgreSQL and Oracle default to READ COMMITTED; MySQL's InnoDB defaults to REPEATABLE READ.
SERIALIZABLE does not queue everything. It detects conflicts and asks you to retry — so code using it must handle that error.

Overview

A transaction may run while other transactions read and write the same data. Isolation defines how those operations interact. The SQL standard names levels such as Read Committed, Repeatable Read, and Serializable, but engines implement them differently. A level's name alone does not tell you every guarantee.

In this guide
  1. The anomalies in plain terms
  2. Choose and handle conflicts
  3. A write-skew example
  4. Use constraints as the first line of defense
  5. Further reading

The anomalies in plain terms

A dirty read observes another transaction's uncommitted change. A non-repeatable read sees a row change between two reads in one transaction. A phantom is a new or removed row that changes the result of repeating a predicate query. A serialization anomaly occurs when concurrent transactions produce a result that cannot be explained by any serial order.

Serializable isolation aims to make concurrent transactions behave as if they ran one at a time. Some systems detect conflicts and abort a transaction, so applications must be ready to retry. Snapshot-based isolation can allow useful concurrency but may still permit anomalies such as write skew.

Choose and handle conflicts

Choose isolation based on the invariant the operation must preserve, not just a label. For a transfer or inventory reservation, consider using a constraint, atomic update, explicit lock, or serializable transaction as appropriate. Keep transactions short and handle serialization failures or deadlocks by retrying the complete transaction when safe. See transactions and ACID and deadlocks.

Test the target engine's actual behavior. SQLite serializes writes differently from PostgreSQL; its readers see snapshots while write concurrency is limited. PostgreSQL maps the standard levels to its MVCC implementation and documents the anomalies each level can permit.

A write-skew example

Two doctors are on call, and the rule says at least one must remain on call. Two concurrent transactions each read that both are on call, then each turns a different doctor off. Each transaction's individual update looks valid, but together the invariant is broken. This is write skew, which snapshot-style isolation can permit in some systems. A constraint, explicit locking strategy, or serializable isolation can prevent it. Choose based on the exact invariant and test concurrent schedules rather than reasoning from single-user examples.

Use constraints as the first line of defense

Some invariants are simpler to enforce with a unique, check, or foreign-key constraint than with a particular isolation level. For example, a unique constraint can prevent two concurrent transactions from claiming the same username. Constraints are checked by the database across all writers, while application checks can race. For rules involving several rows or external state, transactions and locks may still be necessary. Choose the narrowest database mechanism that expresses the invariant, then test concurrent updates and expected retries.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. What is a non-repeatable read?

  2. How does SERIALIZABLE typically deliver its guarantee in a modern engine?

  3. Why is 'we use REPEATABLE READ' an incomplete description of behaviour?

Cheat sheet

Isolation Levels

Step through read uncommitted, read committed, repeatable read and serializable with two concurrent transactions to see what each one allows.

DATABASE · vizlearn.in/database/isolation_levels.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.