Sub topic 8
Isolation Levels and What Each One Prevents
Assume you know basic SQL, primary keys, constraints, and that multiple users or processes can run database operations at the same time. This chapter uses PostgreSQL-style SQL, but exact behavior differs between database systems.
Why isolation matters
An isolation level controls what one transaction can observe while other transactions are running. Stronger isolation makes concurrent work appear more sequential, but it can also increase waiting, aborts, and retries.
Consider two transactions that read the same account balance and then write a new balance. If they overlap carelessly, one transaction can overwrite the other. Isolation rules reduce these concurrency anomalies.
The common isolation levels
| Isolation level | Dirty reads | Non-repeatable reads | Phantom reads | Typical cost |
|---|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible | Lowest |
| Read Committed | Prevented | Possible | Possible | Low |
| Repeatable Read | Prevented | Prevented | Database-dependent | Medium |
| Serializable | Prevented | Prevented | Prevented | Highest |
A dirty read occurs when a transaction sees data that another transaction has changed but not committed. If the other transaction rolls back, the first transaction saw a value that never became permanent.
A non-repeatable read occurs when the same row is read twice and another transaction commits a change between the reads.
A phantom read occurs when the same condition-based query returns a different set of rows because another transaction inserted, deleted, or changed matching rows.
Choosing a level
Read Committed is a common default. Each statement generally sees data committed before that statement began. Two statements in the same transaction can therefore see different committed values.
Repeatable Read keeps a stable view for the transaction in many multiversion concurrency control systems. PostgreSQL, for example, prevents ordinary phantom changes through its snapshot behavior. Other systems may follow the SQL standard more literally, so check your database documentation.
Serializable provides the strongest model. The database detects conflicts that could make concurrent transactions produce an invalid result. It may abort one transaction with a serialization failure. The application must retry the whole transaction.
-- PostgreSQL-style example
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) FROM doctors WHERE on_call;
UPDATE doctors
SET on_call = false
WHERE id = 1;
COMMIT;
-- If a concurrent transaction creates a conflicting result,
-- COMMIT can fail with a serialization error.Isolation is not a substitute for careful updates
Even at Read Committed, an atomic update often avoids a lost update:
UPDATE accounts
SET balance = balance - 20
WHERE id = 1
AND balance >= 20;Check the number of affected rows. A value of zero can mean that the account does not exist or that the balance was too small. For a read-then-write workflow, use SELECT ... FOR UPDATE or use Serializable isolation and retry failures.
> callout note > Isolation levels describe guarantees for concurrent transactions, not a promise that every transaction will commit. Stronger levels can reject work so the application can safely retry it.
Exercise
Which isolation level should an application choose when an invariant must remain true even if concurrent transactions update different rows?
Show answer
Use Serializable isolation, or enforce the invariant with suitable constraints and locking. The application must retry a transaction that fails with a serialization error.