You are on page 1of 18

Department of Computer Science

U i
University
it off Cyprus
C
EPL446 – Advanced Database Systems

Lecture 16
Concurrency Control with Timestamps
Chapter 18.2
18.2-18.4
18.4 (except 18.3.2): Elmasri & Navathe, 5ED
Chapter 17.6: Ramakrishnan & Gehrke, 3ED

Demetris Zeinalipour
http://www cs ucy ac cy/~dzeina/courses/epl446
http://www.cs.ucy.ac.cy/ dzeina/courses/epl446
16-1
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Concurrency Control in DBMSs
(Έλ
(Έλεγχος Τ
Ταυτοχρονίας
ί σε Σ∆Β∆)
• In the previous lecture we explained how a real DBMS
enforces (επιβάλει) Serializability and Recoverability (Strict
2PL) in its transaction schedules using Locking
• We will now see another class of protocols based on
Timestamps (though not widely utilized in real DBMSs, they
have a theoretical interest)
interest).
• Concurrency Control with Timestamps (without Locking)
– Timestamp Ordering (Έλεγχος Ταυτοχρονισμού με ∆ιάταξη
Χρονόσηµων): Ensure serializability using the ordering of timestamps
generated by the DBMS.
– Multiversion CC (Έλεγχος Ταυτοχρονισμού Με Πολλαπλές
Εκδόσεις): Use multiple version of items to enforce serializability.
– Optimistic CC (Αισιόδοξος (Οπτιμιστικός) Έλεγχος
Ταυτοχρονισμού): No checking done during execution of a Transaction
but post-execution validation (επικύρωση) enforces serializability. 16-2
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Timestamp based CC: Definitions
(Έλ
(Έλεγχος Τ
Ταυτοχρονίας
ί με Χ
Χρονόσημα:
ό Ο
Ορισμοί)
ί)
• Timestamp
p ((Χρονόσημο)
ρ ημ )
– A monotonically increasing variable (integer)
indicating the age of an operation or a transaction.
• A larger timestamp indicates a more recent transaction
– Timestamps are assigned in our context during Xact creation.
– Using date timestamps (e.g., a long integer which represents
the number of seconds that have passed from 1/1/1970)
• TS1: 1237917600 (2009
(2009-03-24
03 24 18:00:00)
• TS2: 1237917610 (2009-03-24 18:00:10)
– Using a counter timestamp (e.g., using a counter stored inside
th O
the Operating
ti S System
t kernel
k l as a semaphore)
h )
10 sec Now

TS1 TS2 Time


16-3
OlderAdvanced
EPL446: Transaction Younger
Database Systems Transaction
- Demetris Zeinalipour (University of Cyprus)
Timestamp based CC: Definitions
(Έλ
(Έλεγχος Τ
Ταυτοχρονίας
ί με Χ
Χρονόσημα:
ό Ο
Ορισμοί)
ί)
• Assume a collection of data items that are accessed, with read and
write operations, by transactions.
• For each data item X the DBMS maintains the following values:
– RTS(X): The Timestamp on which object X was last read (by some
transaction Ti, i.e., RTS(X):=TS(Ti))
– WTS(X): The Timestamp on which object X was last written (by
some transaction Tj, i.e., WTS(X):=TS(Tj))
• For the following algorithms we use the following assumptions:
– A data item X in the database has a RTS(X) and WTS(X) (recorded
when the object was last accessed for the given action)
– A transaction T attempts to perform some action (read or write) on
data item X on timestamp TS(T)
• Problem: We need to decide whether T has to be aborted or whether T can
continue execution. Transaction T needs to perform some
action (read, write) on data item X
RTS(X) WTS(X)
TS(T) Action(X)
TS(T): A ti (X)
16-4
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Time
Cyprus)
TS(Ti) TS(Tj)
Basic Timestamp Ordering Algorithm
(Βασικός Αλγόριθμος ∆ιάταξης Χρονοσήμων)
• We shall now p
present the first algorithm,
g , coined Basic
Timestamp Ordering (TO), that utilizes Timestamps to
guarantee serializability of concurrent transactions.
• Timestamp
Ti t Ordering
O d i (TO) Rule R l
– if pi(x) and qj(x) are conflicting operations, of xacts Ti and Tj for
item x, then pi(x) is processed before qj(x) iff (ÍÎ) ts(Ti) < ts(Tj)
– Main Idea: Conflicts are only allowed from older transactions
(with smaller ts) to a younger transaction T (with larger ts)
– Main Idea Example: WTS(X) or N t that
Note th t the
th conflict
fli t
TS(T)
transaction RTS(X) moves only to the
with smaller
T with larger right not to the left!
timestamp timestamp Time

