Instiq
Chapter 4 · Transactions & concurrency control·v1.0.0·Updated 7/10/2026·~16 min

What's changed: Initial version

4.2Concurrency control and locking

Key points

Covers the choice of lock granularity via shared locks (S locks) and exclusive locks (X locks), and the growing phase and shrinking phase of the two-phase locking protocol (2PL), which guarantees serializability, building the judgment needed to choose the optimal concurrency-control approach from the tradeoff between throughput and consistency.

For a DBA designing a system in which multiple business processes access the same inventory or account table concurrently, deciding "at what granularity, and with what kind of lock" is a central judgment that balances data integrity against throughput. Locking coarsely and for a long time makes integrity robust but reduces concurrency; locking finely and briefly raises throughput but increases the risk of compromising integrity. This section builds the judgment needed to decide how to design the concurrency-control approach.

4.2.1Shared locks and exclusive locks

  • A shared lock (S lock) is acquired for reads (SELECT); multiple transactions can hold an S lock on the same resource at the same time (reads do not conflict with each other). An exclusive lock (X lock) is acquired for writes (UPDATE/INSERT/DELETE) and cannot be held concurrently with any other lock (S or X)—it is exclusive. If a resource already holds an X lock, any other transaction requesting either an S lock or an X lock on it must wait.
  • Lock granularity is the scope of the locked target—row-level, page-level, or table-level. A row lock is fine-grained and offers high concurrency, but increases the number of locks and management overhead. A table lock is coarse-grained and lightweight to manage, but concurrency is low because the entire table is the target. Many RDBMSes automatically perform lock escalation to a table lock when they detect an update pattern likely to cause heavy row-lock contention.

4.2.2Two-phase locking and serializability

  • The two-phase locking protocol (2PL) splits each transaction's lock acquisitions and releases into two phases. In the growing phase, only acquiring new locks is allowed; no locks are released. As soon as even one lock is released, the transaction enters the shrinking phase, after which only releasing locks is allowed and no new lock may be acquired—once the shrinking phase begins, the transaction can never return to the growing phase, which is the core of the rule.
  • Any schedule produced by transactions that all follow 2PL is theoretically guaranteed to be serializable (the result of running multiple transactions concurrently is equivalent to running them one at a time in some order). Note, however, that 2PL alone does not prevent deadlock (detailed in the next section)—serializability and deadlock-freedom are distinct properties. In practice, Strict 2PL, which releases all locks together at COMMIT/ROLLBACK, is widely used, since it also prevents cascading rollbacks (cascading aborts).
Exam point

Most-tested: "S lock = for reads, shareable across multiple Tx", "X lock = for writes, exclusive against any other lock", "2PL growing phase = acquisition only", "2PL shrinking phase = release only, cannot return to growing phase", and "2PL guarantees serializability but does not prevent deadlock". Watch for the misconception that "using 2PL also prevents deadlock."

Suppose a DBA for an online seat-reservation system is investigating a throughput drop during a peak-access period. The investigation reveals that both the SELECT that checks seat availability and the UPDATE that reserves a seat are acquiring a table lock scoped to the entire seat table (table-level granularity), which is the cause. The design decision to make here is to narrow the lock granularity to row locks (one seat per row)—transactions updating the same seat row still block each other, but queries and updates on different seats no longer wait on each other, substantially improving concurrency. However, narrowing to row locks alone does not guarantee serializability, so it is also necessary to check whether each transaction follows the two-phase locking protocol (2PL)—specifically, whether checking availability (acquiring an S lock) and reserving the seat (acquiring an X lock) happen within a single transaction, going through a growing phase in which no lock is released until all needed locks are acquired, followed by releasing them all together at COMMIT, i.e., a strict 2PL implementation. A common mistake to avoid here is the design of "releasing the lock immediately after the seat-reservation UPDATE completes, since releasing locks early should raise throughput, and then separately updating an inventory summary afterward"—acquiring a new lock within the same transaction after having already released one and entered the shrinking phase violates the 2PL rule, breaking serializability and risking an inconsistency such as a double booking.

Lock type / phaseBehavior
Shared lock (S)For reads; multiple Tx can hold concurrently
Exclusive lock (X)For writes; exclusive against any other lock
2PL growing phaseAcquisition only; no release
2PL shrinking phaseRelease only; no new acquisition; cannot return to growing phase
Warning

Trap: "following the two-phase locking protocol means deadlock cannot occur" is wrong—what 2PL guarantees is serializability, a property distinct from deadlock-freedom (deadlock requires the detection/avoidance measures covered in the next section). Also wrong: "it is fine to acquire another new lock within a transaction after releasing one"—acquiring a new lock after entering the shrinking phase violates 2PL, breaking serializability.

Shared/exclusive locks, 2PL.
Serializing concurrent execution

4.2.3Section summary

  • A shared lock (S) is for reads and shareable; an exclusive lock (X) is for writes and exclusive against all other locks
  • Lock granularity is row/page/table level—finer granularity raises concurrency but increases management overhead
  • Two-phase locking (2PL) guarantees serializability via a growing phase (acquisition only) followed by a shrinking phase (release only), but deadlock requires separate countermeasures

Sign in to track progress — Log in.

Quick check

(just a quick review)

Q1. In an online seat-reservation system, both the seat-availability query and the seat-reservation update acquire a table lock scoped to the entire seat table, and throughput drops during peak periods. Which is the most appropriate countermeasure?

Q2. A transaction is implemented following the two-phase locking protocol (2PL). Immediately after acquiring an X lock to reserve a seat, it releases that lock, and then within the same transaction attempts to acquire a new X lock to update an inventory summary. What is the problem with this design?

Q3. In a system where multiple transactions frequently update different rows of the same customer table, row locks were adopted, but occasionally when update frequency spikes, the DBMS was observed to automatically switch to a table lock. What is the most appropriate name and purpose for this behavior?

Check your understandingPractice questions for Chapter 4: Transactions & concurrency control

Keep track of your progress

The full study guide is free to read. Sign up free to practice with the question bank, track what you have read, review your mistakes, and highlight passages.