Professional Documents
Culture Documents
Assignment
Assignment
On Concurrency Control
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.
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):
Transaction 2 (Anteneh):
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.
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.
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.
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.