1.The four SQL isolation levels
An isolation level is a rule that decides how much one transaction can see of another transaction's uncommitted or concurrent work. The four standard levels — Read Uncommitted, Read Committed, Repeatable Read, Serializable — trade correctness for concurrency: each stricter level rules out more anomalies, at the cost of more locking or more retried transactions.
Key features
- Read Uncommitted: transactions can see other transactions' uncommitted changes (dirty reads) — rarely used in practice
- Read Committed: only ever sees committed data, but a re-read within the same transaction can see a different value (non-repeatable read) — PostgreSQL's and most databases' default
- Repeatable Read: a value read once stays the same for the rest of the transaction, but new rows matching a filter can still appear (phantom reads)
- Serializable: transactions behave as if run one at a time in some order — the strongest level, at the highest cost in locking or retries
- Each level up removes more anomalies but increases lock contention or the chance a transaction must retry after a conflict
Isolation levels and the anomalies each one prevents
- Txn A → accounts: SELECT balance (reads 100)
- Txn B → accounts: UPDATE + COMMIT (balance = 150)
- Txn A → accounts: SELECT balance again (reads 150 — non-repeatable read)
Command
-- set for one transaction (PostgreSQL / MySQL syntax)
BEGIN;
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- ... queries ...
COMMIT;Four levels, each removing more anomalies: Read Uncommitted (dirty reads possible) → Read Committed (default in most databases) → Repeatable Read (a value stays stable) → Serializable (behaves like one transaction at a time).