Lecture 3 - Concurrency Control and Fault Tolerance
Lecture 3 - Concurrency Control and Fault Tolerance
Lecture 3
Concurrency Control and Fault Tolerance
Agenda
• Transactions
• Transaction Properties
• The Transaction Model
• Types of Transactions
• Schedule
• Concurrency Problems
• Serializability
• Lock based Concurrency Control
• Two Phase Locking Protocol
• Graph based Concurrency Control
• Timestamp based Concurrency Control
• Recovery with Concurrent Transactions
Transactions
• Sequence of separate requests to a server to be atomic in the
sense that
• Example
Transaction T:
a.Withdraw(100)
b.deposit(100)
Transaction Properties
The Transaction Model
• Examples of primitives for transactions.
Primitive Description
9
Transaction in Distributed System
• A transaction is an execution of a sequence of client
requests transforming the server data from one
consistent state to another.
1
Concurrency
Problems
2
Concurrency
Problems
3
Concurrency
Problems
4
Concurrency
Problems
5
Serializability
Schedule Equivalence
• Two schedules are guaranteed to have same results if they will read the same
values and write the values in the same order.
Schedule 1 x = 0; x = x + 1; x = 0; x = x + 2; x = 0; x = x + 3 Legal
Schedule 2 x = 0; x = 0; x = x + 1; x = x + 2; x = 0; x = x + 3; Legal
Schedule 3 x = 0; x = 0; x = x + 1; x = 0; x = x + 2; x = x + 3; Illegal
(d)
• Lock exists to prevent two or more users from performing any change on the
same data item at the very same time. Therefore, it is correct to interpret this
technique as a means of synchronizing access.
• In layman’s terms, this may be further simplified to the metaphorical ‘lock’
that is put on a data item so that no other user may unlock the ability to
perform any update.
Lock based Concurrency Control
• Locks which permit other transactions to make read queries since a READ
query is non-conflicting
• However, if a transaction demands a write query on item X, then that
transaction must be given exclusive access to item X.
Lock based Concurrency Control
Shared or Exclusive Locks
• Multi-mode lock Read/Write Locks
• SHARED-LOCKED –
Data can only be read when a shared lock
is applied. Data cannot be written.
• EXCLUSIVE-LOCKED –
Data can be read as well as written when
an exclusive lock is applied.
• UNLOCKED –
Once a transaction has completed its
read or update operations, no lock is held
and the data item is unlocked. In this
state, the item may be accessed by any
queued transactions.
Lock based Concurrency Control
Shared or Exclusive Locks
• The most popular way of implementing these locks is by introducing a LOCK-
TABLE which keeps track of the number of read-locks on the data items and the
transactions with write-locks on different items.
• Step 3 T1 cannot execute the exclusive lock in R2 as the Exclusive lock is already
obtained by transaction T2 in step 1.
• Also, in step 3, the Exclusive lock on R1 cannot be executed by T2 as T1 has already
applied the Exclusive lock on R1.
• So, if we draw a wait-for graph, we can see that T1 waits for T2 and T2 waits for T1,
which creates conflict, and the waiting time never ends, which leads to a deadlock.
Categories of 2PL
Strict two-phase locking protocol
• The transaction can release the shared lock after the lock point.
• The transaction can not release any exclusive lock until the transaction commits.
• In strict two-phase locking protocol, if one transaction rollback then the other
transaction should also have to roll back. The transactions are dependent on each other.
This is called Cascading schedule.
• Concurrency Control
• Provide correct and highly available access to data in the presence of
concurrent access by large and diverse user populations
• Recovery
• Ensures database is fault tolerant, and not corrupted by software, system or
media failure
• 7x24 access to mission critical data
• Existence of CC&R allows applications to be written without explicit
concern for concurrency and fault tolerance
51
Recovery with Concurrent Transactions
Interaction with Concurrency Control
• The recovery scheme depends greatly on the concurrency control scheme
that is used. So, to rollback a failed transaction, we must undo the updates
performed by the transaction.
Transaction Rollback
• In this scheme, a failed transaction is rollbacked by using the log.
• The system scans the log backward a failed transaction, for every log record
found in the log the system restores the data item.
Recovery with Concurrent Transactions
Checkpoints
• Checkpoints is a process of saving a snapshot of the applications state so
that it can restart from that point in case of failure.
• Checkpoint is a point of time at which a record is written onto the database
form the buffers.
• Checkpoint shortens the recovery process.
• When it reaches the checkpoint, then the transaction will be updated into the
database, and till that point, the entire log file will be removed from the file.
Then the log file is updated with the new step of transaction till the next
checkpoint and so on.
• The checkpoint is used to declare the point before which the DBMS was in
the consistent state, and all the transactions were committed.
Recovery with Concurrent Transactions
Restart Recovery
• When the system recovers from a crash, it constructs two lists.
• The undo-list consists of transactions to be undone,
• The redo-list consists of transaction to be redone.
• Initially, they are both empty.
• The system scans the log backward, examining each record, until it finds the
first <checkpoint> record.