Two-Phase Locking and serializability
how locking enforces serial-equivalent schedules.
a growing phase only acquires locks, a shrinking phase only releases, no lock taken after one is freed.
confusing 2PL with the two-phase commit protocol.
What's really being asked
The interviewer checks whether you understand a concurrency-control protocol that guarantees serializability, and whether you can keep it distinct from two-phase commit.
The full answer
Two-Phase Locking governs the order in which a transaction acquires and releases locks. It has two phases. In the growing phase the transaction may acquire locks but may not release any. The moment it releases its first lock it enters the shrinking phase, during which it may release locks but may never acquire a new one. This single rule, no acquisition after any release, ensures that the resulting schedule is conflict-serializable, meaning equivalent to some serial order of the transactions. A common refinement is strict 2PL, which holds all exclusive locks until the transaction commits or aborts; this additionally prevents cascading aborts and dirty reads, at the cost of holding locks longer. 2PL can itself cause deadlocks, since transactions may wait on each other's locks, so it pairs with deadlock detection.
The mistakes people make
Confusing 2PL with two-phase commit, an unrelated protocol for atomic commit across distributed nodes. Thinking 2PL prevents deadlocks; it does not, it can cause them. Believing basic 2PL alone prevents cascading aborts, which is actually a property of strict 2PL.
What usually comes next
How does strict 2PL differ from basic 2PL? Why does 2PL risk deadlock? What is conflict-serializability? How does 2PL compare with MVCC-based approaches?
A concrete example
A transfer locks both accounts during its growing phase, performs the debit and credit, and only then, in its shrinking phase, releases the locks. Because it never grabs a new lock after releasing one, no other transaction can interleave in a way that produces a non-serializable result.
Interview question
Under Two-Phase Locking, what action is forbidden once a transaction has released its first lock?
- a.Acquiring any new lockCorrect
- b.Reading already-locked rows
- c.Committing the transaction
- d.Releasing any further locks
Why? this is the answer
After the first release the transaction is in the shrinking phase and may only release, never acquire, which is what guarantees a conflict-serializable schedule. Continuing to release locks and committing are both allowed in the shrinking phase.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
- #databases
- #two-phase-locking
- #serializability
- #concurrency
- #locking
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on databases — each one lists the topics its interview covers.
See open roles