Sub topic 6
The Four SQL Standard Isolation Levels
This chapter assumes you can write basic SQL and use BEGIN, COMMIT, and ROLLBACK. You should also know that several clients can run transactions at the same time.
Transaction isolation is the I in ACID. It controls what one transaction can observe while other transactions read or change data. Stronger isolation makes concurrent work appear more like one transaction at a time, but it can reduce throughput and cause more waiting or retries.
The anomalies
An anomaly is an unexpected result caused by interleaved transactions.
- A dirty read happens when a transaction reads data another transaction changed but has not committed. If the writer rolls back, the reader saw data that never really existed.
- A non-repeatable read happens when the same query returns different values during one transaction because another transaction committed an update between the two reads.
- A phantom read happens when repeating a condition-based query returns a different set of rows because another transaction inserted or deleted matching rows.
- A lost update happens when two transactions read the same value and later overwrite each other’s changes. The classic SQL standard table does not use this anomaly to define the four levels, so applications often need an explicit atomic update or lock.
The standard levels
The levels are cumulative: each stronger level prevents at least the anomalies prevented by the levels below it.
| Isolation level | Dirty reads | Non-repeatable reads | Phantom reads | Typical concurrency |
|---|---|---|---|---|
| READ UNCOMMITTED | Allowed | Allowed | Allowed | Highest |
| READ COMMITTED | Prevented | Allowed | Allowed | High |
| REPEATABLE READ | Prevented | Prevented | Allowed by the standard | Medium |
| SERIALIZABLE | Prevented | Prevented | Prevented | Lowest |
READ UNCOMMITTED
READ UNCOMMITTED provides the weakest guarantee. A transaction may read another transaction’s uncommitted changes. This can avoid waiting, but reports and decisions can use values that later disappear.
It is rarely appropriate for correctness-sensitive work. Some database engines do not provide true dirty reads even when this level is requested.
READ COMMITTED
READ COMMITTED prevents dirty reads. Each statement sees data committed before that statement begins. Therefore, two identical SELECT statements in one transaction can see different committed values.
This level is common because it gives useful consistency without holding a transaction to one database-wide snapshot. It does not by itself prevent lost updates.
REPEATABLE READ
REPEATABLE READ prevents dirty reads and non-repeatable reads. A row read once keeps the same visible version when the transaction reads it again.
Under the SQL standard, a new matching row can still appear in a repeated range query, so phantoms are allowed. Many real engines implement this level using snapshots that provide stronger behavior than the standard requires. Do not assume that behavior transfers between databases.
SERIALIZABLE
SERIALIZABLE gives the strongest standard guarantee. The committed result must be equivalent to running the transactions one at a time in some order. The database may block a transaction or abort it with a serialization error to preserve that guarantee.
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT balance
FROM accounts
WHERE id = 1;
UPDATE accounts
SET balance = balance - 10
WHERE id = 1;
COMMIT;An application using this level must usually retry a transaction that fails because of a serialization conflict.
Choosing among the levels
Start with READ COMMITTED when each statement can make a fresh, committed decision. Choose REPEATABLE READ when several reads must use a consistent view. Choose SERIALIZABLE when correctness depends on transactions behaving as if they ran sequentially and you can implement retries. Use READ UNCOMMITTED only when approximate, possibly inconsistent results are acceptable.
Exercise
A transaction reads an account balance twice. Another transaction commits an update between the reads. Which standard level first prevents the second read from changing?
Show answer
REPEATABLE READ. READ COMMITTED prevents dirty reads but allows non-repeatable reads.