Concurrency Control
Concurrency Control is a fundamental mechanism in a database management system (DBMS) that coordinates simultaneous execution of operations from multiple transactions to ensure the correctness and consistency of the database while maximizing system throughput and responsiveness.
This guide covers concurrency anomalies, lock-based protocols (including Two-Phase Locking), timestamp ordering, Multi-Version Concurrency Control (MVCC), deadlock handling, SQL isolation levels, walkthroughs, and a 10-question practice quiz.
1Introduction
Without proper concurrency control, concurrent transactions can interfere with each other, leading to data inconsistencies and violating the ACID properties (Atomicity, Consistency, Isolation, Durability) of transactions. Specifically, concurrency control primarily addresses the Isolation property, ensuring that the concurrent execution of transactions appears as if each transaction is executing in isolation from others.
Concurrency control is a core component of the transaction manager within the DBMS architecture, working alongside the recovery manager. It ensures that the database remains in a consistent state even when multiple users or applications are accessing and modifying data concurrently.
Modern high-volume OLTP (Online Transaction Processing) systems, such as those powering e-commerce platforms, financial trading systems, and airline reservation systems, rely heavily on sophisticated concurrency control mechanisms. These systems must handle thousands of concurrent transactions per second, making efficient and correct concurrency control paramount for both performance and data integrity.
2Key Definitions
Essential terms for understanding concurrency control at the university level.
Transaction (T)
A logical unit of work: an atomic sequence of read/write operations that must execute entirely or not at all.
Schedule (S)
A sequence of operations from concurrent transactions indicating relative execution order.
Serial Schedule
Operations of different transactions are not interleaved -- transactions execute one after another.
Serializable Schedule
Equivalent to some serial schedule -- the gold standard for concurrent execution correctness.
Conflict
Two operations from different transactions on the same data item where at least one is a write (RW, WR, or WW).
Shared Lock (S-lock)
Allows multiple transactions to read concurrently. No writes permitted while S-locks are held.
Exclusive Lock (X-lock)
Grants exclusive access for reading and writing. No other locks (S or X) can be held simultaneously.
Deadlock
Two or more transactions indefinitely waiting for each other to release locks -- a circular wait.
Two-Phase Locking (2PL)
Protocol with growing phase (acquire locks) and shrinking phase (release locks) ensuring conflict serializability.
Timestamp Ordering (T/O)
Assigns unique timestamps to transactions and orders operations accordingly. Deadlock-free.
MVCC
Maintains multiple versions of data items so readers never block writers and vice versa.
Snapshot Isolation (SI)
Each transaction reads from a consistent snapshot at its start time. Prevents most anomalies but allows write skew.
3Concurrency Problems
Without proper concurrency control, concurrent execution of transactions can lead to various anomalies, violating database consistency and the Isolation property. Here are the four classic problems:
Lost Update (WW Conflict)
One transaction's write is overwritten by another transaction that read the old value.
Example: Account A =