Lecture: Lecture 17 - Concurrency Control
Related: Transactions and ACID Properties, DBMS Architecture and Query Processing, Two-Phase Locking and Strict 2PL
Why concurrency control exists
Operational DBMSs (such as banking systems and flight reservations) process workloads of roughly to transactions per second (tps) (Slide 3). Running transactions strictly one at a time avoids interference, but leaves hardware idle and causes unacceptable waiting. Concurrency control allows concurrent access to shared data to:
- Maximize throughput (transactions per second)
- Minimize response time
At the physical I/O boundary, each transaction executes sequences of two elementary operations on single data objects (possibly reading or writing an entire page from/to disk) (Slide 4):
r(x): read data objectxw(x): write data objectx
Inside the Concurrency Control module sits the scheduler, which decides if and when each r(x) or w(x) request may run. Without a scheduler, interleaved transactions produce correctness bugs called anomalies (Slide 5).
The five concurrency anomalies (Slides 6–12)
In the traces below, bot marks the beginning of a transaction (COMMIT/ABORT mark its end) and time flows downward.
1. Lost Update (Slide 6)
Initial state: x = 2. Both and intend to add 1 to x.
| Time | Value seen / written | ||
|---|---|---|---|
| 1 | bot, r₁(x) | reads x = 2 | |
| 2 | x = x + 1 | computes 3 locally | |
| 3 | bot, r₂(x) | reads x = 2 | |
| 4 | x = x + 1, w₂(x), commit | writes x = 3 | |
| 5 | w₁(x), commit | overwrites x = 3 |
- Outcome:
x = 3, whereas the correct result after two increments isx = 4. - Cause: Both transactions read the same initial value (
2), so ‘s write overwrites and loses ‘s committed update.
2. Dirty Read (Slide 7)
Initial state: x = 2.
| Time | Value seen / written | ||
|---|---|---|---|
| 1 | bot, r₁(x), x = x + 1, w₁(x) | writes uncommitted x = 3 | |
| 2 | bot, r₂(x) | reads x = 3 | |
| 3 | x = x + 1, w₂(x), commit | commits x = 4 | |
| 4 | abort | rolls back! |
- Outcome: read
xin an intermediate uncommitted state that never became permanent. - Consequence: Undoing would force a cascade rollback of , which is especially problematic once has already committed.
3. Inconsistent Read (Non-Repeatable Read) (Slide 8)
Initial state: x = 2.
| Time | Value seen / written | ||
|---|---|---|---|
| 1 | bot, r₁(x) | reads x = 2 | |
| 2 | bot, r₂(x), x = x + 1, w₂(x), commit | updates x = 3 and commits | |
| 3 | r₁(x), commit | reads x = 3 |
- Outcome: Transaction reads the same single object
xtwice and observes two different values (2, then3).
4. Ghost Update (a): Partial Observation Across Multiple Objects (Slides 9–10)
Initial state: x = 400, y = 300, z = 300 (invariant total = 1000). transfers 100 from y to z, preserving y + z = 600.
| Time | Value seen / written | ||
|---|---|---|---|
| 1 | bot, r₁(x) | x = 400 | |
| 2 | bot, r₂(y) | y = 300 | |
| 3 | r₁(y) | reads old y = 300 | |
| 4 | y = y - 100, r₂(z), z = z + 100, w₂(y), w₂(z), commit | commits y = 200, z = 400 | |
| 5 | r₁(z) | reads new z = 400 | |
| 6 | total = x + y + z, commit | computes total = 1100 |
- Outcome: The true total is
1000, not1100. - Cause: partially observes the effect of —reading
ybefore ‘s update andzafter ‘s update.
5. Ghost Update (b): Phantom Insert (Slides 11–12)
- reads all salaries in department
xand computesAVG(salary). - inserts a new employee into department
xand commits. - re-reads all salaries in department
xand computes a differentAVG(salary).
- Why it is distinct: Unlike Inconsistent Read (where an existing object changes), the inserted row did not exist yet when ran its first query—so locking only the rows initially read cannot prevent ‘s insert.
Distinguishing the five anomalies
| Anomaly | Key structural signature | Commit/Abort trigger |
|---|---|---|
| Lost Update | Two transactions read initial x and both write x | Both commit; second write overwrites first |
| Dirty Read | reads w₁(x) before finishes; aborts | Rollback / cascade rollback |
| Inconsistent Read | reads the same object x twice across a committed w₂(x) | commits between two reads of x |
| Ghost Update (a) | reads multiple objects (y, z) catching half of ‘s committed transfer | commits between and |
| Ghost Update (b) | re-evaluates a set/aggregate query after inserts a new matching tuple | commits an INSERT |
Schedules, commit projection, and serializability (Slides 14–18)
- A transaction is a sequence of reads and writes sharing one Transaction Identifier (TID), e.g., .
- A schedule is the sequence of read/write operations presented by concurrent transactions in arrival order, e.g., .
- Commit projection (Slide 16) is a temporary simplifying assumption: schedules contain only transactions that commit (putting aside Dirty Reads until Strict 2PL removes the assumption).
- A serial schedule runs every transaction’s actions sequentially without interleaving across transactions—always anomaly-free (Slide 17).
- A schedule (under commit projection) is serializable if it produces the same outcome as some serial schedule of the same transactions (Slide 18).
View Serializability (VSR) (Slides 20–29)
Two schedules are view equivalent if they have the exact same:
reads-fromset: reads-from when precedes , , and there is no intervening write between them.final writeset: is a final write if it is the last write toxappearing in the schedule.
A schedule is view serializable (VSR) if it is view equivalent to an arbitrary serial schedule of the same transactions (Slide 21).
- Positive example (Slides 21–22):
- is view equivalent to serial (both
r₁(x)andr₂(x)read fromw₀(x); final writes arew₂(x)andw₂(z)). - is not view equivalent to (because
r₁(x)now reads fromw₂(x)), but it is view equivalent to serial → so is still VSR!
- is view equivalent to serial (both
- Negative examples (Slides 23–28):
- Lost Update has
reads-from = ∅and final writew₁(x), whereas the only two serial schedules ( and ) each force the second transaction to read from the first → not VSR. - Inconsistent Read has
r₁(x)reading both initialxandw₂(x), impossible in either serial schedule → not VSR. - Ghost Update (a) has
r₁(y)reading initialywhiler₁(z)reads fromw₂(z), matching neither serial schedule → not VSR.
- Lost Update has
Why real DBMSs reject VSR testing (Slide 29): Checking view equivalence against one given schedule takes linear time, but testing whether a schedule is view equivalent to an arbitrary serial schedule is NP-complete—not feasible in real systems.
Conflict Serializability (CSR) and the Conflict Graph (Slides 30–37)
Instead of checking all serial schedules via VSR, conflict equivalence restricts attention to pairs of conflicting actions (Slide 30):
Action conflicts with action () if:
- Both operate on the same object
x. - At least one is a write: Read-Write (RW / WR) or Write-Write (WW) (two reads RR never conflict).
- No other write sits between and .
Two schedules are conflict equivalent if they have the same conflict set and every conflict pair appears in the same order. A schedule is conflict serializable (CSR) if it is conflict equivalent to some serial schedule (Slide 31).
Conflict graph test (Slides 33–35)
- Create one node per transaction.
- Add a directed edge whenever an action of conflicts with and precedes an action of .
- If the conflict graph is acyclic (has no cycles), the schedule is CSR (and a topological order gives an equivalent serial schedule).
Worked source trace (Slides 31–35):
- Conflicts on
x: (), (), (), (). - Conflicts on
z: (), and (), (). - Result: The graph has edges , , , , —no cycles, so is CSR (equivalent to serial schedule : ).
Subset relationship and practical limit (Slides 36–37)
- (Slide 37): Every CSR schedule is VSR, while some VSR schedules (with blind writes) are not CSR.
- Why runtime conflict graphs are still too costly (Slide 36): Detecting cycles is linear in graph size, but in a realistic system running at where each transaction lasts and accesses pages, the graph has active nodes and accesses—updating the graph and checking cycles on every read/write is too expensive. Real DBMSs therefore enforce serializability via Two-Phase Locking (2PL).
Common confusions
- Ghost Update (a) vs. Inconsistent Read: Inconsistent Read reads the same single object twice (
r₁(x)…r₁(x)). Ghost Update (a) reads multiple related objects (ybefore writes,zafter writes), observing half of a committed multi-object transaction. - No cycle vs. cycle in a conflict graph: Acyclic = CSR (good); cycle = NOT CSR (must be rejected).
Retrieval questions
1. (Added study check) Build the conflict graph for . Is CSR?
Conflicts are ( on x) and ( on y). That creates the cycle , so is not CSR.
2. Why can’t row-level locking on existing rows stop Ghost Update (b)?
Because the inserted row did not exist in the database when the first aggregate query ran—there was no existing row to lock in advance.
3. Why don’t commercial DBMSs use VSR or online conflict-graph checking directly?
Testing VSR against an arbitrary serial schedule is NP-complete, and maintaining and cycle-checking a conflict graph on every page access across hundreds of active transactions is too expensive at runtime.