L6 Transactions II
L6 Transactions II
Michael Freedman
Q: What if access patterns
rarely, if ever, conflict?
2
Be optimistic!
• Goal: Low overhead for non-conflicting txns
• Assume success!
– Process transaction as if would succeed
– Check for serializability only at commit time
– If fails, abort transaction
• Modify phase:
– Txn can read values of committed data items
– Updates only to local copies (versions) of items (in db cache)
• Validate phase
• Commit phase
– If validates, transaction’s updates applied to DB
– Otherwise, transaction restarted
4
OCC: Why validation is necessary
txn
P coord
When commits txn updates,
create new versions at Q
some timestamp t
5
OCC: Validate Phase
• Transaction is about to commit.
System must ensure:
– Initial consistency: Versions of accessed objects
at start consistent
6
OCC: Validate Phase
• Validation needed by transaction T to commit:
10
Multi-version concurrency control
• Maintain multiple versions of objects, each with own
timestamp. Allocate correct version to reads.
11
Multi-version concurrency control
• Maintain multiple versions of objects, each with own
timestamp. Allocate correct version to reads.
12
MVCC Intuition
• Split transaction into read set and write set
– All reads execute as if one “snapshot”
– All writes execute as if one later “snapshot”
13
Serializability vs. Snapshot isolation
• Intuition: Bag of marbles: ½ white, ½ black
• Transactions:
– T1: Change all white marbles to black marbles
– T2: Change all black marbles to white marbles
15
Executing transaction T in MVCC
• Find version of object O to read:
– # Determine the last version written before read snapshot time
– Find OV s.t. max { WriteTS(OV) | WriteTS(OV) <= TS(T) }
– ReadTS(OV) = max(TS(T), ReadTS(OV))
– Return OV to T
• Perform write of object O or abort if conflicting:
– Find OV s.t. max { WriteTS(OV) | WriteTS(OV) <= TS(T) }
– # Abort if another T’ exists and has read O after T
– If ReadTS(OV) > TS(T)
• Abort and roll-back T
– Else
• Create new version OW
16
• Set ReadTS(O ) = WriteTS(O ) = TS(T)
Distributed Transactions
25
Consider partitioned data over servers
L R U
O
L R W U
P
L W U
Q
26
Consider partitioned data over servers
L R U
O
L R W U
P
L W U
Q