Transactions and Concurrency Control
By:
Prof.manoj kumar padhi
Transactions
Banking transaction for a customer (e.g., at ATM or browser)
Transfer $100 from saving to checking account;
Transfer $200 from money-market to checking account;
Withdraw $400 from checking account.
Transaction (invoked at client): /* Every step is an RPC */
1. savings.withdraw(100) /* includes verification */
2. checking.deposit(100) /* depends on success of 1 */
3. mnymkt.withdraw(200) /* includes verification */
4. checking. deposit(200) /* depends on success of 3 */
5. checking.withdraw(400) /* includes verification */
6. dispense(400)
7. commit
Client
Server
Transaction
Bank Server: Coordinator Interface
openTransaction() -> trans;
starts a new transaction and delivers a unique transaction identifier (TID) trans. This TID will be used in the other operations in the transaction.
closeTransaction(trans) -> (commit, abort);
ends a transaction: a commit return value indicates that the transaction has committed; an abort return value indicates that it has aborted.
abortTransaction(trans);
aborts the transaction.
Bank Server: Account, Branch interfaces
deposit(amount)
deposit amount in the account
withdraw(amount)
withdraw amount from the account
getBalance() -> amount
return the balance of the account
setBalance(amount)
set the balance of the account to amount
create(name) -> account
create a new account with a given name
lookup(name) -> account
return a reference to the account with the given name
branchTotal() -> amount
return the total of all the balances at the branch
Operations of the Branch interface
Operations of the Account interface
Transaction
But…
Transaction Failure Modes
Transaction:
1. savings.deduct(100)
2. checking.add(100)
3. mnymkt.deduct(200)
4. checking.add(200)
5. checking.deduct(400)
6. dispense(400)
7. commit
A failure at these points means the customer loses money; we need to restore old state
A failure at these points does not cause lost money, but old steps cannot be repeated
This is the point of no return
A failure after the commit point (ATM crashes) needs corrective action; no undoing possible.
Transactions in Traditional Databases (ACID)
Concurrent Transactions:Lost Update Problem
consider three account objects
Transaction T1 Transaction T2
balance = b.getBalance()
balance = b.getBalance()
b.setBalance(balance*1.1)
b.setBalance(balance*1.1)
a.withdraw(balance* 0.1)
c.withdraw(balance*0.1)
T1/T2’s update on the shared object, “b”, is lost
100
200
300
a:
b:
c:
280
c:
80
a:
220
b:
220
b:
Conc. Trans.: Inconsistent Retrieval Prob.
Transaction T1 Transaction T2
a.withdraw(100)
total = a.getBalance()
total = total + b.getBalance
b.deposit(100)
total = total + c.getBalance
T1’s partial result is used by T2, giving the wrong result for T2
100
200
0.00
a:
b:
00
a:
500
200
300
c:
total
300
b:
Concurrency Control: “Serial Equivalence”
Transaction T1 Transaction T2
balance = b.getBalance()
b.setBalance(balance*1.1)
balance = b.getBalance()
b.setBalance(balance*1.1)
a.withdraw(balance* 0.1)
c.withdraw(balance*0.1)
100
200
300
a:
b:
c:
278
c:
a:
242
b:
b:
220
80
== T1 (complete) followed
by T2 (complete)
Checking Serial Equivalence – �Conflicting Operations
Read and Write Operation Conflict Rules
Operations of different
transactions
Conflict
Reason
read
read
No
Because the effect of a pair of
read
operations
does not depend on the order in which they are
executed
read
write
Yes
Because the effect of a
read
and a
write
operation
depends on the order of their execution
write
write
Yes
Because the effect of a pair of
write
operations
depends on the order of their execution
Concurrency Control: “Serial Equivalence”
Transaction T1 Transaction T2
balance = b.getBalance()
b.setBalance(balance*1.1)
balance = b.getBalance()
b.setBalance(balance*1.1)
a.withdraw(balance* 0.1)
c.withdraw(balance*0.1)
100
200
300
a:
b:
c:
278
c:
a:
242
b:
b:
220
80
== T1 (complete) followed
by T2 (complete)
Pairs of Conflicting Operations
Conflicting Operators Example
Transaction T1 Transaction T2
x= a.read()
a.write(20) y = b.read()
b.write(30)
b.write(x)
z = a.read()
x= a.read()
a.write(20) z = a.read()
b.write(x)
y = b.read()
b.write(30)
Serially equivalent interleaving of operations
(why?)
Conflicting Ops.
Non-serially equivalent interleaving of operations
Inconsistent Retrieval Prob
Transaction T1 Transaction T2
a.withdraw(100)
total = a.getBalance()
total = total + b.getBalance
b.deposit(100)
total = total + c.getBalance
T1’s partial result is used by T2, giving the wrong result for T2
100
200
0.00
a:
b:
00
a:
500
200
300
c:
total
300
b:
A Serially Equivalent Interleaving of T1 and T2
Transaction
T1
:
a.withdraw(100);
b.deposit(100)
Transaction
T2
:
aBranch.branchTotal()
a.withdraw(100);
$100
b.deposit(100)
$300
total = a.getBalance()
$100
total = total+b.getBalance()
$400
total = total+c.getBalance()
...
Implementing Concurrent Transactions
Concurrency control
Transactions should not read a “stale” value & use it in
computing a new value
Concurrency control
Update transactions should not interfere with retrievals.
In general:
Transactions should not violate operation conflict rules.
Concurrency control
Serial equivalence
criterion for correct concurrent execution
T1 serially equivalent with T2 iff:
All pairs of conflicting operations of the two transactions are executed in the same order at all objects that both transactions access.
3 approaches to CC:
Tx’s wait for one another
OR:
Restart Tx’s after conflicts
have been detected
Recoverability from aborts
Tx’s should only read values written by committed Tx’s
Recoverability from aborts
Tx’s should be delayed until earlier Tx’s that update the
Same objects have been either committed or aborted.
Recoverability from aborts
Tx’s should be delayed until earlier Tx’s that update the
Same objects have been either committed or aborted.
Concurrency Control: Locks
Operations are synchronized before they are carried out
Operations are carried out, synchronization at the end of the transaction
Locks: Basics
Locking
2PL
Example: Concurrent Transactions
Transaction T1 Transaction T2
OpenTransaction()
balance = b.getBalance() OpenTransaction()
balance = b.getBalance()
b.setBalance(balance*1.1)
a.withdraw(balance* 0.1)
CloseTransaction()
b.setBalance(balance*1.1) c.withdraw(balance*0.1)
CloseTransaction()
Lock B
Lock A
UnLock B
UnLock A
Lock C
UnLock B
UnLock C
…
WAIT on B
Lock B
…
Basic Locking
Basic Locking
2P Locking: Non-exclusive lock (per object)
non-exclusive lock compatibility
Lock already Lock requested
set read write
none OK OK
read OK WAIT
write WAIT WAIT
Two Phase Locking (2PL) Protocols
Growing phase
Shrinking phase
Time
No. of
Locks
Locking Procedure in Strict-2P Locking
Example: Concurrent Transactions
Transaction T1 Transaction T2
OpenTransaction()
balance = b.getBalance() OpenTransaction()
balance = b.getBalance()
b.setBalance(balance*1.1)
Commit
R-Lock B
…
R-Lock B
Cannot Promote lock on B, Wait
Promote lock on B
Example: Concurrent Transactions
Transaction T1 Transaction T2
OpenTransaction()
balance = b.getBalance() OpenTransaction()
balance = b.getBalance()
b.setBalance(balance*1.1)
b.setBalance=balance*1.1
R-Lock B
…
R-Lock B
Cannot Promote lock on B, Wait
Cannot Promote lock on B, Wait
…
Deadlock with write locks
Transaction
T
Transaction
U
Operations
Locks
Operations
Locks
a.deposit(100);
write lock
A
b.deposit(200)
write lock
B
b.withdraw(100)
waits for
U
’s
a.withdraw(200);
waits for
T
’s
lock on
B
lock on
A
T locks A and waits for U to release the lock on B, U on the other
hand locks B and waits for T to release the lock on A
🡺 Circular hold and wait 🡺 Deadlock
�The corresponding wait-for graph
B
A
Waits for
Held by
Held by
T
U
U
T
Waits for
Deadlocks
T
U
Wait for
Held by
Held by
Wait for
A
B
T
U
Wait for
Held by
Held by
Wait for
A
B
V
W
...
...
Wait for
Wait for
Held by
Held by
Hold & Wait
Circular Wait
Naïve Deadlock Resolution Using Timeout
Transaction T
Transaction U
Operations
Locks
Operations
Locks
a.deposit(100);
write lock
A
b.deposit(200)
write lock
B
b.withdraw(100)
waits for
U
’s
a.withdraw(200);
waits for T’s
lock on
B
lock on
A
(timeout elapses)
T’s lock on
A
becomes vulnerable,
unlock
A
, abort T
a.withdraw(200);
write locks
A
unlock
A
,
B
Disadvantages?
Strategies to Fight Deadlock
Optimistic Concurrency Control �(Kung and Robinson)
Optimistic Concurrency Control �(Kung and Robinson)
Optimistic Concurrency Control:�
Earlier committed
transactions
Working
Validation
Update
T
1
T
v
Transaction
being validated
T
2
T
3
Later active
transactions
active
1
active
2
Validation of transactions
Validation Rules
Tv | Ti | Rule |
write | read | 1. Ti must not read objects written by Tv |
read | write | 2. Tv must not read objects written by Ti |
write | write | 3. Ti must not write objects written by Tv and Tv must not write objects written by Ti |
�Validation of Transactions
Backward validation of transaction Tv
boolean valid = true;
for (int Ti = startTn+1; Ti <= finishTn; Ti++){
if (read set of Tv intersects write set of Ti) valid = false;
}
Forward validation of transaction Tv
boolean valid = true;
for (int Tid = active1; Tid <= activeN; Tid++){
if (write set of Tv intersects read set of Tid) valid = false;
}
Summary