Lesson 4 of 9 · 27m
Transactions, Isolation, and MVCC
Isolation levels are concurrency contracts, not academic labels. MVCC explains why readers do not block writers — and where anomalies still leak through.
Every team eventually learns this the hard way: two perfectly correct functions can corrupt data when run concurrently. Transactions exist so multi-step changes succeed or fail as a unit. Isolation levels define how much of other people's in-flight work you are allowed to see while doing it.
Atomicity is the easy part to explain
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 7;
UPDATE accounts SET balance = balance + 100 WHERE id = 9;
COMMIT;If the second statement fails, the first must not survive. The write-ahead log from lesson one makes committed changes durable; undo information lets uncommitted or aborted work disappear. Atomicity is not a hint — either both rows change or neither does.
Isolation is where behavior gets subtle
The SQL standard names anomalies before it names levels:
- Dirty read: seeing data from a transaction that later rolls back.
- Non-repeatable read: reading the same row twice and getting different values.
- Phantom read: running the same predicate twice and seeing new matching rows.
- Write skew: two transactions read overlapping state, make locally valid decisions, and together violate an invariant.
Most systems implement practical approximations: read committed, repeatable read, snapshot isolation, serializable. Do not memorize a chart and stop there; learn what your database actually guarantees. PostgreSQL's REPEATABLE READ is snapshot isolation. MySQL InnoDB's REPEATABLE READ has different locking behavior. Oracle famously does not give you ANSI serializable just because the label sounds similar.
MVCC: many versions of the same row
Multiversion concurrency control lets readers see a snapshot without blocking writers. PostgreSQL keeps multiple tuple versions in the heap; each has transaction visibility metadata. Old versions become dead tuples and vacuum eventually removes them.
row version A: xmin=880, xmax=0 visible to newer snapshots
row version B: xmin=875, xmax=880 visible only to older snapshotsThat design makes ordinary reads cheap and consistent, but long-lived transactions pin old versions. One idle transaction with an open snapshot can turn a healthy table into vacuum debt and storage bloat.
Snapshot isolation still allows write skew
Classic example: at least one doctor must remain on call.
-- both transactions read: two doctors on call
UPDATE doctors
SET on_call = false
WHERE id = $1;
-- each thinks the other doctor remains; both commit; nobody is on callNeither row read was wrong under snapshot isolation. The invariant across rows was the problem. Use explicit locking, constraints where possible, or serializable isolation when the business invariant matters more than throughput.
Field notes
- Never leave transactions open in application code; use timeouts and connection pool settings as guardrails.
SELECT ... FOR UPDATEis not evil — it is the correct tool when row-level intent must be explicit.- Isolation is per transaction, not per query. You can choose stronger isolation only for critical paths.
- Retrying serialization failures is normal application behavior, not a sign the database is broken.
Was this lesson helpful?