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.
Two-Phase Locking (2PL) β Part 3: System Design & Interview Mastery
maang.io System Design Series
What is this?
You now know 2PL from the growing-phase rule down to the gap lock. This chapter is where that knowledge earns the offer. Interviewers don't want you to recite "2PL guarantees serializability" β they want to watch you reach for locking at the right moment, reason about deadlocks and contention out loud, and defend it against MVCC when they push. That's the senior signal.
Two things here: first where 2PL fits in real designs (with a worked mini-design), then a mock interview β for each question, what they're really asking, a model answer, and the senior line.
The one-line idea: interviewers use 2PL to test whether you can tell "this operation needs true serializability under contention" from "this is just a read," pick pessimistic locking only where it's warranted, and then live honestly with its costs β blocking, deadlocks, and reduced throughput.
Keep the library from Parts 1 and 2 in mind: collect every card you need, hold them to commit, hand them all back at once β and mind the standoffs.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson