View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
  1. Home
DBMS

DBMS Tutorial: Learn Database Management Basics

Learn the fundamentals of Database Management Systems (DBMS) with our comprehensive tutorial. Understand key concepts and practical applications. Start now!

  • 32
  • 8 Hours
right-top-arrow
32

Concurrency Control in DBMS

Updated on 07/10/2024550 Views

You must ensure the safety and consistency of your data when managing databases, particularly in cases where many people are using it concurrently. That is where concurrency control comes in. It makes sure everything runs smoothly in the database management system (DBMS) even when lots of things are happening at once.

The management of concurrent transactions becomes more and more important as the need for accessing a database concurrently increases. Today, there are many web-based applications and mobile devices, so the volume of concurrent database transactions has skyrocketed.

According to recent studies, the average number of global internet users has surpassed 5.4 billion. With such a massive user base interacting with databases daily, the importance of robust concurrency control mechanisms cannot be forgotten.

In this guide, I will explain what concurrency control is all about. We will talk about several methods to manage concurrent transactions, effectively.

Overview

Concurrency control serves as the guardian of data integrity in database management systems (DBMS). It mainly enables multiple users to share data, access data at the same time, and modify it without any conflict.

In this guide, I will take you through Concurrency Control in DBMS. By the time we are done with this guide, you will get familiar with several concurrency techniques that help to protect your information.

You will also understand transaction control and the problems and remediation of Concurrency Control in DBMS.

What Exactly is Concurrency Control in DBMS?

Concurrency control in database management systems (DBMS) is an essential element that guarantees that numerous processes run smoothly and without conflict. Its major goal is to guarantee data consistency, integrity, and isolation when a large number of users or applications access the database at once.

Let’s look at a concurrency control in DBMS example, in a busy online marketplace where multiple sellers are updating product listings concurrently. The importance of concurrency control in maintaining data integrity within a database becomes evident if two sellers attempt to update the same product details simultaneously,

Concurrency control is particularly essential in multi-user environments where several users may attempt to access and modify the database concurrently. Without proper control mechanisms, conflicts can arise, leading to data corruption and inconsistency.

Since Concurrency Control manages the simultaneous execution of multiple transactions. Let’s have a brief discussion on what a transaction is.

What is Transaction Control?

A transaction is a group of actions that carry out a single logical task in a database application. Each transaction is characterized by its atomicity, consistency, isolation, and durability properties.

Atomicity: Guarantees that a transaction either completes all of its actions successfully or none at all. This property guarantees that the database remains in a consistent state, even in the event of failures or interruptions.

Consistency: Guarantees that transactions do not violate any database constraints, maintaining the overall integrity of the database.

Isolation: Ensures that the execution of one transaction does not interfere with the execution of other transactions, preventing data inconsistency and concurrency anomalies.

Durability: Durability guarantees that, even in the case of system malfunctions or crashes, a transaction's modifications are kept permanently in the database after it is committed.

Transaction States

Transactions go through several states during their lifecycle:

  • Active: The initial state when a transaction is executing its operations.
  • Partially committed: The state reached when a transaction has executed all its operations successfully but has not yet been permanently saved to the database.
  • Committed: The final state where a transaction has been successfully saved to the database.
  • Aborted: The state reached when a transaction encounters an error or is explicitly rolled back, undoing any changes made by the transaction.

Why is Concurrency Control Needed in DBMS?

Using concurrency control in DBMS ensures data integrity and system efficiency. Let's have a deeper look into why concurrency control is indispensable in DBMS:

1. It Ensures Database Consistency

Picture a scenario where multiple users are simultaneously updating customer information in a database. Without concurrency control, these concurrent transactions could conflict with each other, potentially leaving the database in an inconsistent state. Concurrency control mechanisms ensure that such conflicts are resolved systematically, maintaining the overall consistency of the database.

2. Avoid Conflicting Updates

