Chapter 5 Concurrency Control
Chapter 5 Concurrency Control
In a multi-user database environment, it’s common for numerous users to want to access and modify
the database simultaneously. This is what we call concurrent execution. Imagine a busy library where
multiple librarians are updating book records simultaneously. Just as multiple librarians shouldn’t try
to update the same record simultaneously, database users shouldn’t interfere with each other’s
operations.
Executing transactions concurrently offers many benefits, like improved system resource utilization
and increased throughput. However, these simultaneous transactions mustn’t interfere with each other.
The ultimate goal is to ensure the database remains consistent and correct. For instance, if two people
try to book the last seat on a flight at the exact moment, the system must ensure that only one person
gets the seat.
In a database transaction, the two main operations are READ and WRITE operations. So,
there is a need to manage these two operations in the concurrent execution of the transactions
as if these operations are not performed in an interleaved manner, and the data may become
inconsistent. So, the following problems occur with the Concurrent Execution of the
operations:
Problem 1: Lost Update Problems (W - W Conflict)
The problem occurs when two different database transactions perform the read/write
operations on the same database items in an interleaved manner (i.e., concurrent execution)
that makes the values of the items incorrect hence making the database inconsistent.
For example: Consider the below diagram where two transactions TX and TY, are
performed on the same account A where the balance of account A is $300.
1|Page
At time t1, transaction TX reads the value of account A, i.e., $300 (only read).
At time t2, transaction TX deducts $50 from account A that becomes $250 (only
deducted and not updated/write).
Alternately, at time t3, transaction TY reads the value of account A that will be $300
only because TX didn't update the value yet.
At time t4, transaction TY adds $100 to account A that becomes $400 (only added but
not updated/write).
At time t6, transaction TX writes the value of account A that will be updated as $250
only, as TY didn't update the value yet.
Similarly, at time t7, transaction TY writes the values of account A, so it will write as
done at time t4 that will be $400. It means the value written by TX is lost, i.e., $250 is
lost.
The dirty read problem occurs when one transaction updates an item of the database, and
somehow the transaction fails, and before the data gets rollback, the updated database item is
accessed by another transaction. There comes the Read-Write Conflict between both
transactions.
For example:
2|Page
Consider two transactions TX and TY in the below diagram performing read/write
operations on account A where the available balance in account A is $300:
Also known as Inconsistent Retrievals Problem that occurs when in a transaction, two
different values are read for the same database item.
For example:
Consider two transactions, TX and TY, performing the read/write operations on account
A, having an available balance = $300. The diagram is shown below:
3|Page
At time t1, transaction TX reads the value from account A, i.e., $300.
At time t2, transaction TY reads the value from account A, i.e., $300.
At time t3, transaction TY updates the value of account A by adding $100 to the
available balance, and then it becomes $400.
At time t4, transaction TY writes the updated value, i.e., $400.
After that, at time t5, transaction TX reads the available value of account A, and that
will be read as $400.
It means that within the same transaction TX, it reads two different values of account
A, i.e., $ 300 initially, and after updation made by transaction TY, it reads $400. It is
an unrepeatable read and is therefore known as the Unrepeatable read problem.
Thus, in order to maintain consistency in the database and avoid such problems that take
place in concurrent execution, management is needed, and that is where the concept of
Concurrency Control comes into role.
To address these challenges, the DBMS employs concurrency control techniques. Think of it like
traffic rules. Just as traffic rules ensure vehicles don’t collide, concurrency control ensures
transactions don’t conflict.
4|Page
2. Avoid Conflicting Updates: When two transactions attempt to update the same data
simultaneously, one update might overwrite the other without proper control. Concurrency
control ensures that updates don’t conflict and cause unintended data loss.
3. Prevent Dirty Reads: Without concurrency control, one transaction might read data that
another transaction is in the middle of updating (but hasn’t finalized). This can lead to
inaccurate or “dirty” reads, where the data doesn’t reflect the final, committed state.
4. Enhance System Efficiency: By managing concurrent access to the database, concurrency
control allows multiple transactions to be processed in parallel. This improves system
throughput and makes optimal use of resources.
5. Protect Transaction Atomicity: For a series of operations within a transaction, it’s crucial
that all operations succeed (commit) or none do (abort). Concurrency control ensures that
transactions are atomic and treated as a single indivisible unit, even when executed
concurrently with others.
Two-phase locking (2PL) is a protocol used in database management systems to control concurrency
and ensure transactions are executed in a way that preserves the consistency of a database. It’s called
“two-phase” because, during each transaction, there are two distinct phases: the Growing phase and
the Shrinking phase.
Two phase locking is a process used to gain ownership of shared resources without creating
the possibility of deadlock. The 3 activities taking place in the two phase update algorithm
are:
Lock Acquisition
Modification of Data
Release Lock(the beginning of the shrink phase)
In Two phase locking no process is ever in a state where it is holding some shared resources,
and waiting for another process to release a shared resource which it requires. This means
that deadlock cannot occur due to resource contention.
5|Page
Breakdown of the Two-Phase Locking protocol
I. Phases:
Growing Phase: During this phase, a transaction can obtain (acquire) any number of locks as
required but cannot release any. This phase continues until the transaction acquires all the
locks it needs and no longer requests.
Shrinking Phase: Once the transaction releases its first lock, the Shrinking phase starts.
During this phase, the transaction can release but not acquire any more locks.
II. Lock Point: The exact moment when the transaction switches from the Growing phase to the
Shrinking phase (i.e. when it releases its first lock) is termed the lock point.
The primary purpose of the Two-Phase Locking protocol is to ensure conflict-Serializability, as the
protocol ensures a transaction does not interfere with others in ways that produce inconsistent results.
The Timestamp Ordering Protocol is a concurrency control method used in database management
systems to maintain the Serializability of transactions. This method uses a timestamp for each
transaction to determine its order in relation to other transactions. Instead of using locks, it ensures
transaction order based on their timestamps.
Breakdown of the Time stamp ordering protocol
1. Read Timestamp (RTS):
This is the latest or most recent timestamp of a transaction that has read the data item.
Every time a data item X is read by a transaction T with timestamp TS, the RTS of X
is updated to TS if TS is more recent than the current RTS of X.
2. Write Timestamp (WTS):
This is the latest or most recent timestamp of a transaction that has written or updated
the data item.
Whenever a data item X is written by a transaction T with timestamp TS, the WTS of
X is updated to TS if TS is more recent than the current WTS of X.
6|Page
The timestamp ordering protocol uses these timestamps to determine whether a transaction’s request
to read or write a data item should be granted. The protocol ensures a consistent ordering of
operations based on their timestamps, preventing the formation of cycles and, therefore, deadlocks.
7|Page
V. Conflict Resolution: If two transactions try to modify the same data item concurrently, the
system will need a way to resolve this. Different systems have different methods for conflict
resolution. A common one is that the first transaction to commit will succeed, and the other
transaction will be rolled back or will need to resolve the conflict before proceeding.
8|Page
The area consists of children nodes which are known as files. No file can be present in
more than one area.
Finally, each file contains child nodes known as records. The file has exactly those
records that are its child nodes. No records represent in more than one file.
Hence, the levels of the tree starting from the top level are as follows:
Database ->Area ->File ->Record
In this example, the highest level shows the entire database. The levels below are file, record,
and fields. Multiple granularity locking uses two types of locks:-
I. Shared Lock: - It allows multiple transactions to read the same data simultaneously. It is
used to prevent other transactions from modifying the data while a transaction is reading
it.
II. Exclusive Lock: - It prevents any other transaction from accessing the data. It is used to
prevent other transactions from reading or modifying the data while a transaction is
writing to it.
Intention mode locks are a type of lock used in multiple granularity locking that allows
multiple transactions to acquire locks on the same resource, but with different levels of
access.
There are three types of intention mode locks in multiple granularity locking
Intent Shared (IS) Locks:- This lock is used when a transaction needs to read a
resource but does not intend to modify it. It indicates that the transaction wants to
acquire a Shared lock on a resource.
9|Page
Intent Exclusive (IX) Locks:- This lock is used when a transaction needs to modify a
resource but does not intend to share it. It indicates that the transaction wants to
acquire an Exclusive lock on a resource.
Shared with Intent Exclusive (SIX) Locks:- This lock is used when a transaction
intends to acquire both Shared and Exclusive locks on a resource. It indicates that the
transaction wants to acquire an Exclusive lock on a resource after acquiring Shared
locks on other resources.
These intention mode locks are used to optimize the locking mechanism in a database by
allowing transactions to acquire locks on multiple resources in a coordinated manner. They
help prevent deadlocks and improve concurrency in a database system.
Compatibility Matrix with Intention Lock Modes: The below table describes the compatibility
matrix for these lock modes:
It uses the intention lock modes to ensure Serializability. It requires that if a transaction
attempts to lock a node, then that node must follow these protocols:
Transaction T1 should follow the lock-compatibility matrix.
Transaction T1 firstly locks the root of the tree. It can lock it in any mode.
If T1 currently has the parent of the node locked in either IX or IS mode, then the
transaction T1 will lock a node in S or IS mode only.
If T1 currently has the parent of the node locked in either IX or SIX modes, then the
transaction T1 will lock a node in X, SIX, or IX mode only.
If T1 has not previously unlocked any node only, then the Transaction T1 can lock a
node.
If T1 currently has none of the children of the node-locked only, then Transaction T1
will unlock a node.
Observe that in multiple-granularity, the locks are acquired in top-down order, and locks
must be released in bottom-up order.
If transaction T1 reads record R a9 in file Fa, then transaction T1 needs to lock the
database, area A1 and file Fa in IX mode. Finally, it needs to lock Ra2 in S mode.
If transaction T2 modifies record Ra9 in file Fa, then it can do so after locking the
database, area A1 and file Fa in IX mode. Finally, it needs to lock the Ra9 in X mode.
10 | P a g e
If transaction T3 reads all the records in file F a, then transaction T3 needs to lock the
database, and area A in IS mode. At last, it needs to lock Fa in S mode.
If transaction T4 reads the entire database, then T4 needs to lock the database in S
mode.
11 | P a g e