0% found this document useful (0 votes)
32 views36 pages

Concurrency Controls

Uploaded by

jibranhaider456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views36 pages

Concurrency Controls

Uploaded by

jibranhaider456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Distributed Database

Systems
Course Instructor: Engr. Samina Bilquees
Concurrency
• In a multi-user environment sometime different users simultaneously try to
access same piece of data this is called concurrency. If there is no concurrency
control by the DBMS important data may be lost.
• In order to explain the concept of concurrency lets take an example of ATM
system in a banking environment.
• Suppose two students Ali and Kashan have a joint Bank account and both have
ATM cards to use this bank account. On the particular day both Ali and Kashan
try to use their ATM cards at different ATM terminals but exactly at the same
time.
Concurrency Controls
• Lock-Based Protocol
• Two Phase Locking Protocols
• Time Stamp-Based Protocols
• Validation Based Protocols
Lock-Based Protocol

• To achieve consistency isolation is important idea, locking is the simplest idea


to achieve the isolation i.e. first obtain a lock on a data item then performed a
desired operation and then unlock it.
• It is widely used approach where locking techniques are used for
synchronization.
• A lock is a data variable which is associated with a data item.
• A lock guarantees exclusive use of a data item to a current transaction .
• Before any data item is accessed, the transactions need to lock the data item.
Lock-Based Protocol
• To provide better concurrency along with isolation we use different modes
of lock.
Locking modes

Read Mode or Shared Mode Write Mode or Exclusive Mode


Read Mode or Shared Mode
• Denoted by lock-S(Q)
• When transaction wants to read the data item and not update it.
• The data item can be read between transactions with the shared lock.
• It is also called a Read-only lock
• Any other transaction can also obtain same lock on same data item at the
same time (so called shared)
Write Mode or Exclusive Mode

• Denoted by lock-X(Q)
• The read lock gets upgraded to write lock after compatibility checking
(Both read and Write)
• A data can be read as well as written with exclusive lock.
• X-lock is requested using X-lock instructions
• Any other transactions can not be obtained either write/exclusive mode.
Lock Compatibility Matrix
• A transactions may be granted a lock on an item if the
requested lock is compatible with locks already held on the
S X
item by other transactions.
S  S X
• Any number of transactions can hold shared locks on an X X X
item, but if any transactions hold an exclusive(X) on the
item no other transaction may hold any lock on the item.
• If a lock can not be granted, the requesting transaction is
made to wait till all incompatible locks held by other
transactions have been released. Then lock is granted.
Two phase Locking Protocols
 Two-phase locking protocol which is also known as a 2PL.
 Two phase locking is a concurrency control locking protocol .
 In this type of locking protocol, the transaction should acquire a lock after it releases one of its locks.
 This locking protocol divides the execution phase of a transaction into three different parts.
• In the first phase, when the transaction begins to execute , it requires the locks it needs.
• The second part is where the transaction obtains all the locks when the transaction release its first
lock the third phase starts.
• In the third phase, the transaction can not demand any new locks, instead it only release the acquired
lock.
Phases
• Growing Phase
• Shrinking Phase
Growing Phase
• New locks on items can be acquired
• In this phase transaction may obtain locks but may not
release any locks is known as growing phase.
• It is also called expanding phase.
Shrinking Phase
• Existing locks, but no new lock can be acquired
• In this phase transaction may release locks but not obtain any new lock.
Example
 Transaction T1
• Growing Phase: from step 0-2
• Shrinking Phase: from step 4-6
• Lock point: at 3
 Transaction T2:
