Concurrency Control Mechanisms
Concurrency Control Mechanisms
Rajshahi University of Engineering & Technology
Course Title: Data Base Systems
Course No.: ECE 2215
Md. Rakibul Islam
Lecturer, Dept. of ETE, RUET
Heaven’s Light is Our Guide
Outline
- Types of Schedules Based on Recoverability
- Concurrency control
- Simple locking protocol
- Two phase (2-PL) locking protocol
- Strict & Rigorous two-phase locking
- Deadlock
- Timestamp-Based Protocols
Types of Schedules Based on Recoverability
Schedules Hierarchy
- Recoverable
- Cascadeless
- Strict
Figure 8.1 — Types of Schedules Based on Recoverability (Source: Slide 3)
Recoverable Schedule
A recoverable schedule ensures that the database can return to a consistent state after a transaction failure.
In this type of schedule:
- A transaction cannot use (read) data updated by another transaction until that transaction commits.
- If a transaction fails before committing, all its changes must be rolled back, and any other transactions that have used its uncommitted data must also be rolled back.
| T1 | T2 |
|---|
| R(A)→5 | |
| W(A)→6 | |
| R(A)→6 |
| Commit; |
| Failure | |
Figure 8.2 — Irrecoverable Schedule (Source: Slide 4)
Cascadeless Schedules
A cascade-less schedule ensures that if one transaction fails, it does not cause multiple other transactions to roll back.
| T1 | T2 | T3 |
|---|
| R(x) | | |
| W(x) | ←Safe Read | |
| C | →R(x) | |
| W(x) | |
| C | →R(x) |
| | W(x) |
| | C |
Figure 8.3 — Cascadeless Schedule (Source: Slide 5)
Cascading Schedules Vs Cascadeless Schedule
A cascadeless schedule is a type of transaction schedule that ensures reliability by preventing cascading rollbacks. Cascading rollbacks occur when the failure of one transaction forces the rollback of other dependent transactions, leading to potential data loss and system inefficiencies.
| DB | T1 | T2 | T3 | T4 |
|---|
| A=100 | R(A) | | | |
| A=A−50 | | | |
| W(A) A=50 | | | |
| | R(A) A=50 | | |
| | | R(A) A=50 | |
| | | | R(A) A=50 |
| Rollback | Fail | | | |
| Commit | | | |
Hence, this is Dirty Read Problem
Figure 8.4 — Cascading Rollback and Dirty Read Problem (Source: Slide 6)
Cascading Schedules Vs Cascadeless Schedule
Problems Due to Cascading in DBMS
- Cascading Rollbacks
- Data Inconsistency
- Loss of Progress
A cascadeless schedule is a type of transaction schedule in a database where no transaction is allowed to read data that has been modified by another transaction until that transaction has either committed or aborted.
How a Cascadeless Schedule Works?
When a transaction modifies a data item, other transactions are restricted from accessing (reading) that data until the first transaction has finalized its changes.
This means the data is only accessible after the transaction has either:
- Committed: Confirmed and saved its changes permanently.
- Aborted: Rolled back its changes, returning the data to its previous state.
Strict Schedule
A schedule is strict if it is both recoverable and cascadeless.
A strict schedule ensures strong data consistency by following two key rules:
- A transaction cannot use (read) uncommitted data from another transaction until that transaction commits.
- A transaction cannot update (write) the same data modified by another transaction until the first one commits.
Figure 8.5 — Classification of Schedules (Source: Slide 8)
Concurrency control
Concurrency control protocols are the set of rules which are maintained in order to solve the concurrency control problems in the database.
It ensures that the concurrent transactions can execute properly while maintaining the database consistency.
Some of the currency control protocols are as follows:
- Lock- based protocol
- Graph based protocol
Simple locking protocol (Shared-Exclusive Lock)
In this protocol each transaction cannot read or write the data until it acquires an appropriate lock on it.
Data items can be locked in two modes :
- Exclusive (X) mode. Data item can be both read as well as written. X-lock is requested using lock-X instruction.
- Shared (S) mode. Data item can only be read. S-lock is requested using lock-S instruction.
Based on these locking modes we can define Lock-compatibility matrix:
- A transaction may be granted a lock on an item if the requested lock is compatible with locks already held on the item by other transactions
- Any number of transactions can hold shared locks on an item,
- But if any transaction holds an exclusive on the item no other transaction may hold any lock on the item.
Lock-Compatibility Matrix
Problems with Simple locking protocol
- The serializability cannot be always assured.
- It may produce irrecoverable schedules.
- May not free from starvation.
- Deadlock can be occurs.
- Neither T3 nor T4 can make progress — executing lock-S(B) causes T4 to wait for T3 to release its lock on B, while executing lock-X(A) causes T3 to wait for T4 to release its lock on A.
- Such a situation is called a deadlock.
- To handle a deadlock one of T3 or T4 must be rolled back and its locks released.
| T3 | T4 |
|---|
| lock-X(B) | |
| read(B) | |
| B:=B−50 | |
| write(B) | |
| lock-S(A) |
| read(A) |
| lock-S(B) |
| lock-X(A) | |
Figure 8.6 — Deadlock Occurs in Simple Locking Protocol (Source: Slide 11)
| T1 | T2 |
|---|
| lock-X(B) | |
| read(B) | |
| B:=B−50 | |
| write(B) | |
| unlock(B) | |
| lock-S(A) |
| read(A) |
| unlock(A) |
| lock-S(B) |
| read(B) |
| unlock(B) |
| display(A+B) |
| lock-X(A) | |
| read(A) | |
| A:=A+50 | |
| write(A) | |
| unlock(A) | |
Figure 8.7 — Serializable Schedule Cannot Be Assured (Source: Slide 11)
Problems with Simple locking protocol
Starvation is also possible if concurrency control manager is badly designed.
For example Suppose a transaction T2 has a shared-mode lock on a data item, and another transaction T1 requests an exclusive-mode lock on the data item. Clearly, T1 has to wait for T2 to release the shared-mode lock.
Meanwhile, a transaction T3 may request a shared-mode lock on the same data item. The lock request is compatible with the lock granted to T2, so T3 may be granted the shared mode lock. At this point T2 may release the lock, but still T1 has to wait for T3 to finish.
But again, there may be a new transaction T4 that requests a shared-mode lock on the same data item, and is granted the lock before T3 releases it.
In fact, it is possible that there is a sequence of transactions that each requests a shared-mode lock on the data item, and each transaction releases the lock a short while after it is granted, but T1 never gets the exclusive-mode lock on the data item. The transaction T1 may never make progress, and is said to be starved.
| T1 | T2 | T3 | T4 |
|---|
| S(A) | | |
| X(A) | | | |
| | S(A) | |
| U(A) | | |
| | | S(A) |
| | U(A) | |
| | | U(A) |
| G(X(A)) | | | |
Figure 8.8 — Starvation in Locking Protocol (Source: Slide 12)
Two phase (2-PL) locking protocol
Simple Lock-based protocol does not guarantee Serializability. Schedules may follow the preceding rules but a non-serializable schedule may result. This is where the concept of Two-Phase Locking(2-PL) comes into the picture, 2-PL ensures serializability.
- Phase 1: Growing Phase
– Transaction may obtain locks
– Transaction may not release locks
- Phase 2: Shrinking Phase
– Transaction may release locks
– Transaction may not obtain locks
The protocol assures serializability. It can be proved that the transactions can be serialized in the order of their lock points (i.e., the point where a transaction acquired its final lock).
Figure 8.9 — Two-Phase Locking Protocol (Source: Slide 13)
Two phase (2-PL) locking protocol
Two-phase locking protocol with lock conversions:
– Growing Phase:
• can acquire a lock-S on item
• can acquire a lock-X on item
• can convert a lock-S to a lock-X (upgrade)
– Shrinking Phase:
• can release a lock-S
• can release a lock-X
• can convert a lock-X to a lock-S (downgrade)
Figure 8.10 — Two-Phase Locking Protocol with Lock Conversions (Source: Slide 14)
Two phase (2-PL) locking protocol
Transaction T1
- Growing Phase: From Time 2 to 3
- Shrinking Phase: From Time 5 to 7
- Lock Point: At time 3
Transaction T2
- Growing Phase: From Time 1 to 6
- Shrinking Phase: From Time 8 to 9
- Lock Point: At Time 6
| Time | T1 | T2 |
|---|
| 1 | | Lock-S(A) |
| 2 | Lock-S(A) | |
| 3 | Lock-X(B) | |
| 4 | | |
| 5 | Unlock(A) | |
| 6 | | Lock-X(C) |
| 7 | Unlock(B) | |
| 8 | | Unlock(A) |
| 9 | | Unlock(C) |
As we say, 2PL will generate the serial schedule then what will be the sequence of transaction execution?
Answer: If locking point of Transaction T1 comes earlier than transaction T2 then the T1 will execute first than T2.
Problem with 2-PL locking protocol
Although,2-Phase Locking ensures serializability, but there are still some drawbacks of 2-PL.They are as follows:
- The schedule that is produced through 2PL may be irrecoverable
| T1 | T2 |
|---|
| Lock-X(A) | |
| R(A) | |
| Write(A) | |
| Unlock(A) | |
| Lock-S(A) |
| Read(A) |
| Unlock(A) |
| Commit |
| Failure | |
The above schedule is a irrecoverable because of the reasons mentioned below:
- The transaction T2 is also committed before the completion of transaction T1. The transaction T1 fails later and there are rollbacks.
- The transaction T2 reads an incorrect value.
- The transaction T2 which is performing a dirty read operation on A.
Finally, the transaction T2 cannot recover because it is already committed.
Problem with 2-PL locking protocol
- The schedule that is produced through 2PL locking may still contains a deadlock problem
| T1 | T2 |
|---|
| Lock-X(A) | |
| Lock-X(B) |
| Lock-X(B) | |
| Lock-X(A) |
Here,
- In T1 Exclusive lock on data A is granted. In T2 Exclusive lock on data B is granted
- In T1 Exclusive Lock on data B waits until T2 unlock Exclusive Lock on B
- Exclusive Lock in Data A is wait until T1 unlock the Exclusive lock on A
Problem with 2-PL locking protocol
- Cascading rollback problem
| Time | T1 | T2 | T3 |
|---|
| 1 | Lock-X(A) | | |
| 2 | Read(A) | | |
| 3 | Write(A) | | |
| 4 | Unlock(A) | | |
| 5 | | Lock-S(A) | |
| 6 | | Read(A) | |
| 7 | | | Lock-S(A) |
| 8 | | | Read(A) |
| 9 | Failure | | |
Here,
- T2 is dependent on T1, and T3 is dependent on T2
- If T1 failed, T1 must be rolled back ,similarly as T2 is dependent on T1,T2 also must be rolled back.
- Again as T3 is dependent on T2,T3 must also be rolled back.
Strict & Rigorous two-phase locking
Strict two-phase locking
- A transaction must hold all its exclusive locks till it commits/aborts. Ensures recoverability and avoids cascading rollbacks.
- The only difference between 2PL and strict 2PL is that Strict-2PL does not release a lock after using it.
- Strict-2PL protocol does not have shrinking phase of lock release.
- Strict-2PL waits until the whole transaction to commit, and then it releases all the locks at a time.
Rigorous two-phase locking:
- A transaction must hold all locks till commit/abort.
- Transactions can be serialized in the order in which they commit.
Note: The difference between Strict 2-PL and Rigorous 2-PL is that Rigorous is more restrictive, it requires both Exclusive and Shared locks to be held until after the Transaction commits and this is what makes the implementation of Rigorous 2-PL easier.
Figure 8.11 — Lock Release at Commit in Strict/Rigorous 2PL (Source: Slide 19)
Deadlock
System is deadlocked if there is a set of transactions such that every transaction in the set is waiting for another transaction in the set.
- Transaction T3 request resource B and received it. Transaction T4 request resource A and received it.
- Transaction T4 request resource B and is queued up waiting the release of A by transaction T3.
- Transaction T3 request resource A and is queued up waiting the release of A by transaction T4.
| T3 | T4 |
|---|
| lock-X(B) | |
| read(B) | |
| B:=B−50 | |
| write(B) | |
| lock-S(A) |
| read(A) |
| lock-S(B) |
| lock-X(A) | |
Deadlock
Conditions for deadlock:
We can say a deadlock is occurred if these four conditions occur simultaneously.
- Mutual Exclusion: Only one transaction at a time can use a resource.
- Hold and wait: A transaction holding at least one resource is waiting to acquire additional resources held by other transactions.
- No preemption: A resource can be released only voluntarily by the transaction holding it after the transaction completed its task.
- Circular wait: There exist a set {T1,T2,…,TN} of waiting process such that T1 is waiting for a resource that is held by T2, T2 is waiting for a resource held by T3 and finally TN is waiting for resource held by T1.
There are two principal methods for dealing with the deadlock problem. We can use a deadlock prevention protocol to ensure that the system will never enter a deadlock state. Alternatively, we can allow the system to enter a deadlock state, and then try to recover by using a deadlock detection and deadlock recovery scheme. As we shall see, both methods may result in transaction rollback
Deadlock
Deadlock Prevention
Deadlock Prevention Protocols ensure that the system will never enter into a deadlock state. Two approach to deadlock prevention
- First approach ensures that no cyclic waits can occur by ordering the requests for locks, requiring all locks to be acquired together.
- Second approach performs transaction rolled back instead of waiting for a lock, whenever the wait could be potentially result in a deadlock.
The first approach requires that each transaction locks all its data items before execution. Moreover either all are locked in one step or none are locked.
There are two main disadvantages.
- It is often hard to predict, before the transaction begins, what data items need to be locked.
- Data items utilization may be very low, since many of the data items may be locked but unused for it a long time.
The second approach for preventing deadlock is use preemption and transaction rollback. Two different deadlock prevention schemes using timestamps have been proposed.
Deadlock
Deadlock Prevention
The second approach for preventing deadlock is use preemption and transaction rollback. Two different deadlock prevention schemes using timestamps have been proposed.
- Wait -die scheme (non-preemptive): When transaction Ti requests a data item currently head by Tj. Ti is allowed to wait only if it has a timestamp smaller than that of Tj (i.e Ti older than Tj). Otherwise Ti rolled back(dies). For example, suppose that transactions T1, T2 and T3 have timestamp 20, 30 and 40 respectively. If T1 request data item held by T2, then T1 will be wait. If T3 requests a data item held by T2, then T3 will be rolled back.
- Wound -wait scheme(preemptive): When transaction Ti requests a data item currently held by Tj, Ti is allowed to wait only if it has a timestamp larger than that of Tj (i.e Ti is younger than Tj) . Otherwise Tj rolled back (Tj is wounded by Ti). For example, suppose that transactions T1, T2 and T3 have timestamp 20, 30 and 40 respectively. If T1 request a data item held by T2, then data item will be preempted from T2 and T2 will be rolled back. If T3 requests a data item held by T2, then T3 will wait.
Both in wait-die and in wound-wait schemes, a rolled back transaction is restarted with its original timestamp. Older transaction thus have precedence over new ones, and starvation is hence avoided.
Deadlock
Deadlock Prevention
Timeout-Based schemes: A transaction waits for a lock only for a specified amount of time. After that, the wait times out and the transaction is rolled back. Thus deadlock is not possible. Simple to implement; but starvation is possible. Also difficult to determine good value of the timeout interval.
Deadlock Detection:
- Wait-for graph
- Vertices: transactions
- Edge from Ti→Tj : if Ti is waiting for a lock held in conflicting mode by Tj
- The system is in a deadlock state if and only if the wait-for graph has a cycle.
- Invoke a deadlock-detection algorithm periodically to look for cycles.
Figure 8.12 — Wait-for Graph Without a Cycle (Source: Slide 24)
Figure 8.13 — Wait-for Graph With a Cycle (Deadlock) (Source: Slide 24)
Deadlock
- When deadlock is detected :
- Some transaction will have to rolled back (made a victim) to break deadlock cycle.
- Select that transaction as victim that will incur minimum cost
- Rollback -- determine how far to roll back transaction
- Total rollback: Abort the transaction and then restart it.
- Partial rollback: Roll back victim transaction only as far as necessary to release locks that another transaction in cycle is waiting for
- Starvation can happen (why?)
- One solution: In a system where the selections of victims is based primarily on cost factors, It may happen that the same transaction is always picked as a victim. As a result this transaction never completes its designated task, thus there is a starvation. We must ensure that a transaction can be picked as a victim only a small (finite number of times). The most common solution is to include the number of rollbacks in cost factor.
Timestamp-Based Protocols
- Each transaction Ti is issued a timestamp TS(Ti) when it enters the system.
- Each transaction has a unique timestamp
- Newer transactions have timestamps strictly greater than earlier ones
- Timestamp could be based on a logical counter
- Real time may not be unique
- Can use (wall-clock time, logical counter) to ensure
- Timestamp-based protocols manage concurrent execution such that:
time-stamp order=serializability order
- Several alternative protocols based on timestamps
Timestamp ordering (TSO) protocol
The timestamp ordering (TSO) protocol
- Maintains for each data Q two timestamp values:
- W-timestamp(Q) is the largest time-stamp of any transaction that executed write(Q) successfully.
- R-timestamp(Q) is the largest time-stamp of any transaction that executed read(Q) successfully.
- Imposes rules on read and write operations to ensure that
- Any conflicting operations are executed in timestamp order
- Out of order operations cause transaction rollback
Timestamp ordering (TSO) protocol
- Suppose a transaction Ti issues a read(Q)
- If TS(Ti)<W-timestamp(Q), then Ti needs to read a value of Q that was already overwritten.
- Hence, the read operation is rejected, and Ti is rolled back.
- If TS(Ti)≥W-timestamp(Q), then the read operation is executed, and R-timestamp(Q) is set to max(R-timestamp(Q),TS(Ti)).
Note: We use this concept that, always older transaction completed first. So if a younger transaction do W(Q) or R(Q) after older transaction W(Q) or R(Q) then its no problem. But if a older transaction do W(Q) or R(Q) after younger transaction W(Q) or R(Q) then we roll back the older transaction.
Timestamp ordering (TSO) protocol
- Suppose that transaction Ti issues write(Q).
- If TS(Ti)<R-timestamp(Q), then the value of Q that Ti is producing was needed previously, and the system assumed that that value would never be produced.
- Hence, the write operation is rejected, and Ti is rolled back.
- If TS(Ti)<W-timestamp(Q), then Ti is attempting to write an obsolete value of Q.
- Hence, this write operation is rejected, and Ti is rolled back.
- Otherwise, the write operation is executed, and W-timestamp(Q) is set to TS(Ti).
Timestamp ordering (TSO) protocol
| T25 | T26 |
|---|
| read(B) | |
| read(B) |
| B:=B−50 |
| write(B) |
| read(A) | |
| read(A) |
| display(A+B) | |
| A:=A+50 |
| write(A) |
| display(A+B) |
Figure 8.14 — Schedule of T25 and T26 Under Timestamp Ordering Protocol (Source: Slide 30)
- How about this one, where initially R-TS(Q)=W-TS(Q)=0
| T27 | T28 |
|---|
| read(Q) | |
| write(Q) |
| write(Q) | |
Figure 8.15 — Schedule of T27 and T28 Under Timestamp Ordering Protocol (Source: Slide 30)
Thank you