Module 6: Distributed Transactions
Module 6: Distributed Transactions
Transactions
Outline
• Transaction And Concurrency
Control- Nested Transactions- Locks -
Optimistic Concurrency Control-
Timestamp Ordering.
• Distributed Transactions- Flat and
Nested, Atomic, Two phase Commit
protocol, Concurrency Control
SCOPE, VIT Chennai
Text Book
• Coulouris, G. (2012). Distributed
systems. Boston: Addison-Wesley
Transaction T:
a.withdraw(100);
b.deposit(100);
abortTransaction(trans);
aborts the transaction.
If a transaction aborts for any reason (self abort or server abort), it must be
guaranteed that future transaction will not see its effect either in the object or
in their copies in permanent storage.
a.withdraw(100); $100
total = a.getBalance() $100
total = total+b.getBalance() $300
total = total+c.getBalance()
b.deposit(100) $300
prov.commit
An object can be read and write. From the compatibility table, we know pairs of read
operations from different transactions do not conflict. So a simple exclusive lock
used for both read and write reduces concurrency more than necessary. (Many
readers/Single writer)
Rules;
1. If T has already performed a read operation, then a concurrent transaction U
must not write until T commits or aborts.
2. If T already performed a write operation, then concurrent U must not read or
write until T commits or aborts.
SCOPE, VIT Chennai
Shared Lock and Exclusive
lock
• Locks must be obtained before read/write
can begin
• If a transaction want to read and write the
same object, it can either
– Obtain an X-lock before reading and unlock it
immediately afterwards
– Obtain an S-lock before reading, then obtain an X-
lock before writing. And unlock it immediately
afterwards
T1 (Transfer) T2 (Dividend)
5. X-lock(X)
5. X-lock(X)
T1 6. Write(X, A1) T2
7. Unlock(X) T2 can go ahead
8. …. 5. X-lock(X)
SCOPE, VIT Chennai
Lock based protocol – need
S-lock (X)
for a protocol
1. A1 <- Read(X)
Unlock(X) 2. A1 <- A1 – k
3. Write(X, A1)
X-lock (X)
S-lock (X)
Unlock(X) 1. A1 <- Read(X) Unlock(X)
2. A1 <- A1* 1.01
X-lock(X)
3. Write(X, A1) Unlock(X),
S-lock(Y)
4. A2 <- Read(Y) Unlock(Y)
5. A2 <- A2 * 1.01
X-lock(Y)
6. Write(Y, A2) Unlock(Y),
S-lock (Y)
Unlock(Y)
4. A2 <- Read(Y)
5. A2 <- A2 + k
X-lock (Y)
6. Write(Y, A2)
Unlock(Y)
X : 100 -> 50 -> 50.5; Y : 200 -> 202 -> 252; X+Y = 302.5
Dirty reads caused by a read in one transaction U and an earlier unsuccessful write
in another transaction T on the same object.
T will be rolled back and restore the original a value, thus U will have seen a value
that never existed. U is committed, so cannot be undone. U performs a dirty read.
SCOPE, VIT Chennai
Premature Write: Overwriting
uncommitted values
$100 Transaction T: Transaction U:
a.setBalance(105) a.setBalance(110)
a.setBalance(105) $105
a.setBalance(110) $110
Premature write: related to the interaction between write operations on the same object
belonging to different transactions.
a. If U aborts and then T commit, we got a to be correct 105.
Some systems restore value to “Before images” value for abort action, namely the value
before all the writes of a transaction. a is 100, which is the before image of T’s write. 105 is
the before image of U’s write.
b. Consider if U commits and then T aborts, we got wrong value of 100.
c. Similarly if T aborts then U aborts, we got 105, which is wrong and should be 100.
So to ensure correctness, write operations must be delayed until earlier transactions that
updated the same object have either committed or aborted.
SCOPE, VIT Chennai
Strict two-phase Locking
Protocol
• Because transaction may abort, strict
execution are needed to prevent
dirty reads and premature writes,
– which are caused by read or write to same
object accessed by another earlier
unsuccessful transaction that already
performed an write operation.
Waits for B
Held by
V T
Held by W
T Held by Held by
C
U
Held by B
U
W V
Waits for
T1
Earlier committed
transactions
T2
T3
Transaction
being validated Tv
active
1
Later active
active
transactions 2
2. write write Tc must not write an object that has been written by any Ti where Ti > Tc
this requires that Tc > write timestamp of the committedobject.
3. read write Tc must not read an object that has been written by any Ti whereTi > Tc
this requires that Tc > write timestamp of the committed object.
Each request from a transaction is checked to see whether it conforms to the operation
conflict rules. Conflict may occur when previous done operation from other transaction Ti is
later than current transaction Tc.
It means the request is submitted too late.
T2 T1 T2 Key:
Before Before Ti
Committed
After T2 T3 After T1 T2 T3
Ti
Time Time
Tentative
object produced
(c) T3 write (d) T3 write by transaction Ti
Transaction
T1 T4 T4 aborts T1<T2<T3<T4
Before Before
After T1 T3 T4 After T4
Time Time
read read
T2 T2 T4 proceeds
proceeds Ti
Selected Committed
Selected Time
Time
Ti
(c) T3 read (d) T3 read
Tentative
Selected
Time Time