What it is
An isolation level states how much concurrent transactions may affect each other. The SQL standard defines four levels (READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE) by the anomalies they rule out. Real databases implement them differently, so the useful question is always "what does my database allow at this level?". This lesson uses Postgres semantics.
Why it exists
Running transactions strictly one after another would be correct and very slow. Databases run them concurrently and use snapshots and locks so that each one sees a sensible view of the data. Full protection (serializability) costs aborts or blocking, so databases offer weaker levels that are cheaper but let specific anomalies through. Knowing which ones is the difference between "works in testing" and "loses money under load".
How it works
The anomalies.
- Dirty read: reading another transaction's uncommitted change.
- Non-repeatable read: reading the same row twice in one transaction and getting different committed values. Phantoms are the same with rows appearing or disappearing from a query result.
- Lost update: two transactions read a value, both compute a new one and write it; the second write erases the first.
- Write skew: two transactions read overlapping data, then write different rows based on what they read; each is fine alone, together they break an invariant (two doctors both leave the on-call rota).
Postgres levels. Postgres uses MVCC: readers see a snapshot of committed row versions and never block writers.
READ UNCOMMITTEDis accepted and behaves exactly like READ COMMITTED.READ COMMITTED(the default) takes a new snapshot for each statement. When an UPDATE has to wait for another transaction's row lock, it re-checks the newest committed version of the row after the wait and applies itself to it. SoSET balance = balance - 50is safe, while writing a value computed in the application is not.REPEATABLE READtakes one snapshot at the first statement of the transaction (snapshot isolation). Non-repeatable reads and phantoms cannot occur. If the transaction tries to update or delete a row that a concurrent transaction changed and committed after the snapshot, it fails with SQLSTATE40001("could not serialize access due to concurrent update").SERIALIZABLEuses Serializable Snapshot Isolation (SSI): REPEATABLE READ plus tracking of read/write dependencies between concurrent transactions. When a pattern appears that no serial order could produce, one transaction fails with40001. Reads still do not block.
| Anomaly | READ COMMITTED | REPEATABLE READ | SERIALIZABLE |
|---|---|---|---|
| Dirty read | No | No | No |
| Non-repeatable read / phantom | Possible | No | No |
| Lost update (read-modify-write) | Possible | No (40001 abort) | No |
| Write skew | Possible | Possible | No (40001 abort) |
Explicit locking. At any level, SELECT … FOR UPDATE locks the selected rows until the transaction ends, so a second transaction doing the same waits before reading. FOR NO KEY UPDATE, FOR SHARE and SKIP LOCKED / NOWAIT refine this. A lock on a frequently updated row serialises all its writers: the row can complete at most 1 ÷ (lock hold time) changes per second.
When to use it
- READ COMMITTED for most OLTP work, with atomic statements (
SET x = x + 1,INSERT … ON CONFLICT, conditionalUPDATE … WHERE) and constraints protecting invariants. - REPEATABLE READ for reports and exports that run several queries and need them to agree, and for read-modify-write code that can retry on
40001. - SERIALIZABLE when invariants span rows or tables and are hard to express as constraints or locks (rotas, double bookings, quota checks), provided every such transaction is retried on
40001. - FOR UPDATE when a short critical section must decide based on current data and retrying is undesirable.
When not to
- Do not raise the whole application to SERIALIZABLE without retry logic: serialization failures become user-facing errors.
- Do not use long REPEATABLE READ or SERIALIZABLE transactions for interactive work: old snapshots keep dead rows from being vacuumed and raise abort rates.
- Do not take
FOR UPDATElocks on hot rows across network round trips or user think time.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| READ COMMITTED | No aborts from isolation, simplest | You must guard read-modify-write and cross-row invariants yourself |
| REPEATABLE READ | Stable snapshot, no lost updates | 40001 aborts on contended rows; still allows write skew |
| SERIALIZABLE | Correct as if run one at a time | More aborts (including false positives), tracking overhead, mandatory retries |
| SELECT … FOR UPDATE | Simple mental model, no retries | Blocking, deadlock risk, hot rows capped by lock hold time |
| Atomic single statement | Shortest lock, no retries | Only works when the logic fits in one statement |
Alternatives
- Constraints and unique indexes: often the cleanest way to make an invariant impossible to break (
UNIQUE,CHECK, exclusion constraints for overlapping bookings). - Optimistic concurrency control: a
versioncolumn,UPDATE … WHERE id = $1 AND version = $2, retry when zero rows changed. Common in ORMs; works at READ COMMITTED. - Advisory locks (
pg_advisory_xact_lock) to serialise work on a logical key that is not a single row. - Sharding a hot counter into several rows and summing them on read.
Failure modes
- Silent lost updates from read-modify-write code at READ COMMITTED, often hidden inside ORM "load, change, save" flows.
- Write skew under snapshot isolation that nobody tested for: double-booked rooms, overdrawn shared limits.
- Retry storms: SERIALIZABLE or REPEATABLE READ under heavy contention aborting and retrying the same transactions repeatedly.
- Lock queues on hot rows: one popular counter or inventory row limits the whole system; waits exceed
lock_timeoutor request timeouts. - Porting assumptions between databases: MySQL InnoDB's default REPEATABLE READ lets an UPDATE act on the latest committed row instead of aborting, so lost updates need explicit locking there.
In production
Know the default of every database you use (Postgres and SQL Server: READ COMMITTED; MySQL InnoDB: REPEATABLE READ; Oracle: READ COMMITTED, with "SERIALIZABLE" meaning snapshot isolation). Wrap REPEATABLE READ and SERIALIZABLE transactions in a retry loop for SQLSTATE 40001 (and 40P01 deadlocks) with jittered backoff, and keep them short. Set lock_timeout so lock queues fail fast. Find hot rows through lock waits (log_lock_waits, pg_locks) and fix them with atomic statements, SKIP LOCKED work queues or split counters.
Common misconceptions
- "REPEATABLE READ means serializable enough." It allows write skew.
- "SERIALIZABLE means transactions run one at a time." They run concurrently; the database aborts the ones that would break the illusion.
- "Higher isolation is always slower." SSI readers do not block; the cost shows up as aborts under contention, not as uniformly slower queries.
- "Isolation levels mean the same thing everywhere." The names are standard, the behaviour is not.
Interview angle
Expect a concurrency bug to diagnose: "two users book the last seat" or "a counter loses increments". A strong answer names the anomaly, says which level prevents it in the given database, and proposes the cheapest correct fix (an atomic conditional UPDATE, a unique constraint, FOR UPDATE, or SERIALIZABLE with retries), noting the hot-row ceiling of locking and the need for retry logic.
Model assumptions
Steps 1–7 are scripted illustrations of Postgres behaviour, not simulations: the engine has no snapshots, row versions, locks or serialization checks. Packets are slowed down so the interleaving is visible; values and outcomes are annotations, and where Postgres may raise the SERIALIZABLE error at different points, the illustration shows it at commit. Steps 8–9, the sandbox and the challenge are simulated: the hot row's lock is modelled as a resource with a single slot, each purchase holds it for an average of 15 ms (FOR UPDATE transaction) or 1.5 ms (atomic UPDATE), and a purchase that waits more than 1 s fails, like lock_timeout = 1s. Arrivals are random (Poisson), hold times lognormal. Numbers are illustrative, not benchmarks.