4.4.17Databases

Transaction isolation levels — READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE

2,145 words10 min readdifficulty · medium5 backlinks

WHY do isolation levels exist?

The "gold standard" is serializability: the concurrent result must equal some serial execution order. But locking everything to guarantee that is slow. So the SQL standard defines 4 levels, each banning a specific set of read anomalies.

The three classic anomalies (this is the 20% that explains 80%)


The 4 levels and what each BANS

Figure — Transaction isolation levels — READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE

HOW it's implemented (so the table isn't magic)


Forecast-then-Verify drill

Recall Before reading: predict the outputs

Two transactions at READ COMMITTED. T1 reads bal (=100). T2 commits bal=200. T1 reads again. Forecast: second read = ? · Is this an anomaly, and which?

Verify: second read = 200. This is a non-repeatable read — allowed at READ COMMITTED, banned at REPEATABLE READ.


Common mistakes (Steel-man → fix)



Recall Feynman: explain to a 12-year-old

Imagine the database is a shared notebook and people scribble in it at the same time.

  • READ UNCOMMITTED: you read someone's pencil scribble before they decide to keep it — they might erase it. Risky.
  • READ COMMITTED: you only read things people have inked in for real. But if you look at the same line twice, someone might have re-inked it in between, so it changes.
  • REPEATABLE READ: you take a photo of your page at the start and read from the photo, so lines you already looked at never change under you.
  • SERIALIZABLE: it's as if everyone takes strict turns — the final notebook looks exactly like if people went one by one. No surprises at all, but you wait more.

Flashcards

What are the three classic read anomalies?
Dirty read, non-repeatable read, phantom read.
Which isolation level allows dirty reads?
READ UNCOMMITTED (only).
Difference between non-repeatable read and phantom read?
Non-repeatable = an existing row's value changes between two reads; phantom = the set of rows matching a predicate changes (rows inserted/deleted).
Which level prevents non-repeatable reads but (per the standard) still allows phantoms?
REPEATABLE READ.
Order the 4 levels weakest → strongest.
READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE.
What does SERIALIZABLE actually guarantee?
The concurrent result is equivalent to SOME serial (one-at-a-time) execution order — not literal serial execution.
In MVCC, what's the difference between READ COMMITTED and REPEATABLE READ snapshots?
READ COMMITTED takes a fresh snapshot per statement; REPEATABLE READ takes one snapshot at transaction start and reuses it.
How are phantoms prevented under locking?
Range/predicate (next-key) locks block inserts into a queried range.
Default isolation level in PostgreSQL vs MySQL InnoDB?
PostgreSQL = READ COMMITTED; MySQL InnoDB = REPEATABLE READ.
Why not just use SERIALIZABLE everywhere?
More blocking, deadlocks, and serialization-failure retries → lower throughput; use the lowest level that prevents anomalies your app cares about.

Connections

  • ACID properties — the I in ACID is exactly this isolation.
  • MVCC (Multi-Version Concurrency Control) — snapshot mechanism behind READ COMMITTED / REPEATABLE READ.
  • Two-Phase Locking (2PL) — lock-based path to serializability.
  • Serializable Snapshot Isolation (SSI) — how PostgreSQL does SERIALIZABLE optimistically.
  • Deadlocks — side effect of holding locks at higher levels.
  • Write skew anomaly — anomaly that snapshot isolation allows but SERIALIZABLE prevents.

Concept Map

cause

trade correctness for speed

includes

includes

includes

reads uncommitted data

existing row value changes

row set membership changes

allows all

bans dirty read

bans non-repeatable

bans phantoms

achieves

defines

adds guarantee

adds guarantee

adds guarantee

Concurrent transactions

Read anomalies

Isolation levels

Dirty read

Non-repeatable read

Phantom read

Serializability goal

READ UNCOMMITTED

READ COMMITTED

REPEATABLE READ

SERIALIZABLE

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, database mein ek saath bahut saari transactions chal rahi hoti hain. Agar woh same rows ko touch karein bina kisi control ke, toh galat results aa sakte hain. Isolation levels ek dial hai jahan tum decide karte ho ki kitni "weirdness" allow karni hai concurrency badhane ke liye. Teen famous problems hain: dirty read (kisi ki uncommitted scribble padh li, jo baad mein rollback ho gayi), non-repeatable read (ek hi row do baar padhi, value badal gayi), aur phantom read (ek WHERE query do baar chalayi, naye rows aa gaye ya gayab ho gaye).

Char levels hain, weakest se strongest: READ UNCOMMITTED → READ COMMITTED → REPEATABLE READ → SERIALIZABLE. Har step neeche jaate hi ek anomaly band ho jaati hai. READ UNCOMMITTED sab kuch allow karta hai. READ COMMITTED sirf committed data dikhata hai (dirty read gone). REPEATABLE READ ek baar ka snapshot pakad leta hai, toh jo rows tumne padhi woh nahi badlengi (non-repeatable read gone). SERIALIZABLE poori tarah safe — result aisa hoga jaise transactions ek-ek karke chali ho (phantom bhi gone).

Yaad rakhne ka mnemonic: U-C-R-S order, aur D-N-P (Dirty, Non-repeatable, Phantom) ek-ek karke peel off hoti hain. Ek important baat: SERIALIZABLE ka matlab yeh nahi ki transactions literally line mein chalti hain — bas final result kisi serial order ke barabar hota hai, andar concurrency rehti hai (PostgreSQL toh SSI use karke conflicting transactions ko abort kar deta hai, jise tumhe retry karna padta hai).

Practical tip: hamesha SERIALIZABLE mat lagao — woh slow karta hai, deadlocks aur retries badhata hai. App ko jo anomaly actually nuksan karti hai, bas usse rokne wala lowest level choose karo. PostgreSQL default READ COMMITTED hai, MySQL InnoDB default REPEATABLE READ — yeh interview mein puchte hain!

Go deeper — visual, from zero

Test yourself — Databases

Connections