Lecture 9 Distributed Transactions
Lecture 9 Distributed Transactions
Serializability
• Execution of some concurrent transactions yield results
o "results" means both changes (T1) and output (T2) in the DB
• The results are serializable if:
o There exists a serial execution order of the transactions
o Yields the same results as the actual execution
§ Serial means one at a time
• No parallel execution
§ This definition should remind you of linearizability
• You can test whether an execution's result is serializable by looking
for an order that yields the same results
o For our example, the possible serial orders are
T1; T2 or T2; T1
• So, the correct (serializable) results are:
T1; T2: x=11 y=9 "11,9"
T2; T1: x=11 y=9 "10,10"
• The results for the two differ; either is okay
o No other result is okay
• The implementation might have executed T1 and T2 in parallel
o But it must still yield results as if in a serial order
What if T1's operations run entirely between T2's two get()s? would the
result be serializable?
• T2 would print 10,9
• But 10,9 is not one of the two serializable results!
Why hold locks until after commit/abort? why not release as soon as done with
the record?
• Example 1 of a resulting problem:
o Suppose T2 releases x's lock after get(x)
o T1 could then execute between T2's get()s
o T2 would print 10,9
o Oops: that is not a serializable execution: neither T1;T2 nor
T2;T1
• Example 2 of a resulting problem:
o Suppose T1 writes x, then releases x's lock
o T2 reads x and prints
o T1 then aborts
§ Maybe because the value of y is already 0
o Oops: T2 used a value that never really existed
o We should have aborted T2, which would be a "cascading abort"
• Deadlock!
T1 T2
get(x) get(y)
get(y) get(x)
• The system must detect cycles and abort a transaction (e.g., by using
lock timeout)
First-cut approach:
Challenges
• Failures, performance
The setting
Correctness
• Neither A nor B can commit unless they both agreed
Fault Tolerance
1. Node Failure
What if B crashes and restarts?
• If B sent YES before crash, B must remember (despite crash)!
• Because TC might have sent commit message to A and A might have already
committed
• So, B must be able to commit (or not) even after a reboot
Thus, participants must write persistent (on-disk) state:
• B must remember on disk before saying YES, including modified data
• If B reboots, and disk says YES but no COMMIT
o B must ask TC or wait for TC to re-send
• And meanwhile, B must continue to hold the transaction's locks
• If TC says COMMIT, B copies modified data to real data
2. Network Failure
What if TC never gets a YES/NO from B?
• Perhaps B crashed and didn't recover; perhaps network is broken
• TC can time out, and abort (since has not sent any COMMIT messages)
• Good: allows servers to release locks
Note:
• The commit/abort decision is made by a single entity => the TC
• This makes two-phase commit relatively straightforward
• The penalty is that A/B, after voting YES, must wait for the TC