Two-Phase Locking (2PL)
maang.io System Design Series
Two-phase locking is the textbook mechanism for serializable transactions: a transaction first only acquires locks (the growing phase), then only releases them (the shrinking phase) — never acquiring after releasing. That discipline guarantees a serializable schedule, at the cost of deadlocks, which the database detects and breaks. It's the counterpoint to MVCC and a pillar of the ACID story.
This deep dive comes in three parts:
- Foundations & Core Concepts — shared/exclusive locks, the two phases, and strict 2PL.
- Internals — deadlock detection, predicate locks for phantoms, and the MVCC contrast, with worked numbers.
- Interview — Staff/Principal Q&A: isolation levels, deadlock handling, and locking vs MVCC.