Atomic Commit and Concurrency Control: COS 418: Distributed Systems Wyatt Lloyd
Atomic Commit and Concurrency Control: COS 418: Distributed Systems Wyatt Lloyd
Concurrency Control
Wyatt Lloyd
Lets Scale Strong Consistency!
1. Atomic Commit
• Two-phase commit (2PC)
2. Serializability
• Strict serializability
3. Concurrency Control:
• Two-phase locking (2PL)
• Optimistic concurrency control (OCC)
Atomic Commit
• Atomic: All or nothing
• Order product
• Charge customer card
• Decrement stock
• Ship stock
Relationship with Replication
• Replication (e.g., RAFT) is about doing the same
thing multiple places to provide fault tolerance
• Unilateral abort
• Two-phase commit
• Prepare -> Commit/abort
Lets Scale Strong Consistency!
1. Atomic Commit
• Two-phase commit (2PC)
2. Serializability
• Strict serializability
3. Concurrency Control:
• Two-phase locking (2PL)
• Optimistic concurrency control (OCC)
Two Concurrent Transactions
transaction transfer(A, B):
begin_tx
a ß read(A)
if a < 10 then abort_tx
transaction sum(A, B): else write(A, a−10)
begin_tx b ß read(B)
write(B, b+10)
a ß read(A) commit_tx
b ß read(B)
print a + b
commit_tx
13
Isolation Between Transactions
• Isolation: sum appears to happen either
completely before or completely after transfer
• i.e., it appears that all operations of a
transaction happened together
14
Problem from Concurrent Execution
16
Equivalence of Schedules
• Two operations from different transactions are
conflicting if:
1. They read and write to the same data item
2. The write and write to the same data item
17
Serializability
• A schedule is serializable if it is equivalent to
some serial schedule
• i.e., non-conflicting operations can be
reordered to get a serial schedule
18
A Serializable Schedule
• A schedule is serializable if it is equivalent to
some serial schedule
• i.e., non-conflicting operations can be
reordered to get a serial schedule
transfer: rA wA rB wB ©
sum: rA rA rB ©
19
A Non-Serializable Schedule
• A schedule is serializable if it is equivalent to
some serial schedule
• i.e., non-conflicting operations can be
reordered to get a serial schedule
transfer: rA wA rB wB ©
sum: rA rB ©
Time à
But in a Conflicting
serial schedule,
ops sum’s
Conflicting ops reads © = commit
either both before wA or both after wB
20
Linearizability vs. Serializability
• Linearizability: a guarantee • Serializability is a
about single operations on guarantee about
single objects transactions over one
• Once write completes, all or more objects
reads that begin later should • Doesn’t impose real-
reflect that write time constraints
21
Consistency Hierarchy
Strict Serializability e.g., Spanner
Sequential Consistency
CAP
PRAM 1988
(Princeton)
Causal+ Consistency e.g., Bayou
2. Serializability
• Strict serializability
3. Concurrency Control:
• Two-phase locking (2PL)
• Optimistic concurrency control (OCC)
Concurrency Control
• Concurrent execution can violate serializability
• No concurrency at all
• Terrible for performance: one transaction at a time
25
Locking
• Locks maintained on each shard
• Transaction requests lock for a data item
• Shard grants or denies lock
• Lock types
• Shared: Need to have before read object
• Exclusive: Need to have before write object
transfer: ◢A rA wA ◣A ◢B rB wB ◣B ©
sum: ◿A rA ◺A ◿B rB ◺B ©
27
Two-Phase Locking (2PL)
• 2PL rule: Once a transaction has released a lock
it is not allowed to obtain any other locks
• Growing phase: transaction acquires locks
• Shrinking phase: transaction releases locks
• In practice:
• Growing phase is the entire transaction
• Shrinking phase is during commit
28
2PL Provide Strict Serializability
• 2PL rule: Once a transaction has released a lock it is
not allowed to obtain any other locks
transfer: ◢A rA wA ◣A ◢B rB wB ◣B ©
sum: ◿A rA ◺A ◿B rB ◺B ©
transfer: ◿A rA ◢A wA ◿B rB ◢B wB✻©
sum: ◿A rA ◿B rB✻©
30
2PL Doesn’t Exploit All Opportunities
For Concurrency
• 2PL rule: Once a transaction has released a lock it is
not allowed to obtain any other locks
transfer: rA wA rB wB ©
sum: rA rB ©
Time à
© = commit
(locking not shown)
31
Issues with 2PL
• What do we do if a lock is unavailable?
• Give up immediately?
• Wait forever?
32
Lets Scale Strong Consistency!
1. Atomic Commit
• Two-phase commit (2PC)
2. Serializability
• Strict serializability
3. Concurrency Control:
• Two-phase locking (2PL)
• Optimistic concurrency control (OCC)
2PL is Pessimistic
• Acquires locks to prevent all possible violations
of serializability
35
2PL vs OCC
Conflict Rate
37
OCC Uses 2PC
• Validation and Commit use Two-Phase Commit
2. Serializability
• Strict serializability
3. Concurrency Control:
• Two-phase locking (2PL)
• Optimistic concurrency control (OCC)
• Uses 2PC