If two users concurrently attempt to reserve the last available seat on a flight. Without concurrency control, both users might end up booking the same seat simultaneously, resulting in a conflict. Concurrency control mechanisms prevent such conflicts by coordinating access to shared resources, ensuring that only one user successfully reserves the seat while the other is notified of the unavailability.

3. Prevent Dirty Reads

Consider a scenario where a user is updating their bank account balance while another user simultaneously queries the same account balance. Concurrency control mechanisms prevent such dirty reads by ensuring that transactions see only the committed state of the database, thereby maintaining data accuracy and integrity.

4. Improve System Efficiency

In a busy online marketplace, multiple users may be browsing, adding items to their carts, and checking out simultaneously. Concurrency Control allows these transactions to be processed concurrently, maximizing system efficiency and user satisfaction

5. Protect Transaction Atomicity

Transactions in a DBMS often involve multiple operations that must be treated as a single, atomic unit.

For example, transferring funds from one bank account to another involves deducting the amount from the sender's account and crediting it to the recipient's account. Concurrency control ensures that these operations are executed atomically, meaning either all operations within a transaction succeed or none do.

This atomicity guarantees data consistency and prevents partial updates or inconsistencies in the database.

6. Maintain Data Isolation and Integrity

In a multi-user environment, it is crucial to ensure that each transaction operates on a consistent view of the data.

Concurrency control mechanisms enforce isolation levels to prevent transactions from interfering with each other. For instance, by using locking mechanisms, transactions can acquire exclusive access to data they intend to modify, preventing other transactions from accessing it concurrently and ensuring data integrity.

7. Scalability

Scalability becomes essential when a system has more users and transactions. Concurrency control mechanisms allow database systems to scale efficiently by facilitating parallel execution of transactions without compromising data integrity. By efficiently managing concurrent access to resources.

Concurrency Control Techniques in DBMS

As discussed, concurrency control techniques play an important role in maintaining data consistency and integrity in Database Management Systems (DBMS). Let's discuss four key concurrency control techniques in DBMS:

1. Two-phase Locking Protocol

The Two-phase Locking (2PL) protocol operates in two distinct phases during each transaction: the Growing phase and the Shrinking phase.

In the Growing phase, a transaction can acquire multiple locks but cannot release any until it has obtained all necessary locks. Once the transaction releases its first lock, it enters the Shrinking phase, where it can release locks but cannot acquire new ones. The transition from the Growing phase to the Shrinking phase is known as the lock point. This protocol ensures conflict-serializability, preventing transactions from interfering with each other in ways that could lead to inconsistent results.

For example, imagine a banking application where multiple users are transferring funds simultaneously. Without proper concurrency control, two transactions might attempt to withdraw money from the same account concurrently, leading to a potential race condition and resulting in inaccurate balances.

The Two-phase Locking Protocol ensures that such conflicts are avoided by carefully managing the acquisition and release of locks.

2. Timestamp Ordering Protocol

The Timestamp Ordering Protocol assigns a unique timestamp to each transaction upon its initiation. Older timestamp transactions are prioritized above more recent timestamp transactions.

If two transactions attempt to access the same data item concurrently, the transaction with the older timestamp is granted access, while the newer transaction is blocked until the older one completes its operation. This protocol utilizes two key timestamps: The Read Timestamp (RTS) and the Write Timestamp (WTS).

  • The RTS represents the latest timestamp of a transaction that has read a data item,
  • while the WTS represents the latest timestamp of a transaction that has written or updated the data item. By using these timestamps to determine access rights, these timestamp ordering protocols ensure a consistent ordering of operations, thereby preventing deadlocks and maintaining data consistency.

For instance, consider an online shopping platform where multiple users are attempting to purchase the last available item in stock. Without concurrency control, simultaneous transactions might incorrectly update the inventory, leading to overselling. However, the Timestamp Ordering Protocol ensures that only one transaction can update the inventory at a time, based on the timestamps assigned to each transaction.

3. Multi-Version Concurrency Control (MVCC)

