Concurrency Problems
Concurrent transactions are supposed to behave as if they ran one at a time. Interleavings can break that promise in four classic ways.
In plain English
It is two people editing the same shared document without seeing each other's changes.
The one idea
Concurrent transactions can interleave into histories that no serial order could produce, and each anomaly is a different broken guarantee.
Isolation levels are chosen by naming the anomalies they prevent. You cannot reason about a level without knowing the failure it allows.
By the end
- 01Explain lost update, dirty read, unrepeatable read, and phantom read.
- 02Trace an interleaving that causes each problem.
- 03Define serializability.
- 04Know why isolation levels trade correctness for concurrency.
Lost update and dirty read
A lost update happens when two transactions read the same value, both modify it, and one overwrites the other. A dirty read happens when a transaction reads data written by another transaction that has not committed and may still roll back.
T1: read x=100
T2: read x=100
T1: write x=110
T2: write x=120 /* T1's update is lost */
T1: write x=200
T2: read x=200 /* dirty read */
T1: rollback /* T2 read a value that never existed */Unrepeatable read and phantom
An unrepeatable read happens when a transaction reads the same row twice and gets different values because another transaction committed a change in between. A phantom read happens when a transaction repeats a range query and sees new rows that another transaction inserted.
Serializability
A schedule is serializable if its effect is equivalent to some serial execution of the same transactions. The DBMS uses locks, timestamps, or validation to enforce a serializable schedule. Weaker isolation levels allow some anomalies to gain concurrency.
Two withdrawals from one balance
Account A has 100. T1 and T2 each withdraw 50 in separate transactions.
Both transactions read A = 100.
Each believes there is enough money for its withdrawal.
Both write 50.
The final balance is 50 instead of 0; one withdrawal is lost.
Run the same operations serially.
The first writes 50, the second reads 50 and writes 0. The serial result is correct.
ConclusionA lost update is not a race in the application's arithmetic. It is a missing guarantee that the read and write form one atomic unit.
The trade-off
No free correctness.
Gain
Higher isolation prevents more anomalies.
Cost
Stronger isolation blocks more concurrency, increases waits, and can cause deadlocks.
Verdict
Pick the weakest level that provably prevents the anomalies your business cannot tolerate.
Recap
What to keep.
Takeaways
- +Lost update: one transaction overwrites another's change.
- +Dirty read: reading uncommitted data.
- +Unrepeatable read: the same row changes between reads.
- +Phantom read: a range query sees new rows.
- +Serializability is the correctness target.
Traps
- !Thinking a transaction is isolated just because it is atomic.
- !Assuming every isolation level prevents phantoms.
- !Ignoring the cost of serializable isolation.
Focus
- →Trace each anomaly with two transactions.
- →Explain serializability in one sentence.
- →Match isolation levels to the anomalies they prevent.
Check yourself
Before you turn the page.
What is a dirty read?
What does serializability guarantee?