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 object x
  • w(x): write data object x

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.

TimeValue seen / written
1bot, r₁(x) reads x = 2
2x = x + 1 computes 3 locally
3bot, r₂(x) reads x = 2
4x = x + 1, w₂(x), commit writes x = 3
5w₁(x), commit overwrites x = 3
  • Outcome: x = 3, whereas the correct result after two increments is x = 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.

TimeValue seen / written
1bot, r₁(x), x = x + 1, w₁(x) writes uncommitted x = 3
2bot, r₂(x) reads x = 3
3x = x + 1, w₂(x), commit commits x = 4
4abort rolls back!
  • Outcome: read x in 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.

TimeValue seen / written
1bot, r₁(x) reads x = 2
2bot, r₂(x), x = x + 1, w₂(x), commit updates x = 3 and commits
3r₁(x), commit reads x = 3
  • Outcome: Transaction reads the same single object x twice and observes two different values (2, then 3).

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.

TimeValue seen / written
1bot, r₁(x)x = 400
2bot, r₂(y)y = 300
3r₁(y) reads old y = 300
4y = y - 100, r₂(z), z = z + 100, w₂(y), w₂(z), commit commits y = 200, z = 400
5r₁(z) reads new z = 400
6total = x + y + z, commit computes total = 1100
  • Outcome: The true total is 1000, not 1100.
  • Cause: partially observes the effect of —reading y before ‘s update and z after ‘s update.

5. Ghost Update (b): Phantom Insert (Slides 11–12)

  1. reads all salaries in department x and computes AVG(salary).
  2. inserts a new employee into department x and commits.
  3. re-reads all salaries in department x and computes a different AVG(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

AnomalyKey structural signatureCommit/Abort trigger
Lost UpdateTwo transactions read initial x and both write xBoth commit; second write overwrites first
Dirty Read reads w₁(x) before finishes; abortsRollback / 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:

  1. reads-from set: reads-from when precedes , , and there is no intervening write between them.
  2. final write set: is a final write if it is the last write to x appearing 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) and r₂(x) read from w₀(x); final writes are w₂(x) and w₂(z)).
    • is not view equivalent to (because r₁(x) now reads from w₂(x)), but it is view equivalent to serial → so is still VSR!
  • Negative examples (Slides 23–28):
    • Lost Update has reads-from = ∅ and final write w₁(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 initial x and w₂(x), impossible in either serial schedule → not VSR.
    • Ghost Update (a) has r₁(y) reading initial y while r₁(z) reads from w₂(z), matching neither serial schedule → not VSR.

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:

  1. Both operate on the same object x.
  2. At least one is a write: Read-Write (RW / WR) or Write-Write (WW) (two reads RR never conflict).
  3. 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)

  1. Create one node per transaction.
  2. Add a directed edge whenever an action of conflicts with and precedes an action of .
  3. 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 (y before writes, z after 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.