MVCC is a clever strategy used in DBMS to sidestep conflicts between concurrent operations by creating multiple versions of data items. Look discuss how it works:

Multiple Versions: Instead of directly modifying data items, MVCC creates new versions of them when transactions make changes. This means that different versions of the same database object can coexist simultaneously.

Reads are not Blocked: One of the key perks of MVCC is that read operations are not held up by write operations. Even if a transaction is modifying a data item, other transactions can still read a version of that item consistent with their last access.

Timestamps or Transaction IDs: Each version of a data item is stamped with a unique identifier, usually a timestamp or transaction ID. This identifier determines which version of the data item a transaction sees when accessing it, ensuring consistency.

Garbage Collection: As transactions churn out newer versions of data items, older versions can become obsolete. There is typically a background process that tidies up these outdated versions, known as "garbage collection".

Conflict Resolution: In cases where two transactions attempt to modify the same data item simultaneously, conflict resolution becomes crucial. Various systems employ different methods for this. For instance, the first committing transaction might succeed while the other is either rolled back or requires conflict resolution.

4. Validation Concurrency Control (VCC)

VCC, also known as Optimistic Concurrency Control, takes a different approach compared to traditional methods. Instead of locking data items, it allows transactions to operate on private copies and validates them only during commit. Transactions in VCC undergo three phases:

  • Read Phase: Transactions read data from the database and make changes to private copies without affecting the actual database.
  • Validation Phase: Before committing, transactions verify if their changes can be safely written to the database without conflicts.
  • Write Phase: If validation passes, transactions update the database with changes made to their private copies.

During validation, the system scrutinizes for potential conflicts with other transactions. If conflicts arise, the system may opt to roll back the transaction or postpone it for a retry, depending on the chosen strategy.

Concurrency Control Problems in DBMS

When you have multiple transactions concurrently accessing the same data in a Database Management System (DBMS), it opens the door to a host of potential issues for concurrency control in DBMS. Without proper regulation, these simultaneous accesses can lead to several concurrency control problems. Let's look at some of these problems in DBMS:

Uncommitted Data

This problem occurs when one transaction reads data that another transaction has modified but not yet committed to the database. If the modifying transaction aborts, the changes are rolled back, leaving the first transaction with inconsistent or invalid data.

Inconsistent Retrievals

This problem arises when a transaction reads multiple values from the database, but some of those values are modified by another transaction before the first transaction completes

Deadlocks

Deadlocks occur when two or more transactions are waiting for each other to release resources, causing them to remain in a state of indefinite waiting. In a database context, deadlocks can happen when two transactions lock rows in opposite orders, causing each to wait for the other to release its locks.

Data Corruption

Data corruption occurs when multiple transactions attempt to modify the same data simultaneously, leading to inconsistent or erroneous results. For instance, if two users simultaneously update the same record in a database without proper concurrency control, one user's changes might overwrite the other's, resulting in corrupted data.

Phantom Read Problem

The phantom read problem occurs when a transaction retrieves a set of data twice but finds additional rows in the second read that were not present in the first read.

For instance, consider a transaction that retrieves all orders placed by a customer. If another transaction inserts a new order for the same customer between the two reads, the first transaction might see the new order in the second read, resulting in a phantom read.

Lost Update Problem

This problem occurs when two or more transactions attempt to update the same data concurrently, but one transaction's updates are lost because they are overwritten by another transaction before they can be committed.

Incorrect Summary Problem

This problem occurs when two transactions produce the same data summary but use different versions of the data, leading to inconsistent or incorrect results.

Dirty Read Problem

A dirty read happens when one transaction accesses data that has been updated by another but has not yet been committed. For example, if one user updates a customer's address, another user might read the updated address before the first user commits the changes. If the first user later aborts their transaction, the second user would have read invalid data.

Unrepeatable Read Problem

