Difficulty: Advanced
Describe the four standard SQL isolation levels and which read anomalies each one prevents. What do popular databases use by default?
The SQL standard defines four isolation levels, from weakest to strongest: Read Uncommitted, Read Committed, Repeatable Read and Serializable. Each one rules out more concurrency anomalies: Read Committed prevents dirty reads, Repeatable Read also prevents non-repeatable reads, and Serializable also prevents phantom reads and every other anomaly, making concurrent transactions behave as if they ran one after another. Stronger levels are safer but cost more in locking, aborts or throughput.
SELECT COUNT(*) FROM bookings WHERE slot = '10am' and gets 4; T2 inserts a fifth booking and commits; T1 re-runs and gets 5. The rows matching a predicate changed.| Level | Dirty read | Non-repeatable read | Phantom read |
|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible |
| Read Committed | Prevented | Possible | Possible |
| Repeatable Read | Prevented | Prevented | Possible (per standard) |
| Serializable | Prevented | Prevented | Prevented |
40001. Read Uncommitted behaves like Read Committed, so dirty reads never happen.SELECTs read from a consistent snapshot; locking reads (SELECT ... FOR UPDATE) use next-key locks that block inserts into the gap, which prevents most phantoms.READ_COMMITTED_SNAPSHOT; Oracle's "Serializable" is really snapshot isolation.Older engines used locking: shared locks for reads, held longer at higher levels. Modern engines mostly use MVCC: each transaction reads a snapshot, so readers do not block writers. Read Committed takes a new snapshot per statement; Repeatable Read keeps one snapshot for the whole transaction. Serializable adds either predicate/range locks or conflict detection.
Most applications run at Read Committed and protect critical sections explicitly with SELECT ... FOR UPDATE, atomic updates (SET stock = stock - 1 WHERE stock > 0) or optimistic version checks. Use Serializable for logic with complex invariants (seat booking, double-entry ledgers) and be ready to retry on serialization failures.
-- Session A
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT price_paise FROM products WHERE id = 1; -- 49900
-- Session B (autocommit)
UPDATE products SET price_paise = 54900 WHERE id = 1;
-- Session A again
SELECT price_paise FROM products WHERE id = 1; -- 54900 (changed!)
COMMIT;
-- Repeat with a stronger level
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT price_paise FROM products WHERE id = 1; -- 54900
-- Session B updates to 59900 and commits
SELECT price_paise FROM products WHERE id = 1; -- still 54900
COMMIT;
Read Committed takes a fresh snapshot for each statement, so committed changes appear mid-transaction. Repeatable Read pins one snapshot for the whole transaction.
import psycopg
from psycopg import errors
def book_seat(conn, show_id, seat):
for attempt in range(3):
try:
with conn.transaction():
conn.execute("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE")
taken = conn.execute(
"SELECT 1 FROM bookings WHERE show_id=%s AND seat=%s",
(show_id, seat)).fetchone()
if taken:
return False
conn.execute("INSERT INTO bookings(show_id, seat) VALUES (%s, %s)",
(show_id, seat))
return True
except errors.SerializationFailure:
continue # SQLSTATE 40001: safe to retry
raise RuntimeError("too much contention")
Serializable transactions can abort under contention, so the application must retry the whole transaction rather than treat the error as fatal.
Read Committed, Repeatable Read, Serializable, Dirty Read, Phantom Read, MVCC