Open In App

Concurrency Control in DBMS

Concurrency control is a very important concept of DBMS which ensures the simultaneous execution or manipulation of data by several processes or user without resulting in data inconsistency. Concurrency Control deals with interleaved execution of more than one transaction.

What is Transaction? 

A transaction is a collection of operations that performs a single logical function in a database application. Each transaction is a unit of both atomicity and consistency. Thus, we require that transactions do not violate any database consistency constraints. That is, if the database was consistent when a transaction started, the database must be consistent when the transaction successfully terminates. However, during the execution of a transaction, it may be necessary temporarily to allow inconsistency, since either the debit of A or the credit of B must be done before the other. This temporary inconsistency, although necessary, may lead to difficulty if a failure occurs.



It is the programmer’s responsibility to define properly the various transactions, so that each preserves the consistency of the database. For example, the transaction to transfer funds from the account of department A to the account of department B could be defined to be composed of two separate programs: one that debits account A, and another that credits account B. The execution of these two programs one after the other will indeed preserve consistency. However, each program by itself does not transform the database from a consistent state to a new consistent state. Thus, those programs are not transactions.

The concept of a transaction has been applied broadly in database systems and applications. While the initial use of transactions was in financial applications, the concept is now used in real-time applications in telecommunication, as well as in the management of long-duration activities such as product design or administrative workflows.



A set of logically related operations is known as a transaction. The main operations of a transaction are:

(Note: It doesn’t always need to write it to a database back it just writes the changes to buffer this is the reason where dirty read comes into the picture) 

Let us take a debit transaction from an account that consists of the following operations:

  1. R(A);
  2. A=A-1000;
  3. W(A);

Assume A’s value before starting the transaction is 5000.

But it may also be possible that the transaction may fail after executing some of its operations. The failure can be because of hardware, software or power, etc. For example, if the debit transaction discussed above fails after executing operation 2, the value of A will remain 5000 in the database which is not acceptable by the bank. To avoid this, Database has two important operations: 

For more details please refer Transaction Control in DBMS article. 

Properties of a Transaction

Atomicity: As a transaction is a set of logically related operations, either all of them should be executed or none. A debit transaction discussed above should either execute all three operations or none. If the debit transaction fails after executing operations 1 and 2 then its new value of 4000 will not be updated in the database which leads to inconsistency.

Consistency: If operations of debit and credit transactions on the same account are executed concurrently, it may leave the database in an inconsistent state.

T1 T1’s buffer space T2 T2’s Buffer Space Database
        A=5000
R(A); A=5000     A=5000
  A=5000 R(A); A=5000 A=5000
A=A-1000; A=4000   A=5000 A=5000
  A=4000 A=A+500; A=5500  
W(A);     A=5500 A=4000
    W(A);   A=5500

Isolation: The result of a transaction should not be visible to others before the transaction is committed. For example, let us assume that A’s balance is Rs. 5000 and T1 debits Rs. 1000 from A. A’s new balance will be 4000. If T2 credits Rs. 500 to A’s new balance, A will become 4500, and after this T1 fails. Then we have to roll back T2 as well because it is using the value produced by T1. So transaction results are not made visible to other transactions before it commits.

Durable: Once the database has committed a transaction, the changes made by the transaction should be permanent. e.g.; If a person has credited $500000 to his account, the bank can’t say that the update has been lost. To avoid this problem, multiple copies of the database are stored at different locations.

What is a Schedule? 

A schedule is a series of operations from one or more transactions. A schedule can be of two types: 

Serial Schedule: When one transaction completely executes before starting another transaction, the schedule is called a serial schedule. A serial schedule is always consistent. e.g.; If a schedule S has debit transaction T1 and credit transaction T2, possible serial schedules are T1 followed by T2 (T1->T2) or T2 followed by T1 ((T2->T1). A serial schedule has low throughput and less resource utilization.

Concurrent Schedule: When operations of a transaction are interleaved with operations of other transactions of a schedule, the schedule is called a Concurrent schedule. e.g.; the Schedule of debit and credit transactions shown in Table 1 is concurrent. But concurrency can lead to inconsistency in the database.  The above example of a concurrent schedule is also inconsistent.

Difference between Serial Schedule and Serializable Schedule

                           Serial Schedule                             Serializable Schedule
In Serial schedule, transactions will be executed one after other. In Serializable schedule transaction are executed concurrently.
Serial schedule are less efficient. Serializable schedule are more efficient.
In serial schedule only one transaction executed at a time. In Serializable schedule multiple transactions can be executed at a time.
Serial schedule takes more time for execution. In Serializable schedule execution is fast. 

Concurrency Control in DBMS

Concurrency Control Problems

There are several problems that arise when numerous transactions are executed simultaneously in a random manner. The database transaction consist of two major operations “Read” and “Write”. It is very important to manage these operations in the concurrent execution of the transactions in order to maintain the consistency of the data. 

Dirty Read Problem(Write-Read conflict)

Dirty read problem occurs when one transaction updates an item but due to some unconditional events that transaction fails but before the transaction performs rollback, some other transaction reads the updated value. Thus creates an inconsistency in the database. Dirty read problem comes under the scenario of Write-Read conflict between the transactions in the database

  1. The lost update problem can be illustrated with the below scenario between two transactions T1 and T2.
  2. Transaction T1 modifies a database record without committing the changes.
  3. T2 reads the uncommitted data changed by T1
  4. T1 performs rollback
  5. T2 has already read the uncommitted data of T1 which is no longer valid, thus creating inconsistency in the database.

Lost Update Problem

Lost update problem occurs when two or more transactions modify the same data, resulting in the update being overwritten or lost by another transaction. The lost update problem can be illustrated with the below scenario between two transactions T1 and T2.

  1. T1 reads the value of an item from the database.
  2. T2 starts and reads the same database item.
  3. T1 updates the  value of that data and performs a commit.
  4. T2  updates the same data item based on its initial read and performs commit.
  5. This results in the modification of T1 gets lost by the T2’s write which causes a lost update problem in the database.

Concurrency Control Protocols

Concurrency control protocols are the set of rules which are maintained in order to solve the concurrency control problems in the database. It ensures that the concurrent transactions can execute properly while maintaining the database consistency. The concurrent execution of a transaction is provided with atomicity, consistency, isolation, durability, and serializability via the concurrency control protocols.

Locked based Protocol

In locked based protocol, each transaction needs to acquire locks before they start accessing or modifying the data items. There are two types of locks used in databases.

There are two kind of lock based protocol mostly used in database:

Timestamp based Protocol

Advantages of Concurrency

In general, concurrency means, that more than one transaction can work on a system. The advantages of a concurrent system are:

Disadvantages of Concurrency 

Conclusion

Concurrency control ensures transaction atomicity, isolation, consistency, and serializability. Concurrency control issues occur when many transactions execute randomly. A dirty read happens when a transaction reads data changed by an uncommitted transaction. When two transactions update data simultaneously, the Lost Update issue occurs. Lock-based protocol prevents incorrect read/write activities. Timestamp-based protocols organise transactions by timestamp.

Important GATE Question

Question 1: Consider the following transaction involving two bank accounts x and y:

read(x);
x := x – 50;
write(x);
read(y);
y := y + 50;
write(y);

The constraint that the sum of the accounts x and y should remain constant is that of?

Atomicity
Consistency
Isolation
Durability                                                                                                                                                                                         

[GATE 2015]

Solution: 

As discussed in properties of transactions, consistency properties say that sum of accounts x and y should remain constant before starting and after completion of a transaction. So, the correct answer is B.

Article contributed by Sonal Tuteja.  


Article Tags :