Concurrency Control Techniques in DB?

The concurrency control technique is a process of managing the simultaneous execution of transactions in a shared database. It preserves the database consistency, enforces the isolation of different transactions, and resolves the conflicts which occur due to the read-write operation of transactions.

The various concurrency control techniques are

  1. Locking based protocol
  2. Timestamp based protocol
  3. Optimistic protocol
  4. Multiversion protocol

1). Locking based protocol

This protocol uses a lock that guarantees the exclusive use of data of transactions. The lock is used to restrict different transaction which is executing at the same time.

There are 3 phases of this protocol

  1. Taking the lock control and accessing the data of the transaction
  2. Complete the transaction execution
  3. Release lock

There are two types of lock available in this protocol

  1. Shared lock: It is also represented by ‘lock-S’, and allows only to read the data from the transaction.
  2. Exclusive lock: It is also represented by ‘lock-X, and allows both the read and write operation to the data from the transaction.

2). Timestamp based protocol

In Timestamp based Protocol, timestamp are the Logical Counter or System Time which is used to serialize the execution of concurrent transactions. This protocol is a unique identifier that indicate the relative stating time of the transaction. It executes all the conflicting transactions in timestamp order. The confliction occurs due to read and write operations from a number of transactions running at the same time. This protocol is generated using a System clock that stores the starting time of any transaction and the Logical counter which keeps incrementing after the assignment of every new timestamp.

The timestamp value of the new transaction is always greater than the timestamp value of the old transaction. And in this method old transaction always gets the priority over the new transactions

There are two types of transaction values

  1. Read timestamp, which is the largest timestamp of any transaction that is executed the read operation successfully on a transaction.
  2. Write timestamp, which is the largest timestamp of any transaction that is executed the write operation successfully on a transaction.

For Example

Suppose we have two transactions T1 and T2.

T1 enters in system at Time 5 and T2 enters in System at time 10.

Priority will always given to T1 and then T2

3). Optimistic protocol/Validation Based Protocol

This protocol is also called validation-based protocol which copies the transaction data locally and updates the local copy of data instead of actual data in the database.

It works in three phases

  1. Read phase: All transaction data are read and stored in a temporary variable which is also called the local variable of that transaction, after which a write operation is performed on the temporary variable.
  2. Validation phase: In this phase, a test is performed to determine if the changes of the local variable are also possible in the actual database. This phase ensures the violation-free change in the execution of a transaction.
  3. Write phase: After the validation phase passed successfully then the changes are made in the actual database.

This protocol results in less interference during the execution of transactions and is used where transactions are conflict-free and read-only.

4). Multi-Version protocol

This protocol minimizes the delay for reading operation and maintains different versions of data items. For each writes operation performs, it creates a new version of transaction data so that whenever any transaction performs read operation to read that data then the appropriate created data version is selected by the control manager to make that read operation conflict-free and successful.