• Theorem: If the TO rule is enforced in a schedule then


the schedule is (conflict) serializable.
• Why?
Wh ? Because
B cycles
l are nott possible
ibl in
i th
the Conflict
C fli t
Precedence Graph (Γράφος Προτεραιότητας Συγκρούσεων)!
16-5
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Basic Timestamp Ordering Algorithm
(Βασικός Αλγόριθμος ∆ιάταξης Χρονοσήμων)
• Basic Timestamp
p Ordering
g (TO)
( ) Algorithm
g
Case 1 (Read): Transaction T issues a read(X) operation
A. If TS(T) < WTS(X), then read(X) is rejected (as the TO rule is violated).
T has to abort and be rejected.
rejected
TS(T): Read(X) WTS(X) Reject T
(TO rule violated)
Time
X
B If WTS(X) ≤ TS(T),
B. TS(T) then execute read(X) of T and update RTS(X).
RTS(X)
WTS(X) TS(T): Read(X)
Accept T (TO ok)
Time

Case 2 (Write): Transaction T issues a write(X) operation


A. If TS(T) < RTS(X) or if TS(T) < WTS(X), then write is rejected.
TS(T): Write(X) RTS(X) TS(T): Write(X) WTS(X)
Reject T (TO rule violated)
X Time
X Time
B. If RTS(X) ≤ TS(T) or WTS(X) ≤ TS(T), then execute write(X) of T and
p
update WTS(X)( )
RTS(X) TS(T): Write(X) WTS(X) TS(T): Write(X)
Accept T (TO ok) 16-6
EPL446: AdvancedTime
Database Systems - DemetrisTime
Zeinalipour (University of Cyprus)
Basic TO Algorithm Example
(Παράδειγμα Βασικός Αλγόριθμος ∆ιάταξης Χρονοσήμων)

• Consider the following scenario:


– Two transactions T1 and T2
– Initially RTS=0 and WTS=0 for data items X,
X Y
– Timestamps are as follows: TS(T1)=10 and TS(T2)=20
T1(10) T2(20)
1. A1 = Read(X) 1. A1 = Read(X)
2 A1 = A1 – k
2. 2 A1 = A1 * 1
2. 1.01
01
3. Write(X, A1) 3. Write(X, A1)
4. A2 = Read(Y) 4. A2 = Read(Y)
5. A2 = A2 + k 5. A2 = A2 * 1.01
6. Write(Y, A2) 6. Write(Y, A2)

16-7
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Basic TO Algorithm Example
(Π άδ
(Παράδειγμα Βασικός
Β ό Αλγόριθμος
Αλ ό θ ∆ιάταξης
∆ ά ξ Χρονοσήμων)
Χ ή )

• Is the schedule serializable?


– Utilize the Basic TO Algorithm to justify your answer (otherwise the
precedence graph could have be used to answer this question)
T1(10) T1(20)
RTS(X) : 10 1. A1 = Read(X)
WTS(X)
( ) : 10
RTS(Y) : 0 2 A1 = A1 – k
2. WTS(X) TS(T): Read(X)

WTS(Y) : 0 3. Write(X, A1) Time

1. A1 = Read(X) RTS(X) : 20
WTS(X) : 20
2 A1 = A1*
2. A1 1.01
1 01 RTS(Y) : 0
3. Write(X, A1) WTS(Y) : 0
RTS(X) : 20 4. A2 = Read(Y) WTS(X) TS(T): Write(X)
WTS(X) : 20
5 A2 = A2 + k
5.
RTS(Y) : 10 Time
WTS(Y) : 10 6. Write(Y, A2)
4. A2 = Read(Y) RTS(X) : 20
Yes! The schedule is serializable! 5. A2 = A2 * 1.01 WTS(X) : 20
RTS(Y) : 20
This can be confirmed by the 6. Write(Y, A2) WTS(Y) : 20
precedence graph which is acyclic 16-8
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Basic TO Algorithm Example
(Π άδ
(Παράδειγμα Βασικός
Β ό Αλγόριθμος
Αλ ό θ ∆ιάταξης
∆ ά ξ Χρονοσήμων)
Χ ή )

• Is the schedule serializable?


– Utilize the Basic TO Algorithm to justify your answer
T1(10) T1(20)
RTS(X) : 10 1. A1 = Read(X)
WTS(X) : 10
RTS(Y) : 0
2. A1 = A1 – k
WTS(X) TS(T): Read(X)
WTS(Y) : 0 3. Write(X, A1)
Time
1.
1 A1 = Read(X)
R d(X)
2. A1 = A1* 1.01 WTS(X) TS(T): Write(X)

