Transactions and isolation levels
Everyone can expand ACID: Atomicity (all or nothing), Consistency (constraints hold), Isolation (concurrent transactions don't corrupt each other), Durability (committed survives a crash). Here's what fewer people know: Isolation is not a guarantee — it's a dial, with settings, and the default setting on your database almost certainly permits bugs you think it prevents. Let's meet the bugs first, then the dial.
The anomalies — a rogues' gallery
- Dirty read: you read data another transaction has written but not yet committed — then it rolls back. You made a decision based on data that officially never existed.
- Non-repeatable read: you read a row, do some work, read the same row again — and it changed under you, because another transaction committed in between. Your own transaction can't trust its own earlier reads.
- Phantom read: the range version. "How many orders over ₹1000?" — 5. Same query, moments later, same transaction — 6. A new row appeared in your range. Individual rows didn't change; the set did.
- Write skew — the sneaky one, worth slowing down for. A hospital rule: at least one doctor must always be on call. Two doctors are on. Both, simultaneously, open the app to cancel their shift. Alice's transaction checks: "is anyone else on call? Yes, Bob." She cancels. Bob's transaction checks: "is anyone else on? Yes, Alice." He cancels. Both transactions read true data, both wrote to different rows, neither saw a conflict — and together they broke the invariant. Nobody is on call. Notice why it's so slippery: there's no row both transactions touched, so simple row-level machinery never fires.
The isolation dial
| Read uncommitted | all anomalies possible |
| Read committed | no dirty reads |
| Repeatable read | + no non-repeatable reads |
| Serializable | none — as if run one at a time |
Each step down buys correctness with throughput: stronger isolation means more coordination, more waiting, and — at Serializable — the database sometimes aborting transactions it can't safely order, handing your application a "serialization failure, please retry" that your code must actually handle.
Now the part that surprises working engineers: Postgres defaults to Read Committed. MySQL/InnoDB defaults to Repeatable Read. Neither default prevents write skew. Only Serializable does. So the doctor bug above can happen, today, on a stock installation of either — inside transactions, with everyone following the rules.
What professionals do instead of cranking the dial
Don't reflexively raise the global isolation level and eat the throughput cost on every transaction for one invariant's sake. Protect the specific invariant with a targeted tool:
- A targeted lock: in the doctor case,
SELECT ... FOR UPDATEon the on-call roster rows forces the two cancellations to run one after the other — the second one then sees the truth. - A constraint, better still: express the rule so the storage engine enforces it regardless of interleaving. A unique index is a booking-conflict guard stronger than any isolation level — two inserts for the same seat, and the engine itself rejects the second, always, no timing window, no exceptions.
The hierarchy of trust: constraints beat locks beat isolation levels beat application-level checks. Push invariants as far down that list as they'll go.
Believing "we use transactions" means "we're safe from concurrency bugs." At default isolation, a check-then-act inside a transaction is still a race — the transaction gives you atomicity of your writes, not exclusivity of your reads. Two transactions can both read the same balance, both decide the withdrawal is fine, and both commit. The Tier 2 fixes — conditional atomic updates, versions, targeted locks — are exactly as necessary inside transactions as outside them.
Going deeper
- A Critique of ANSI SQL Isolation Levels Berenson et al., 1995
Demonstrates that the standard’s levels are defined by the anomalies they happen to prohibit, which is why vendor behaviour at the same named level differs so much. - Jepsen analyses Kyle Kingsbury
Empirical testing of what databases actually guarantee under partition, as opposed to what their documentation claims. Read the one for whatever you run.