CSE Roadmap

Core tier · about 2 hours · 5 parts

Part 4 of 5 · 2 min read

Why two-phase locking guarantees serializability

The growing-then-shrinking discipline rules out the kind of lock pattern that would create a cycle in the precedence graph.

Why two-phase locking guarantees serializability

Think of why the construction crew's strict rule prevents chaos. If every crew member finished acquiring everything before anyone started returning anything, two crews could never end up each waiting on a resource the other still holds. A circular, unresolvable wait simply cannot form.

In plain words: it is a proven result that any schedule produced under two-phase locking is conflict serializable, in the sense defined two pages ago using the precedence graph. The intuition is that the growing-then-shrinking discipline prevents the specific interleaved lock-acquisition patterns that would otherwise create a cycle in the precedence graph; by the time a transaction starts releasing locks, it has already acquired everything it needs, closing off the possibility of a conflicting transaction interleaving in a way that could form a cycle back. This result is why two-phase locking remains one of the standard, foundational techniques for implementing serializable isolation.

A worked example. Two transactions each acquiring all their needed locks before releasing any, under two-phase locking, cannot produce the same cyclic conflict pattern that an uncontrolled, freely-interleaved acquire-and-release pattern could produce.

The key lines. No new SQL; this guarantee is a theoretical property of the protocol, underlying why databases that implement it can claim to provide serializable isolation.

Two-phase locking's serializability guarantee

The serializability guarantee of two-phase locking is the proven result that any schedule it produces is conflict serializable, because its growing-then-shrinking discipline rules out the lock-acquisition patterns that would otherwise create a precedence-graph cycle.

Example: a construction rule preventing crews from ever circularly waiting on each other's still-being-returned resources mirrors how two-phase locking rules out cyclic conflicts.

Remember this

Shared locks allow concurrent reads; exclusive locks require sole access for writes; two-phase locking's growing-then-shrinking discipline is what turns ordinary locking into a guarantee of serializability, not just a way to avoid simultaneous writes.

Quick notes

  • A shared lock allows concurrent reads; an exclusive lock requires sole access for a write.
  • Two-phase locking splits a transaction's lock activity into a growing phase and a shrinking phase.
  • Once a transaction releases any lock, it may not acquire any new lock afterward.
  • Any schedule produced under two-phase locking is proven to be conflict serializable.
  • This is why two-phase locking is a foundational technique for implementing serializable isolation.