3. Write(X, A1)
Time
4
4. A2 = R
Read(Y)
d(Y)
RTS(X) : 20
5. A2 = A2 * 1.01 WTS(X) : 20
6. Write(Y, A2) RTS(Y) : 20
4. A2 = Read(Y)
4 WTS(Y) : 20
5. A2 = A2 + k TS(T1)=10
WTS(Y)=
20
Reject T1 (TO rule violated)
6. Write(Y, A2) (Restart with new TS)
Time
X
NO! The schedule is NOT serializable
16-9
• this is confirmed
EPL446: with the
Advanced Database precedence
Systems - Demetrisgraph which
Zeinalipour is cyclic
(University of Cyprus)
Advantages/Disadvantages of Basic TO
(Πλεονεκ /Μειονεκ του Βασικού Αλγ.
(Πλεονεκ./Μειονεκ. Αλγ ∆ιατ.
∆ιατ Χρον.)
Χρον )
• Basic TO Remark
– Note that there is no notion of RR-conflict
If TS(T) < RTS(X), then execute read(X) of T and update RTS(X).

TS(T): Read(X) RTS(X) Accept T


Time
(TO rule NOT violated)

• Advantages
Ad t off Basic
B i TO Algorithm
Al ith
– Schedules are serializable (like 2PL protocols)
– No
N waiting
iti ffor ttransaction,
ti thus,
th no deadlocks!
d dl k !
• Disadvantages
– Schedule
S h d l may nott b
be recoverable
bl
• Solution: Utilize Strict TO Algorithm (see next)
– Starvation is possible (if the same transaction is
continually aborted and restarted) 16-10
• Solution:
EPL446: AssignSystems
Advanced Database new timestamp for aborted
- Demetris Zeinalipour transaction
(University of Cyprus)
Strict Timestamp Ordering
(
(Αυστηρός Αλγόριθμος
θ ∆ιάταξης
ξ Χρονοσήμων))
• The Basic T.O. algorithm guarantees serializability but not
recoverability (επαναφερσιμότητα)
• The Strict T.O. algorithms introduces recoverability.
– (Revision) Strict Schedule: A transaction can neither read or write
an uncommitted data item X.
• Strict T
T.O.
O Main Idea: Extend the Accept cases of the
Basic T.O. algorithm by adding the requirement that a
commit occu
co occurs
s be
before
oeTpproceeds
oceeds with its
s ope
operation.
a o i.e.,
e,
WTS(X) TS(T): Read(X)
Accept T (TO ok)
Time
commit

RTS(X) TS(T): Write(X) WTS(X) TS(T): Write(X)


Accept T (TO ok)
Time Time
commit commit 16-11
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Strict Timestamp Ordering
(
(Αυστηρός Αλγόριθμος
θ ∆ιάταξης
ξ Χρονοσήμων))
• Strict Timestamp Ordering (Strict T.O.)
– Case 1: Transaction T issues a read(X) operation:
• If WTS(X) < TS(T),
TS(T) then delay T until the transaction
T’ that wrote or read X has terminated (committed or
aborted).
)
WTS(X) TS(T): Read(X)
Accept T (TO ok)
Time
commit

– Case 2: Transaction T issues a write(X) operation:


• If RTS(X) < TS(T), then delay T until the transaction
T’ that wrote or read X has terminated (committed or
aborted).
RTS(X) TS(T): Write(X) WTS(X) TS(T): Write(X)
Accept T (TO ok)
Time Time 16-12
commit
EPL446: Advanced commitZeinalipour (University of Cyprus)
Database Systems - Demetris
Improving the Basic TO Algorithm
using
i ThThomas’s
’ WWrite
it RRule
l
Timestamp
Ti t based
b d concurrency control
t l algorithm
l ith
• Thomas’s Write Rule
– If read_TS(X) > TS(T) then abort and roll-back T and
reject the operation.
– If write_TS(X)
write TS(X) > TS(T)
TS(T), then just ignore the write
operation and continue execution. This is because
the most recent writes counts in case of two
consecutive writes.
– If the conditions given in 1 and 2 above do not occur,
then execute write_item(X)
write item(X) of T and set write
write_TS(X)
TS(X)
to TS(T).

Slide 18- 13 16-13


EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Multiversion Concurrency Control
(Έλεγχος Ταυτοχρονισμού Με Πολλαπλές Εκδόσεις)

• Multiversion technique based on timestamp


ordering (Έλεγχος Ταυτοχρονισμού Με
Πολλαπλέςς Εκδόσεις)
ς)
– This approach maintains a number of versions of a
data item and allocates the right version to a read
operation
ti off a transaction.
t ti
• Thus unlike other mechanisms a read operation in this
j
mechanism is never rejected.

