What do transaction isolation levels actually prevent?
Company tags are compiled from public interview reports and are signals, not guarantees.
What interviewers are testing
Isolation levels are the sharpest test of whether you understand concurrency, because the happy-path answer ("wrap it in a transaction") is wrong in interesting ways. Interviewers listen for the anomaly ladder — dirty read, non-repeatable read, phantom — and for you to name your engine’s default instead of reciting the textbook table. The strongest signal is knowing that real engines are stronger than the standard, and that SERIALIZABLE moves the failure into your retry loop.
Mental model
Each level is a promise about visibility. READ COMMITTED refreshes the snapshot per statement — you always see the latest committed world, so two statements can disagree. REPEATABLE READ freezes one snapshot for the whole transaction — reads are stable, but the set of rows can still shift under the standard. SERIALIZABLE adds conflict detection that aborts one side rather than letting a contradiction commit.
Step-by-step solution
Step 1: Dirty reads: acting on data that never existed
The weakest level, READ UNCOMMITTED, lets a transaction read rows another transaction has written but not yet committed. The nightmare scenario is short: T1 reads T2's uncommitted withdrawal and makes a decision — approve a loan, skip a re-check, send a confirmation — and then T2 rolls back. The 0 T1 acted on never existed in any committed state of the database. Watch the animation: nothing about T1's SQL is wrong; the contract under it was simply absent. This anomaly is why every serious engine refuses to ship READ UNCOMMITTED as a default, and why the interesting interview ground is a level up: what your default still allows after dirty reads are gone.
Step 2: READ COMMITTED: fresh per statement, so re-reads drift
Most engines — Postgres, Oracle, SQL Server — default to READ COMMITTED, and it feels safe: every statement sees a fresh snapshot, and dirty reads are impossible. The trap is across statements inside one transaction. T1 reads balance = 100, decides, and reads again — meanwhile T2 committed an update to 50 in between. Both reads are perfectly honest; they just describe different worlds, and any invariant T1 computed from the first read is now wrong. Watch the animation: nothing T1 did caused it, which is what makes this the most common production anomaly. It is invisible in single-statement operations and lethal in read-then-act flows — exactly the shape of payment, inventory, and seat-booking code.
Step 3: REPEATABLE READ: stable rows, moving sets
REPEATABLE READ freezes one snapshot for the entire transaction: read the same row twice and you get the same answer, no matter who commits in between. What the standard still allows is a phantom — T1 counts the rows matching a predicate, T2 inserts a matching row and commits, and T1's second count returns one more. Nothing T1 read changed; the set it asked about did, which is why phantoms break range-based invariants like "the sum of pending orders" or "no two overlapping bookings". The honest nuance to volunteer: real engines are stronger than the textbook. Postgres REPEATABLE READ is snapshot isolation and blocks phantoms too, and InnoDB adds gap locks on locking reads — so name your engine before reciting the table.
Step 4: SERIALIZABLE: conflicts become your retries
SERIALIZABLE makes the database detect when concurrent transactions could not have run one after another in some order, and aborts one of them instead of committing a contradiction. It also closes the anomaly the screenshot-famous table forgets: write skew — two transactions that each read an overlapping set and write different rows, staying invisible to snapshot isolation. The bargain is explicit: the failure moves into the application, which must catch serialization errors (SQLSTATE 40001 in Postgres) and retry, bounded and idempotent. Watch the animation: T2 is aborted on a state that moved, then retried fresh. Interviewers are listening for that retry loop — calling SERIALIZABLE "just a safer setting" without owning the retries is the failure mode.
Step 5: What to actually do about it
The practical answer is a toolkit, matched to the shape of the bug. Read-modify-write on a hot row — check balance, then update — is a lost update under many defaults; fix it with a lock (SELECT … FOR UPDATE) or optimistic locking (a version column and a retry on mismatch). Invariants spanning rows want SERIALIZABLE plus a bounded retry loop, not a pile of hope. Job queues want SELECT … FOR UPDATE SKIP LOCKED, which is the modern pattern for letting workers take different rows without fighting. And the universal rules: know your engine’s default before the incident, and never hold a transaction open across an HTTP call or user think-time — long transactions turn contention into outages.
Edge cases & traps
- Snapshot isolation still allows write skew: two transactions each read an overlapping set and update different rows — only SERIALIZABLE (or explicit locking) catches it, which is the anomaly most candidates miss.
- Engine defaults are not the standard: Postgres READ COMMITTED and InnoDB REPEATABLE READ both behave stronger than the textbook table in places — never cite the table without naming the engine.
- Long transactions amplify everything: they widen every conflict window and hold back cleanup (Postgres vacuum, InnoDB history lists), so a slow transaction is a performance incident waiting for load.
- Serialization failures are workload, not bugs: SQLSTATE 40001 must be handled by a bounded, idempotent retry loop — unbounded retries under contention just move the outage.
- Never hold a transaction across an HTTP call, a queue publish, or human think-time: row locks become site-wide outages when the lock owner is waiting on something slower than the database.