Isolation levels define which effects of concurrent transactions are allowed to become visible. Stronger isolation generally provides simpler correctness guarantees, but it can reduce concurrency or increase retries, locking and resource usage.
The SQL standard defines four commonly discussed levels: Read Uncommitted, Read Committed, Repeatable Read and Serializable.
The Four Standard Isolation Levels
| Isolation level | Dirty reads | Non-repeatable reads | Phantom reads |
|---|
| Read Uncommitted | Possible | Possible | Possible |
| Read Committed | Prevented | Possible | Possible |
| Repeatable Read | Prevented | Prevented | Possible under the SQL standard |
| Serializable | Prevented | Prevented | Prevented |
These columns describe the standard's classic phenomena, not every possible concurrency anomaly. In particular, lost updates and write skew need separate consideration, and real database engines may implement stronger behavior than the minimum standard requires.
1. Read Uncommitted
This is the weakest isolation level. A transaction may be allowed to read changes made by another transaction before those changes are committed. This is known as a dirty read.
Imagine a transaction changing an account balance from €500 to €50. Before it commits, another transaction reads €50 and makes a decision based on that value. The first transaction then rolls back, meaning the second transaction acted on data that never became permanent.
Dirty reads can lead to incorrect decisions and are rarely acceptable for business-critical operations.
Some database engines do not provide true dirty reads. For example, PostgreSQL treats READ UNCOMMITTED as READ COMMITTED.
2. Read Committed
A transaction can read only committed data. However, if it reads the same row twice, another transaction may commit a change between those reads.
This is a non-repeatable read.
For example:
- Transaction A reads a product price of €100.
- Transaction B changes the price to €120 and commits.
- Transaction A reads the same product again and sees €120.
Both reads are valid under Read Committed, but Transaction A did not see a stable value throughout its transaction.
Read Committed is a common default isolation level. It works well for many workloads, provided the application handles concurrent updates and protects important business rules explicitly.
3. Repeatable Read
Repeatable Read provides a more stable view of data during a transaction. A row read once should not appear to change when read again within that transaction.
Under the SQL standard, phantom rows can still be possible. A phantom read occurs when a query executed again returns a different set of rows because another transaction inserted or deleted rows matching the query condition.
For example, Transaction A runs:
SELECT *
FROM products
WHERE stock_quantity < 5;
Transaction B inserts another product with a stock quantity of 2 and commits. Depending on the database's implementation, Transaction A may see that new row if it repeats the query.
Database engines differ here. Some implement Repeatable Read using snapshot-based mechanisms that prevent classic phantom reads, while other anomalies may still be possible. Always check the behavior of the database you deploy.
4. Serializable
Serializable is the strongest standard isolation level. Its goal is to make concurrent transactions behave as though they had executed one at a time in some serial order.
This is useful when correctness depends on a rule involving multiple rows or queries, not just one row update.
The trade-off is that the database may block transactions or abort one when it detects a conflict. Applications using Serializable must be prepared to retry transactions that fail because of serialization conflicts.
Stronger isolation does not mean every transaction will simply wait politely. Some databases detect that a safe serial execution is impossible and abort a transaction so the application can retry it.