Demystifying Concurrency Control Protocols!

Harsh MangeHarsh Mange
5 min read

Concurrency control protocols (CCP) are mechanisms that are used in database management systems to manage the concurrency of transactions accessing shared data. CCPs are used to ensure that multiple transactions do not interfere with each other when accessing and modifying the same data, and to maintain the consistency and correctness of the data in the database.

There are several types of concurrency control protocols, including locking, timestamp ordering, and optimistic concurrency control. Here are some examples of these protocols:

  1. Lock-based Concurrency Control

    Overview

    Lock-based concurrency control is a widely used concurrency control protocol that uses locks to manage access to shared data. A lock is a mechanism that allows a transaction to gain exclusive access to a data item. When a transaction wants to access a data item, it first requests a lock on the data item. The lock manager grants the lock if the data item is not already locked by another transaction. The transaction can then read or write the data item. Once the transaction has finished accessing the data item, it releases the lock.

    Example

    Suppose we have two transactions T1 and T2 that want to access the same data item X. T1 requests a lock on X, and the lock manager grants the lock. T1 can now read or write X. T2 also wants to access X, but since X is already locked by T1, the lock manager does not grant the lock. T2 has to wait until T1 releases the lock on X.

  2. Timestamp Ordering Concurrency Control

    Overview

    Timestamp ordering is a concurrency control protocol that uses timestamps to manage access to shared data. Each transaction is assigned a unique timestamp that reflects its start time. When a transaction wants to access a data item, it first compares its timestamp with the timestamps of other transactions that have accessed the data item. If the timestamp of the transaction is greater than the timestamps of other transactions, the transaction can access the data item. Otherwise, the transaction has to wait until the other transactions have finished accessing the data item.

    Example

    Suppose we have two transactions T1 and T2 that want to access the same data item X. T1 has a timestamp of 10, and T2 has a timestamp of 12. T1 requests access to X, and the timestamp manager grants the access. T1 can now read or write X. T2 also wants to access X, but since T1 has already accessed X with a lower timestamp, T2 has to wait until T1 has finished accessing X.

  3. Optimistic Concurrency Control

    Overview

    Optimistic concurrency control is a concurrency control protocol that assumes that conflicts between transactions are rare, and therefore, transactions are allowed to access shared data without obtaining locks or timestamps. When a transaction wants to commit, it checks if there are any conflicts with other transactions that have accessed the same data items. If there are no conflicts, the transaction commits. If there are conflicts, the transaction is rolled back and restarted with a new set of data values.

    Example

    Suppose we have two transactions T1 and T2 that want to access the same data item X. T1 reads X and modifies it to a new value. T2 also reads X and modifies it to a different value. When T1 and T2 commit, the optimistic concurrency control protocol detects a conflict between the two transactions. The protocol rolls back one of the transactions and restarts it with the new value of X.

  4. Two-Phase Locking (2PL)

    Overview

    Two-Phase Locking is a concurrency control protocol used to ensure the consistency and isolation of transactions in a database. It is based on the concept of locks, which are mechanisms to control access to shared resources.

    Phases

    • Growing Phase (First Phase): In this phase, transactions can acquire locks but cannot release any.

    • Shrinking Phase (Second Phase): Once a transaction releases a lock, it cannot acquire any new locks.

Types of Locks

  • Shared Lock (S): Allows multiple transactions to read but not write the resource.

  • Exclusive Lock (X): Allows a transaction to both read and write the resource.

Example

Consider two transactions, T1 and T2, and a shared resource R.

  1. Growing Phase

    • T1 acquires a shared lock on R (S(R)).

    • T2 attempts to acquire an exclusive lock on R but is blocked since T1 holds a shared lock.

  2. Shrinking Phase

    • T1 releases its shared lock on R.

    • T2 acquires an exclusive lock on R (X(R)).

This example illustrates the basic principles of 2PL. Transactions acquire locks before accessing resources and release them only after they have completed their operations.

  1. Multi-Version Concurrency Control (MVCC)

    Overview

    MVCC is a concurrency control method that allows multiple versions of a row to exist at the same time. It is commonly used in database systems where read and write operations occur concurrently.

    Principles

    • Read Transactions: Can access a snapshot of the database that reflects a consistent state at the start of the transaction.

    • Write Transactions: Create a new version of a row rather than updating the existing one.

Components

  • Transaction ID (TID): Unique identifier assigned to each transaction.

  • Begin Timestamp and End Timestamp: Indicate the validity period of a version.

Example

Consider two transactions, T1 and T2, and a table with a row containing a value V.

  1. Read Phase

    • T1 starts with a timestamp T1.

    • T2 starts with a timestamp T2.

    • T1 reads the value of V (version 1).

    • T2 reads the value of V (version 1).

  2. Write Phase

    • T1 updates the value of V and creates version 2.

    • T2 updates the value of V and creates version 3.

  3. Commit Phase

    • T1 commits, and its changes become visible to subsequent transactions.

    • T2 commits, and its changes become visible.

MVCC allows both transactions to read and write concurrently without blocking each other. Each transaction works with a snapshot of the data, ensuring consistency and isolation.

In summary, concurrency control protocols are essential to ensure the correctness of database transactions in a concurrent environment. The choice of protocol depends on the characteristics of the system and the requirements of the application.

0
Subscribe to my newsletter

Read articles from Harsh Mange directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harsh Mange
Harsh Mange

This is Harsh Mange, working as a Software Engineer - Backend at Argoid. I love building apps. Working on contributing to open-source projects and the dev community.