• Growing phase: from step 1-5
• Shrinking Phase: fro step 7-8
• Lock Point: at 6
2 PL in Distributed Database
• Centralized 2PL
• Primary Copy 2PL
• Distributed 2PL
Centralized 2PL/ Single Lock Manager
• System maintains a single lock manager that resides in a single chosen site, say S3
• Transaction Coordinator at site S1 divides the transactions
• Transaction Coordinator at site S1 acts as global transaction manager or
transaction coordinator.
• DM(data manager) component at site S1 sends the sub transaction to the
appropriate sites.
• When a transaction needs to lock a data item, it send a lock request to S3 and lock
manager determines whether the lock can be granted immediately.
• If yes then the lock manager sends a message to the site which initiated the
request.
• If no then request is delayed until it can be granted at which time a message is ent
to the initiating site.
Centralized 2PL/ Single Lock Manager
Contd…
• If the transaction involves an update of the data item that is replicated, the
coordinator must ensure that all copies of data item are updated.
• The coordinator requests write locks on all the copies before updating
each copy and releasing the locks
• The coordinator can elect to use any copy of the item for needs generally
the copy at its site, if one exists,
Advantages and Disadvantages
Advantages
• Simple implementation
• Simple deadlock handling
Disadvantages
• Bottleneck: lock manager site become a bottleneck
• Vulnerability: system is vulnerable to lock manager site failure
Primary Copy 2PL
• Choose one replica of data item to be the primary copy.
• Site containing the replica is called the primary site for that
data item
• Other copies are called slaves copes
• Different data item can have different primary site
• When a transaction needs to lock a data item Q9update) it
requests a primary site of Q
• The response request is delayed until it ca be granted.
• Implicitly gets lock on all the replicas of data item
Primary Copy 2PL
• Once the primary copy has been updated the change can b propagated to
slaves copies.
• The propagation should be carried out as soon as possible to prevent other
transactions from reading out of data values.
• If the primary site Q fails, Q is inaccessible even though other site
containing a replica may be accessible
Distributed Lock Manager
• Lock managers are assigned at each site.
• Lock managers control access to local data items
• When a transaction wishes to lock a data item Q, which is not replicated and
resides at the site S3, a message is sent to the lock manager at the site S3
requesting a lock
• If Q is locked in a n incompatible mode, the request is delayed until it is granted
• Once the lock request is granted the lock manager messages back to the initiator
indicating that it has granted the lock request.
Distributed Lock Manager Contd..
• When a transaction wishes to lock a data item Q, which is replicated,
Read- One-Write-All is implemented
• Any copy can be used for reading a data item.
• All copies must be write locked before an item can be updated.
• Work is distributed and can be robust in failure
• Deadlock detection is more complicated due to multiple lock managers
Timestamp Based Protocols
• Time stamp= assigning time to a transaction or asigning logical counter to any new transaction
• Basic idea of Time stamping is to decide the order between the transactions before that enters into the
system. So that in case of conflict during execution, we can resolve the conflict using ordering.
• Time stamp-based protocols uses a timestamp to serialize the execution of concurrent transactions
• The reason we call timestamp not stamp because for stamping we use the value of system clock (as it will
always be unique or never repeat itself)
• This protocols ensure that every conflicting read and write operations are executed in time stamp order
• Every transaction is timestamp with its start time
Two ideas of time stamping
• Time stamp with transactions
• With each transaction Ti, we associated a time stamp denoted by T.S(Ti)
• It is the value of system clock when a transaction enters into the system,
so if a new transaction Tj enters after Ti then T.S(Ti) ,(Tj) always unique
will remain fixed through the execution
Two ideas of time stamping Contd…

Time stamp with the data item:

 For each data item Q protocol maintains two time


stamps
 W-time stamp(Q): is the latest time stamp of
any transaction that executed Write(Q)
successfully
 R-time stamp (Q): is the latest time stamp of