This problem occurs when a transaction reads the same data twice but receives different values each time because another transaction modified the data between the reads. For instance, if one user retrieves a product's price and then retrieves it again, but another user updates the price between the two reads, the first user would see inconsistent prices.

Measures to Address Concurrency Control Problems in DBMS

Steps can be taken to ensure that when multiple transactions access the same data simultaneously, they don't interfere, thus preventing data inconsistencies or inaccurate outcomes. The four techniques earlier discussed do help tackle most of the problems discussed above. Now, let's discuss more key remediation measures:

Locking

Prevents multiple users from accessing the same data simultaneously, using shared and exclusive locks to manage access. For example, in a banking application, exclusive locks ensure that only one withdrawal operation is processed at a time, preventing conflicts in the account balance.

Isolation Levels

This dictates the degree of concurrency and data consistency, providing different balances between concurrency and data integrity.

Optimistic Concurrency Control

This assumes conflicts between transactions are rare, allowing many transactions to run concurrently without acquiring locks initially. Conflicts are resolved before committing.

Serializability

This ensures that transaction execution mimics a serial execution, preventing data abnormalities and maintaining database integrity.

For example, in an airline seat reservation system, serializability prevents conflicts in seat assignments when multiple users book the same seat.

Deadlock Detection

Deadlock detection Identifies and resolves deadlocks by analyzing resource allocation graphs, ensuring smooth resource allocation.

For example, in a resource management system, deadlock detection prevents conflicts in resource allocation during peak usage.

Application Design and Tuning

This involves structuring data access patterns, transaction boundaries, and optimization of queries and performance-related features to minimize conflicts and improve concurrency.

Final Words!

Concurrency control ensures the smooth operation of multiple tasks without conflicts. It helps avoid conflicting updates and maintain data isolation and integrity in DBMS

Various techniques exist to tackle concurrency challenges head-on. They are the Two-phase Locking Protocol, the Timestamp Ordering Protocol, Multi-Version Concurrency Control (MVCC), and Validation Concurrency Control (VCC). Each technique offers its unique approach to managing simultaneous transactions. This enables efficient and reliable database operations in multi-user environments.

When you implement measures, such as locking, isolation levels, and optimistic concurrency control, you can mitigate the risks associated with concurrency problems, ensuring data consistency and accuracy.

There is no doubt that concurrency control helps manage simultaneous transactions and maintain the integrity and reliability of your data in the face of ever-increasing demands. So, concurrency in DBMS is very important and should not be overlooked.

FAQs

1. How do you control concurrency?
Concurrency is managed through techniques like locking, timestamp ordering, and multi-version concurrency control.

2. What is serializability and concurrency control in DBMS?
Serializability ensures that transaction schedules yield the same result as if transactions were executed sequentially. Concurrency control in DBMS maintains this property, preventing data anomalies and maintaining consistency.

3. What is a concurrent schedule in DBMS?
A concurrent schedule in DBMS involves multiple transactions executing simultaneously, allowing for overlapping or interleaving operations.

4. What is meant by the concurrent execution of database transactions in a multiuser system, why is concurrency control needed, and give informal examples?

Concurrent execution refers to multiple users or transactions accessing and modifying the database simultaneously. Concurrency control ensures data consistency. For example, simultaneous updates to the same bank account balance without control could lead to incorrect balances or lost transactions.

5. What is concurrency control with an example?

Concurrency control manages simultaneous access to shared resources in a database to prevent conflicts and maintain data consistency. For instance, online shopping databases with concurrency control ensure multiple users can add items to their carts and checkout without overselling or inventory discrepancies.

6. What is the basic concurrency control?

The basic concurrency control techniques include locking mechanisms, where transactions acquire locks on data items to prevent other transactions from accessing them concurrently.

Get Free Career Counselling
form image
+91
*
By clicking Submit, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

  1. upGrad facilitates program delivery and is not a college/university in itself. Credits and credentials are awarded by the university. Please refer relevant terms and conditions before applying.

  2. Past record is no guarantee of future job prospects.