Topic 070 / Core
Transactions and isolation levels: transaction boundaries, isolation anomalies
Chapter 5.4 · Engines, Durability, Transactions, and Concurrency
What and why
A transaction boundary is the deliberate choice of what counts as one business operation — not one SQL statement, not one HTTP request. Isolation levels then decide which anomalies concurrent transactions can produce for each other; each level is defined by exactly which anomalies it rules out. Builds on: → 69 (ACID properties).
Diagram
| Level (weak → strong) | Rules out | May still allow |
|---|---|---|
| Read uncommitted | — | dirty reads |
| Read committed | dirty reads | non-repeatable reads, write skew |
| Repeatable read | + non-repeatable reads | phantom reads or write skew, depending on the engine |
| Serializable | all of the above, in general | — (behaves as if transactions ran one at a time) |
Exactly what a given level guarantees depends on the engine's implementation — check your database's own documentation rather than assuming.
How it works
- Draw the transaction boundary around one business operation, deliberately.
- Dirty read: reading data written by a transaction that hasn't committed (and might roll back).
- Non-repeatable read: re-reading the same row twice in one transaction returns different values because another transaction committed in between.
- Phantom read: re-running the same range query returns a different set of rows.
- Lost update: two transactions read the same row, both write back a value computed from what they read, and one overwrite silently clobbers the other (classic case: two transactions each decrement the same starting balance and only one decrement survives — the worked example below).
- Write skew: two transactions each read shared data, then each writes a different row; either write is fine alone, but together they violate a cross-row invariant (classic case: two rows both marked "on duty", and two transactions each check "someone else is still on duty" before setting their own row off duty — both checks pass, both commit, and no one is left on duty).
- Stronger isolation rules out more anomalies but costs more concurrency — pick the weakest level that is still correct for the operation.
Practical example
Two transactions both read a balance of 100, both compute
100 - 10 = 90, both write 90. The balance should be 80 after two withdrawals; it's 90 —
one decrement was silently lost. This is reproducible at a weak isolation level and is the
textbook lost-update anomaly.
When to use it
- Use when: correctness across multiple statements in one transaction actually depends on not seeing a concurrent transaction's in-flight or just-committed changes — money, inventory, bookings.
- Avoid when: defaulting to the strongest level everywhere without need — the throughput cost may be unnecessary where anomalies genuinely don't matter (e.g., independent append-only writes).
- Failure scenarios: the lost update above; a report reading inconsistent cross-table state at a weak isolation level and drawing a wrong conclusion from it.
- Simpler alternative: for a single-statement, read-only operation, the database's own default behavior in autocommit mode is usually sufficient — no explicit tuning needed.
Practice
(vocabulary + mechanism, 20–30 min, lab Postgres — merges this chapter's
concurrency exercise). Open two concurrent psql sessions against the lab database at
the default isolation level. Have both read the same balance row, compute a decrement, and
write it back, interleaving the two sessions by hand. Reproduce the lost update above, then
name which standard anomaly it is and which isolation level would have prevented it. Done
when the lost update is reproduced and correctly named.
Key takeaway
A transaction boundary is a deliberate business choice; an isolation level is a precise promise about which anomalies can't happen. Done when you can: name the anomaly in a given interleaving and the weakest isolation level that rules it out. Used next in → 71 (fixing it); cross-system operations that can't share a transaction → 139.
References
Kleppmann and Riccomini, Designing Data-Intensive Applications — see
references.md.