• Disadvantage:
– Significantly more storage (RAM and Disk) is required
to maintain multiple versions.
– To
T check
h k unlimited
li it d growth
th off versions,
i a garbage
b
collection is run periodically. 16-14
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Multiversion Concurrency Control
(Έλεγχος Ταυτοχρονισμού Με Πολλαπλές Εκδόσεις)

• Multiversion technique based on Timestamp Ordering


– Assume X1, X2, …, Xn are the version of a data item X
created byy a write operation
p of transactions.
• Note: New version of Xi is created only by a write operation.
• With each Xi a RTS (read timestamp) and a WTS (write
timestamp)) are associated. RTS(X3) RTS(X1) … RTS(Xi) TS(T): Write(X)

• Notation Time
commit
– RTS(Xi): The read timestamp of Xi is the largest of all the
timestamps of transactions that have successfully read version Xi.
– WTS(Xi): The write timestamp of Xi that wrote the value of
version Xi.
• Basic Idea: Works much like Basic TO with the difference that
instead of WTS(X) and RTS(X) we now utilize the highest WTS(Xi)
and highest RTS(Xi) respectively.
16-15
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Multiversion Concurrency Control
(Έλεγχος Ταυτοχρονισμού Με Πολλαπλές Εκδόσεις)
• To ensure serializability, the following rules are used
– C1B) If transaction T issues read(X), find the version i of X that
has the highest WTS(Xi) of all versions of X that is also less than
or equal to TS(T),
TS(T) then accept the read and update the RTS(X)
respectively. Max{WTS(Xi)} TS(T): Read(X)
Accept T (TO ok)
Time

– C2A) If transaction T issues write(X) and version i of X has the


highest WTS(Xi) of all versions of X that is also less than or equal
to TS(T),
TS(T) and TS(T) < RTS(Xi),
RTS(Xi) then abort and rollroll-back
back T;
TS(T): Write(X) Max{RTS(Xi)} TS(T): Write(X) Max{WTS(Xi)}
Reject T (TO rule violated)
Time Time
X X
– C2B) otherwise create a new version Xi and read_TS(X) =
write_TS(Xj) = TS(T).
Max{RTS(Xi)}) TS(T): Write(X) Max{WTS(Xi)} TS(T): Write(X)
Accept T (TO ok)
Time Time 16-16
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Optimistic Concurrency Control

(Οπτιμιστικός
ό Έλεγχος
Έλ Τ
Ταυτοχρονίας)
ί )
• Locking and TO are pessimistic (απαισιόδοξοι) ways to
h dl concurrency (W
handle (We assume thath conflicts
fli will
ill arise)
i )
• When most transactions don’t conflict with the other
transactions then Optimistic CC is much more efficient
efficient.
(Optimistic) Concurrency Control
(Αισιόδοξος (Οπτιμιστικός) Έλεγχος Ταυτοχρονισμού)
• Basic Idea:
– In Optimistic CC, a schedule is checked against serializability
only at the time of commit (using timestamp orders)
• transactions are aborted in case of non-serializable
schedules.
• Three phases:
1. Read phase (Φάση Ανάγνωσης)
2 Validation
2. V lid ti phase
h (Φά
(Φάση Ε
Επικύρωσης)
ύ )
3. Write phase (Φάση Γραφής) 16-17
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)
Optimistic Concurrency Control

(Οπτιμιστικός
ό Έλεγχος
Έλ Τ
Ταυτοχρονίας)
ί )
1. Read phase:
– A transaction can read values of committed data items. However, updates are
applied only to local copies (versions) of the data items (in database cache).
2. Validation phase: Serializability is checked before
transactions write their updates to the database.
– This phase for Ti checks that, for each transaction Tj that is either committed or
is in its validation phase, one of the following conditions holds:
1. C1B) Tj completes its write phase before Ti starts its read phase. OR
2. C2Bii) Ti starts its write phase after Tj completes its write phase, and the
read_set of Ti has no items in common with the write_set of Tj OR
3 Both the read_set
3. read set and write_set
write set of Ti have no items in common with the
write_set of Tj, and Tj completes its read phase.
– When validating Ti, the first condition is checked first for each transaction Tj, since
(1) is the simplest condition to check.
– If (1) iis ffalse
l ththen (2) iis checked
h k d and
d if (2) iis ffalse
l ththen (3 ) iis checked.
h k d
– If none of these conditions holds, the validation fails and Ti is aborted

3. Write phase: On a successful validation transactions’


updates
d t are applied
li d tto th
the d
database;
t b otherwise,
th i
transactions are restarted. 16-18
EPL446: Advanced Database Systems - Demetris Zeinalipour (University of Cyprus)

You might also like