Chapter 4 Concurency Control
Chapter 4 Concurency Control
Concurrency control is a critical aspect of database management systems (DBMS) that ensures
the integrity of data when multiple transactions are executed simultaneously. When multiple
transactions run concurrently, there is a risk of inconsistencies and anomalies, such as lost
updates, temporary inconsistency, and uncommitted data being read by other transactions. Here
are the primary techniques used for concurrency control:
1. Locking Mechanisms
Locking is one of the most widely used concurrency control techniques. When a transaction
wants to perform an operation on a data item, it must acquire a lock on that item first.
Exclusive Locks (X Locks): Prevent other transactions from reading or writing the
locked data item.
Shared Locks (S Locks): Allow multiple transactions to read the data item but prevent
any of them from writing to it.
Lock Modes: Different DBMSs may implement various lock types, like intent locks and
range locks.
Growing Phase: A transaction can acquire locks but cannot release them.
Shrinking Phase: A transaction can release locks but cannot acquire new ones.
Strict 2PL: Transactions cannot release any locks until they have completed.
Rigorous 2PL: Locks are held until the commit point, preventing any dirty reads.
Cascading 2PL: Locks can be released before a transaction commits, allowing more
concurrency but introducing potential rollbacks due to cascading failures.
2. Timestamp Ordering
Each transaction is assigned a unique timestamp. The execution of transactions is then ordered
based on their timestamps, ensuring that earlier transactions have precedence over later ones.
This approach allows transactions to execute without restrictions, assuming that conflicts
between transactions will be rare. There are three phases:
Read Phase: Transaction reads the required data and performs updates in a local copy.
Validation Phase: Before committing, the system checks whether any other transactions
have modified the data during the read phase.
Write Phase: If validation is successful, updates are applied; otherwise, the transaction is
rolled back.
In this technique, multiple versions of a data item are maintained, allowing transactions to read
different versions without blocking each other.
Read Operations: Transactions read the most recent committed version of the data.
Write Operations: When a transaction writes, a new version of the data is created
without altering the existing version until the transaction commits.
This technique extends MVCC to ensure that transactions operate in a fashion that resembles
serial execution. It allows for better concurrency than strict serializability while ensuring that the
outcomes are consistent.
6. Quorum-Based Techniques
These techniques require a specified number of votes (or quorums) from participating nodes in a
distributed database before a transaction can proceed, ensuring that sufficient agreement is
reached among distributed copies of data.
In binary locking, each data item can be in one of two states: locked or unlocked. A transaction
must lock an item before it can access it. Once it’s done, it releases the lock.
Example:
Shared Lock (S-Lock): Allows a transaction to read a data item. Multiple transactions
can hold shared locks on the same data item simultaneously.
Exclusive Lock (X-Lock): Allows a transaction to write to a data item. Only one
transaction can hold an exclusive lock on a data item.
Example:
Two-Phase Locking is a protocol that ensures serializability. In this protocol, a transaction enters
the growing phase where it can acquire any number of locks and then enters the shrinking phase
where it can only release locks.
Example:
Strict 2PL is a stronger form of 2PL where transactions must hold all their locks until they
commit or abort. This eliminates the potential for cascading rollbacks.
Example:
Transaction T1 locks A (X-lock(A)), reads and updates it, and only releases the lock after
it has committed. No other transaction can read or write to A while T1 holds the lock.
In locking systems, deadlocks can occur when transactions hold locks and wait for other locks to
be released. Some techniques include:
Wait-Die: Older transactions wait for younger transactions, while younger transactions
are aborted if they request a lock held by an older transaction.
Wound-Wait: Younger transactions are aborted if they request a lock held by an older
transaction.
Example:
T1 (older) holds lock on A and is waiting for lock on B held by T2 (younger). According
to the Wait-Die scheme, T1 will wait, but T2 will be aborted.
6. Timestamp Ordering
This mechanism gives each transaction a unique timestamp. Transactions are executed in the
order of their timestamps, ensuring serializability without the need for locks.
Example:
Multiversion Concurrency Control (MVCC) is a concurrency control method that allows for
multiple versions of data items to be stored, enabling multiple transactions to proceed without
locking resources. This technique is commonly used in databases to increase performance and
reduce contention. One of the primary methods of implementing MVCC is through Optimistic
Concurrency Control (OCC), which assumes that conflicts between transactions are rare.
Example Scenario
Let's consider a simple banking application where two users are trying to withdraw money from
the same account concurrently.
Transactions:
Execution Steps:
1. Read Phase:
o T1 reads the account balance: $100.
o T2 also reads the account balance: $100.
2. Validation Phase (Before committing):
o T1 checks if the balance is sufficient: $100 (Balance) - $70 (Withdrawal) >= 0 →
Valid. It proceeds to the Write Phase.
o T2 checks the balance too: $100 - $50 >= 0 → Valid. It also proceeds to the Write
Phase.
3. Write Phase:
o T1 writes the new balance: $100 - $70 = $30.
o T2 then attempts to write its new balance. However, at this point, T2's validation
fails because it had read a balance of $100, which is no longer valid. Now it must
roll back or retry.
transaction T1 {
read balance from account
// Assume balance = 100
requested_withdraw = 70
// Validation phase
if balance - requested_withdraw >= 0 {
// Write Phase
balance = balance - requested_withdraw
update account with new balance
commit
} else {
roll back
}
}
transaction T2 {
read balance from account
// Assume balance = 100
requested_withdraw = 50
// Validation phase
if balance - requested_withdraw >= 0 {
// Write Phase
balance = balance - requested_withdraw
update account with new balance
commit
} else {
roll back
}
}
Advantages of OCC
Performance: Since transactions do not wait for locks, the performance can be
improved, especially in read-heavy scenarios.
Scalability: Systems can handle many transactions concurrently without blocking.
Disadvantages of OCC
Rollback Overhead: If conflicts do occur, transactions may roll back and retrial can
happen, which might lead to performance degradation.
Conflict Rate: OCC is more effective in environments with low contention.
Granularity of Data Items and Multiple Granularity Locking Using Locks for
Concurrency Control in Indexes
oncurrency control is a critical aspect of database management systems (DBMS) that ensures
that multiple transactions can occur simultaneously without leading to inconsistencies in the
database. Granularity of data items and multiple granularity locking are important concepts in
this context, particularly when dealing with indexes.
Granularity refers to the size of the data item that is being locked. It can range from very fine
(individual rows) to very coarse (entire tables). The granularity chosen impacts the system's
throughput, deadlock potential, and response time.
1. Coarse Granularity: Locking larger units of data (like tables) can reduce the overhead
of obtaining locks but may lead to increased contention. For example, if two transactions
want to update different rows in the same table, they may have to wait for each other if a
table-level lock is used.
Example:
Example:
Multiple granularity locking is a strategy that allows systems to lock data items at different levels
of granularity (like tuples, pages, and tables) using a hierarchical locking scheme. This method
combines the advantages of both coarse and fine granularity.
Lock Modes
Locking Protocol
1. Locking Hierarchy:
o At the database level (coarsest level), a lock can be placed on an entire table.
o At the page level, locks can be placed on individual pages.
o At the row level (finest level), locks can be placed on specific rows.
2. Locking Protocol:
o A transaction must acquire an intention lock on a higher level before locking
lower levels.
o For example, to lock Row 3 of a Page, a transaction must first acquire an IX lock
on the Page and then an X lock on Row 3.
Table: Products
Fields: ProductID, Name, Price
Locking Process:
1. T1 acquires an IX lock on the "Products" table because it plans to perform an update (an
exclusive action).
2. T1 then requests an X lock on Row 100 to update the price.
3. T2 can acquire an IS lock on the "Products" table to read data.
4. T2 can also acquire an X lock on Row 300 since it will directly access that row, as long
as it does not interfere with T1’s exclusive lock on Row 100.
This concurrent execution is possible as T1 and T2 are working on separate rows and have
obtained the appropriate locks according to the locking hierarchy.
ncurrency control is crucial in databases and applications that involve multiple users and
transactions accessing shared resources. Locks are a common mechanism used for this purpose,
particularly with index structures. Locks ensure that transactions are executed in a manner that
preserves data integrity and consistency.
Types of Locks
1. Exclusive Locks (X Lock): A transaction that holds an exclusive lock on a resource prevents other
transactions from accessing that resource in any manner.
2. Shared Locks (S Lock): A shared lock allows multiple transactions to read a resource
simultaneously but prevents any transaction from modifying it.
Locking in Indexes
Index structures—such as B-trees or hash indexes—are often locked to control access during
operations like insertions, deletions, and updates. Depending on the operation, different locking
strategies can be employed.
Consider a B-tree index on a table for a banking application containing account information such
as account numbers, names, and balances.
Scenario
Two transactions, Transaction A and Transaction B, are trying to perform operations on the
same B-tree index.
Process
Transaction B acquires a Shared Lock (S Lock) on the B-tree index for reading the account
information.
Since it's reading and not modifying, other transactions can also acquire shared locks on the
same index.
Step 2: Transaction A Writes
Conclusion
Locking in index structures is a sophisticated but necessary process to ensure the consistency and
integrity of data during concurrent operations. By implementing strategies such as shared and
exclusive locks in a two-phase locking manner, databases can allow multiple concurrent
transactions while preventing conflicts that could lead to data corruption.
Best Practices:
Minimize Lock Duration: Keep transactions short to reduce the time locks are held.
Use Granularity: Use finer-grained locking (like row-level locks) over block-level locks for better
concurrency.
Deadlock Prevention: Implement deadlock detection and resolution strategies to ensure that
two transactions don’t wait on each other indefinitely.