# Locking Protocols

> A locking protocol is a set of rules governing when a transaction may acquire and release locks on data. Two-Phase Locking (2PL), the most important one, splits a transaction into a growing phase that only acquires locks and a shrinking phase that only releases them, which guarantees conflict serializable schedules.

Canonical: https://devsunite.com/resources/core-subjects/dbms/locking-protocols
Last updated: 2026-11-22

Subject: DBMS · Category: Transaction Processing · 3 min read

A locking protocol is a set of rules governing when a transaction is allowed to acquire and release locks on data. The most important one, Two-Phase Locking (2PL), is specifically designed to guarantee that any schedule produced under it is conflict serializable, making it one of the foundational techniques behind real database concurrency control.

## Two-Phase Locking, the core idea

2PL splits every transaction's lifetime into exactly two phases:

1. **Growing phase**: the transaction can acquire new locks, but cannot release any.
2. **Shrinking phase**: the transaction can release locks, but cannot acquire any new ones.

Once a transaction releases its first lock, it has entered the shrinking phase and can never acquire another lock for the rest of its life. This single rule (acquire everything you need before you release anything) is what mathematically guarantees the resulting schedule is conflict serializable.

## A worked example

```
T1: acquires lock on A (growing)
T1: acquires lock on B (growing)
T1: releases lock on A (shrinking begins)
T1: tries to acquire lock on C  -- NOT ALLOWED, already in shrinking phase
```

Once T1 released its lock on `A`, 2PL forbids it from acquiring any new lock, including on `C`, even though `C` is unrelated to `A`. This might feel overly strict for this specific case, but the rule has to be unconditional to guarantee serializability holds for every possible transaction, not just ones that happen to look safe.

## Strict Two-Phase Locking

Basic 2PL guarantees serializability, but can still allow cascading rollbacks: if T1 releases a lock early (during its own shrinking phase) and T2 then reads that data before T1 actually commits, and T1 later has to abort, T2's read was based on data that's now invalid too. Strict 2PL fixes this by holding every exclusive (write) lock until the transaction commits or aborts, so nobody can read data written by a transaction that hasn't finished yet. (Holding every lock, shared ones included, until commit is called rigorous 2PL.) Most real production databases use strict 2PL, or an equivalent guarantee, specifically to avoid cascading rollbacks.

## Where this matters in real systems

Two-Phase Locking, in its strict form, underlies the default concurrency control in many traditional relational databases. It's also directly responsible for a well-known real operational problem: deadlocks, if T1 holds a lock T2 wants and T2 holds a lock T1 wants, both transactions wait forever unless the database detects and breaks the cycle (typically by aborting one of the two transactions), a genuinely common occurrence in busy, lock-heavy systems that every backend engineer working with transactions eventually has to handle.

## What interviewers actually ask about locking protocols

- **"What are the two phases in Two-Phase Locking?"** The growing phase, where locks can be acquired but not released, and the shrinking phase, where locks can be released but no new ones acquired.
- **"What does 2PL guarantee, and why?"** Conflict serializability, because the strict acquire-everything-before-releasing-anything rule prevents the specific interleavings that would make a schedule non-serializable.
- **"What's the difference between basic 2PL and strict 2PL?"** Basic 2PL can still allow cascading rollbacks if locks are released before commit; strict 2PL holds all locks until commit or abort, preventing that.
- **"How can 2PL lead to a deadlock?"** If two transactions each hold a lock the other needs, and both wait for the other to release it, neither can proceed, a genuine cycle the database has to detect and break.

## Key takeaways

- A locking protocol governs when a transaction can acquire and release locks.
- Two-Phase Locking splits a transaction into a growing phase (acquire only) and a shrinking phase (release only), guaranteeing conflict serializability.
- Strict 2PL holds all locks until commit or abort, avoiding cascading rollbacks that basic 2PL can still allow.
- 2PL is also the direct cause of deadlocks, which the database must detect and resolve by aborting one of the involved transactions.

## Related

- [Previous: Problems in Concurrency Control](https://devsunite.com/resources/core-subjects/dbms/problems-in-concurrency-control)
- [Next: Deadlocks in Transaction Processing](https://devsunite.com/resources/core-subjects/dbms/deadlocks)
- [More in Transaction Processing: What Is Transaction Processing?](https://devsunite.com/resources/core-subjects/dbms/what-is-transaction-processing)
- [More in Transaction Processing: Schedules and Their Types](https://devsunite.com/resources/core-subjects/dbms/schedules-and-types)
- [More in Transaction Processing: Serializability](https://devsunite.com/resources/core-subjects/dbms/serializability)
- [More in Transaction Processing: What Is Concurrency Control?](https://devsunite.com/resources/core-subjects/dbms/what-is-concurrency-control)
- [More in Transaction Processing: Recoverability of Schedules](https://devsunite.com/resources/core-subjects/dbms/recoverability)
- [All 174 DBMS interview questions](https://devsunite.com/resources/core-subjects/dbms)
