DBMS Interview Questions · Question 18

How does Two-Phase Locking help maintain serializability?

Interview preparation resource from Gate Smashers.

Interview-ready answer

Two-Phase Locking (2PL) enforces conflict serializability by forcing each transaction to have a growing phase (it may acquire locks but not release any) followed by a shrinking phase (it may release locks but not acquire any). Because every transaction follows this ordering rule, resulting schedules are conflict-serializable. Common variants: Basic 2PL (growing then shrinking), Strict 2PL (hold write/exclusive locks until commit or rollback, preventing access to uncommitted writes and simplifying recovery), and Conservative 2PL (attempt to acquire all required locks before execution to avoid deadlocks, at the cost of reduced concurrency).

DBMS Interview Questions diagram explaining How does Two-Phase Locking help maintain serializability
Understand it clearly

Core mechanism

Two-Phase Locking controls when a transaction may acquire and release locks by dividing its execution into two phases. This enforced ordering prevents interleavings that would violate conflict-serializability.

  • Growing phase: The transaction may acquire new locks, but it cannot release any lock.
  • Shrinking phase: The transaction may release locks, but it cannot acquire any new lock.

Why this yields serializability

Because every transaction follows the same ordering rule (acquire-only then release-only), the set of schedules produced is conflict-serializable. The enforced order prevents cycles of conflicting operations that would make a schedule non-serializable.

Common variants

Several variants of 2PL adjust lock-holding behavior to trade off concurrency, deadlock risk, and recovery complexity.

  • Basic 2PL: Growing phase followed by shrinking phase.
  • Strict 2PL: Hold write/exclusive locks until commit or rollback.
  • Conservative 2PL: Acquire required locks before execution begins.

Effects and trade-offs

Basic 2PL guarantees serializability but can still allow deadlocks because transactions may wait for each other's locks. Strict 2PL is a stronger variant that prevents other transactions from reading or overwriting uncommitted writes and simplifies recovery by holding exclusive locks until commit or rollback. Conservative 2PL attempts to avoid deadlocks by acquiring all required locks up front, but doing so can reduce concurrency.

Quick comparison
BasisVariantDetails
Basic 2PLGrowing phase followed by shrinking phaseGuarantees conflict serializability but can still allow deadlocks
Strict 2PLHold write/exclusive locks until commit or rollbackPrevents other transactions from reading/overwriting uncommitted writes and simplifies recovery
Conservative 2PLAcquire required locks before execution beginsAvoids deadlocks but may reduce concurrency