Skip to main content
PreparationZone Logo
PrepZone

Isolation Levels and Locking

Dirty reads, phantom rows, MVCC in Postgres, and choosing the right isolation level.

Pass checkpoint below

Why this matters

  • Wrong isolation assumptions cause double charges, negative stock, and duplicate loyalty points at VaultCommerce scale.
  • READ COMMITTED is Postgres default — knowing what it allows and blocks prevents surprise bugs.
  • Long transactions hold locks and block checkout; isolation tuning is a performance and correctness decision.
  • Senior interviews expect dirty read, non-repeatable read, and phantom read definitions with examples.
Read uncommitted
Read committed
Repeatable read
Serializable
Stronger isolation prevents more anomalies but reduces concurrency.

The isolation spectrum

SQL defines four standard levels. Each prevents a class of concurrency anomaly:

Anomalies and prevention

  • Dirty read — Reading uncommitted data from another transaction. Prevented by READ COMMITTED and above.
  • Non-repeatable read — Same query returns different rows within one transaction. Prevented by REPEATABLE READ and above.
  • Phantom read — New rows appear in a repeated range scan. Prevented by SERIALIZABLE (Postgres REPEATABLE READ also blocks phantoms via MVCC).

Postgres implements READ UNCOMMITTED as READ COMMITTED — dirty reads never occur.

Postgres defaults and MVCC

Multi-Version Concurrency Control keeps old row versions visible to snapshots. Readers do not block writers; writers do not block readers. Conflicts appear when two transactions try to update the same row:

Java
-- Session A
BEGIN;
UPDATE products SET stock = stock - 1 WHERE id = 881;
-- holds row lock until COMMIT

-- Session B (blocked until A commits or rolls back)
UPDATE products SET stock = stock - 1 WHERE id = 881;

VaultCommerce checkout uses short transactions — lock duration is milliseconds, not seconds.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 4 sections

  • Isolation levels in practice
  • Explicit locking with SELECT FOR UPDATE
  • Optimistic concurrency
  • Observing locks

Test yourself

Answer these before moving on — recall is what makes it stick.