Kayavak / Databases / DB·59 Transactions and Concurrency Control / Deeper
Deeper · Databases
Transactions and Concurrency Control
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
When several people change shared data at once, and machines crash mid-change, the data can end up in states no single person intended. Jim Gray's work in the 1970s, summarised in The Transaction Concept (1981), defined the transaction: a group of changes that happens completely or not at all, in isolation from others, and survives crashes. Theo Härder and Andreas Reuter coined ACID in 1983. Gray received the Turing Award in 1998 for it.
Full isolation (serializability) was expensive, so databases offered weaker isolation levels, and the 1995 paper A Critique of ANSI SQL Isolation Levels showed the standard's definitions were ambiguous and that snapshot isolation, widely used, allowed an anomaly called write skew. In 2008 Michael Cahill and colleagues published Serializable Snapshot Isolation, which detects dangerous patterns instead of locking; PostgreSQL adopted it in 2011.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
Isolation levels are a menu of anomalies you agree to tolerate in exchange for concurrency. Choose a level by asking which anomalies your logic can't survive, and be ready to retry when the database refuses a transaction.
| Question | Answer |
|---|---|
| What is it? | Grouping operations so they're atomic, isolated and durable, and controlling how concurrent groups interact |
| Problem it solves | Correct data under concurrent changes and crashes |
| Parts | ACID, isolation levels, anomalies (lost update, read skew, write skew, phantoms), locks, MVCC snapshots, SSI, deadlocks |
| How it works | Each transaction reads a snapshot or takes locks; conflicting commits are blocked, aborted or detected |
| Assumptions | The application retries serialization failures and deadlocks |
| When they change | Without retries, correct aborts become user-facing errors; with weak isolation, invariants spanning rows break |
| Where it fails | Long transactions holding locks; invariants checked across rows under snapshot isolation |
| Related ideas | Concurrency and races (The Backend, ch38); distributed transactions and consistency (Distributed Systems) |
What if?
What if two on-call doctors both go off call at the same moment?
Reason it through, then open
The rule is "at least one doctor on call". Each transaction reads the table under snapshot isolation, sees two doctors
on call, and removes itself. Neither wrote the row the other wrote, so neither conflicts, and both commit: zero
doctors on call. That's write skew. Serializable isolation (or locking the rows that were read, with SELECT … FOR
UPDATE, or a constraint the database can check) prevents it.
What if the database never aborted transactions?
Reason it through, then open
To guarantee serializable results without aborting, it would have to lock pessimistically and make transactions wait for each other, sometimes for a long time, and deadlocks would still force one to give up. Aborts and retries are the price of letting transactions run in parallel optimistically.
Where the boundary is
| Aspect | Snapshot isolation (Repeatable Read in PostgreSQL) | Serializable |
|---|---|---|
| Each transaction sees | A consistent snapshot | A consistent snapshot, plus conflict detection |
| Prevents | Dirty and non-repeatable reads, lost updates on the same row | Every anomaly, including write skew |
| Costs | Little | Some aborts to retry; tracking overhead |
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Snapshot isolation allows write skew | Theorem | Berenson et al., 1995 |
| SSI gives serializability without blocking readers | Theorem, with a measured overhead | Cahill, Röhm and Fekete, 2008; PostgreSQL 9.1 (2011) |
| Many applications run safely at Read Committed | Model | True when invariants are per row or enforced by constraints; studies of real apps find latent anomalies |
Still open
How often real applications are broken by weak isolation is still studied: research such as ACIDRain (2017) found exploitable anomalies in popular e-commerce applications. Making serializable the cheap default is an ongoing goal.
What would change your mind? If your invariants were all single-row or enforced by constraints, weaker isolation would be safe for you. Check each invariant that spans rows.
Recall
What is write skew?
Two transactions read overlapping data, write different rows, and together break an invariant neither broke alone.
Who defined the transaction concept and coined ACID?
Jim Gray described transactions (1981); Härder and Reuter coined ACID (1983).
Why must applications retry transactions?
Databases abort transactions on deadlocks and serialization conflicts by design.
What does Serializable Snapshot Isolation add?
Detection of dangerous read-write patterns, aborting a transaction instead of locking readers.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.