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.
Two transactions, one schedule, four levels. Step through and watch the same read return different answers.
This module needs JavaScript: it steps through two transactions rather than showing a finished picture.
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.
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.
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 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.
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.
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.
Answer without scrolling back up.
What is a non-repeatable read?
It is permitted at READ COMMITTED, where each statement sees the latest committed data - so a report reading a table twice can produce internally inconsistent totals.
How does SERIALIZABLE typically deliver its guarantee in a modern engine?
It lets transactions proceed optimistically and rejects one when no serial order explains the outcome. Code that assumes commit always succeeds fails intermittently under load.
Why is 'we use REPEATABLE READ' an incomplete description of behaviour?
The standard says which anomalies are permitted, not which are prevented, and engines prevent more than required in different ways. The engine matters as much as the level.
Step through read uncommitted, read committed, repeatable read and serializable with two concurrent transactions to see what each one allows.