Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Department Of Computer Science

Advanced Database Management System Assignment

On Concurrency Control

Submitted by Michael Belete

Submitted to Ashenafi

Oct 2023
Concurrency Control

Concurrency control in a database is a set of techniques and mechanisms that ensure data
consistency and integrity when multiple users or processes access and modify the same data
concurrently. In a multi-user database system, it's essential to manage concurrent access to
prevent problems such as data corruption, inconsistencies, and conflicts between transactions.
Here are some key concepts and methods related to concurrency control in databases:

1. Transactions
A transaction is a logical unit of work that consists of one or more database operations
(e.g., read, write, update) that should be executed as a single, indivisible unit.
Concurrency control primarily focuses on managing concurrent access to transactions.
2. Isolation Levels: Isolation levels define the degree of isolation between concurrent
transactions. The four standard isolation levels in databases are:
 Read Uncommitted: Allows transactions to read uncommitted changes by other
transactions.
 Read Committed: Ensures that transactions only see committed changes made by
other transactions.
 Repeatable Read: Guarantees that, within a transaction, data read remains
consistent throughout the transaction.
 Serializable: Provides the highest level of isolation, ensuring that concurrent
transactions do not interfere with each other, as if they were executed serially.
3. Locking: Locks are used to control access to data items to ensure that only one
transaction can modify a particular data item at a time. Common types of locks include
shared locks (for read access) and exclusive locks (for write access). Locking
mechanisms can be implemented at different granularities, such as at the row, page, or
table level.
 Two-Phase Locking (2PL): This is a widely used locking protocol where a
transaction acquires and releases locks in two phases – an "acquisition phase"
during which locks are acquired, and a "release phase" during which locks are
released. This protocol ensures that a transaction never releases a lock it has
acquired.

 Timestamp-Based Concurrency Control: Each transaction is assigned a unique


timestamp, and data items have associated timestamps. Timestamps are used to
determine the order of execution of transactions and resolve conflicts when two
transactions access the same data item.

 Optimistic Concurrency Control: In this approach, transactions are allowed to


proceed without acquiring locks initially. Conflicts are checked at the end of the
transaction, and if conflicts are detected, one or more of the conflicting
transactions may be rolled back and retried.

 Multiversion Concurrency Control: This method maintains multiple versions of a


data item to allow for concurrent reads and writes without blocking. Each version
is associated with a timestamp, and transactions read the appropriate version
based on their timestamp.

 Deadlock Detection and Resolution: Concurrency control systems often include


mechanisms for detecting and resolving deadlocks, which occur when two or
more transactions are waiting for each other to release locks.

Concurrency control plays a crucial role in ensuring data consistency and integrity
in a multi-user database environment. The choice of concurrency control
mechanisms depends on the specific requirements of the application, the
workload, and the desired trade-offs between data consistency and system
performance.

Concurrency Example

Suppose there is a bank account with a balance of $1,000, and two individuals, Michael and
Anteneh, both want to transfer money from the account simultaneously.

Transaction 1 (Michael):

Read the account balance: $1,000.

Deduct $200 for a purchase.

Write the updated balance: $800.

Transaction 2 (Anteneh):

Read the account balance: $1,000.

Deduct $100 for an online payment.

Write the updated balance: $900.


Without proper concurrency control, issues can arise. Let's consider two scenarios:

Scenario 1: No Concurrency Control (Race Condition)

If there is no concurrency control in place, both Michael and Anteneh can access the account
balance simultaneously, leading to a race condition:

Michael reads the balance ($1,000) and deducts $200, resulting in a new balance of $800.

At the same time, Anteneh also reads the balance ($1,000) and deducts $100, resulting in a new
balance of $900.

Now, the account balance is $800 according to Michael and $900 according to Anteneh, even
though only $300 has been spent. This is a data inconsistency issue.

Scenario 2: With Concurrency Control (e.g., Using Locks)

With concurrency control mechanisms, the situation is controlled to avoid data inconsistency:

Michael requests and acquires an exclusive lock on the account to perform her transaction.

She reads the balance ($1,000) and deducts $200, resulting in a new balance of $800.

Michael releases the lock on the account.

Anteneh requests and acquires an exclusive lock on the account to perform his transaction.

He reads the balance ($1,000) and deducts $100, resulting in a new balance of $900.

Anteneh releases the lock on the account.

In this case, Michael's transaction occurs sequentially with respect to Anteneh's. The account
balance is updated correctly, and there's no data inconsistency issue. The locks ensure that only
one transaction can access and modify the account at a time.

Concurrency control, in this example using locks, prevents the race condition and guarantees
data consistency by ensuring that only one transaction can modify the shared data at any given
moment. This helps maintain the integrity of the database and ensures that the final state of the
data accurately reflects the sequence of transactions.

You might also like