any transaction that executed Read(!)
successfully
Basic TO Protocol
• The basic TO protocol in the following two cases:
• Transaction T issues a read (A) operations:
• If (Write_TS(A)> TS(T))
• Abort T and Rollback: else
• (Read(A): Read_TS(A)=TS(T);}
• Transaction t issues a write (A) operation,
• If (read_TS(A)>TS(T) or write_TS(A)>TS(T) abort T and Rollback;
• Else{ write(A);
• Write TS(A)=TS(T);}
Conservative Timestamp Ordering
• Each Data manager maintains:
• A read queue (RQ)
• A write queue (WQ)
• For each transaction manager, TM, Let
TS(Q) denote the timestamp of the first
operation in Q
• Let TS(Q) denote the timestamp of the first
operation
• Read<object, TS>
Conservative Timestamp Ordering
• If (non-empty(WQ) and TS(WQ)> TS for i=1,2,…N
• Then execute the read operation
• Else add the read operation to RQ;
• Write<object, val, TS>
• If (non-empty (RQi) and non-empty (WQi) and TS(RQi)>TS or TS(WQi) >TS for
=1…N
• Then execute the write operation
• Else add the write operation to WQ
Unique timestamp generation is difficult in DDBMS then
centralized DBMS

• The principle idea behind the timestamp based concurrency control


technique is that a unique timestamp is assigned to each transaction to
determined the serialization order of transaction in distributed system.
• The serialization order is determined depending on the timestamp values
of transactions prior to the execution of transactions
• A timestamp value is a simple identifier that is used to define each
transaction uniquely and permit ordering.
Unique Timestamp Generation Contd…
• In a centralized DBMS, generation of the unique timestamp values for a transaction can be handled
in a simpler way while in a DDBMS time stamp values must be derived from a totally ordered
domain.
• Here are two primarily methods for generating unique timestamp value in a distribute
environment :centralized and distributed.
• In the centralized approach, a single site is responsible for assigning unique timestamp values to all
transactions that are generated at different sites of the distributed system. The central site can use a
logical counter or its own system clock for assigning timestamp values. The centralized approach is
very simple.
• In distributed approach, each site generate unique local timestamp by logical counter or by using its
own system clock . This approach is difficult as compared to centralized one.
Validation Based Protocols
• Validation based protocol is also called optimistic concurrency control technique.
• In the validation based protocol, the transaction Ti is executed in the following three phases:
• Read Phase: in this phase, the transaction Ti reads the value of various data item and stores
them in temporary local variables of Ti i.e. it does not update actual database.
• Validation Phase: Transaction Ti perform validation test whether it can copy value of its local
variable to database. Checking is performed to make sure that there is no violation of
serializability when the transaction updates are applied to the database.
• Write Phase: If the validation of transaction Ti is validated, then the temporary results are
written to the database i.e. system applies actual update to the database.
Validation Based Protocol Contd…
• To perform validation test, we need to know when the various phases of transaction Ti took
place. We shall therefore associate three different timestamp with transaction Ti. Here each
phase has the following different timestamps:
• Start(Ti): it is the time when Ti started its execution.
• Validation(Ti): It is the time when Ti just finished its read phase and begin its validation
phase.
• Finish(Ti): the time when Ti end its all writing operations in the database under the write-
phase.
• We determine the serializbility order by timestamp ordering technique, using the value of
timestamp validation (Ti). Thus value of TS(Ti)=validation(Ti)
Validation Test
• The validation test for transaction Tj requires that for all the transaction Ti
with TS(Ti)<TS(Tj) one of the following condition hold.
• Finish(Ti)<Starts(Tj), since Ti finished before Tj started hence
serializability order is indeed maintained.
• Finish(Ti)<Validation (Tj). This ensures actual write by Ti and Tj will not
overlap.
• Validation(Ti)<Validation (Tj). It ensures that Ti has completed read phase
before Tj completed read phase.
Example
Advantages
• Avoid Cascading-rollbacks: This validation based scheme avoid cascading
rollbacks since the final write operations to the database are performed
only after the transaction passes the validation phase. If the transaction
fails then no updation operation is performed in the database So no dirty
read will happen hence possibilities cascading-rollback would be null.
• Avoid Deadlock: Since a strict time stamping based technique is used to
maintain the specific order of transactions. Hence deadlock is not possible
in this scheme.
Disadvantages
• Starvation: There might be a possibility of starvation for long-term
transactions, due to a sequence of conflicting short-term transactions that
cause the repeated sequence of restarts of the long-term transactions so
on .
Thanks
• Any Question???

You might also like