Concurrency Control
Transactions that run at several sites must remain globally serializable. Approaches:
- Distributed two-phase locking: a single lock manager at one site, primary copy locking for each item, or distributed lock managers (each site locks its own data). With replicas, read-one, write-all or majority protocols decide which copies to lock.
- Timestamp ordering with globally unique timestamps (site id attached).
- Deadlock handling: each site keeps a local wait-for graph (WFG). A deadlock between sites does not appear in any local graph, so the graphs are merged into a global WFG (centrally or by passing edges), and a cycle means a deadlock. Prevention uses timestamps (wait-die, wound-wait) and timeouts.
Commit Protocols
A distributed transaction must commit at all sites or at none (atomicity). The two-phase commit (2PC) protocol has a coordinator and participants:
- Phase 1 (voting): the coordinator sends PREPARE; each participant forces its log to disk and replies YES (ready) or NO (abort).
- Phase 2 (decision): if all vote YES the coordinator writes COMMIT and sends COMMIT; if any votes NO (or times out) it sends ABORT. Participants act and send an ACK.
With n participants a successful run needs 4n messages. 2PC can block if the coordinator fails after participants have voted; three-phase commit (3PC) adds a pre-commit phase to avoid blocking.
Recovery
Every site keeps a log. After a crash a site uses its log to redo committed work and undo uncommitted work; a participant in the ready state asks the coordinator or the others for the outcome. Replication and quorums keep data available when a site or the network fails.
Definition: Two-phase commit. A protocol in which a coordinator first asks all participants to prepare and vote, and then tells all of them to commit if every vote is yes and to abort otherwise.
Table 3.5: Commit protocols
| Protocol | Phases | Remark |
|---|---|---|
| 2PC | Voting, decision | Can block if coordinator fails |
| 3PC | Voting, pre-commit, decision | Avoids blocking, more messages |
Example 3.5: Two-phase commit and a global deadlock
Problem: A transaction has 3 participants. In one run they vote YES, YES, YES and in another YES, NO, YES. What does the coordinator decide and how many messages does the committing run need? Also, T1 waits for T2 at site 1 and T2 waits for T1 at site 2: is there a deadlock?
Solution:
- All votes are YES, so the coordinator decides COMMIT; with the votes YES, NO, YES it decides ABORT.
- The committing run needs PREPARE (3), votes (3), COMMIT (3) and ACK (3) = 12 messages = 4n.
- Site 1 has the edge T1 → T2 and site 2 has the edge T2 → T1; neither local graph has a cycle.
- The global graph has the cycle T1 → T2 → T1.
Result: First run COMMIT, second run ABORT, 12 messages for the commit; the global deadlock is only visible in the merged graph.
def two_pc(votes):
return "COMMIT" if all(v == "YES" for v in votes) else "ABORT"
print("Votes YES, YES, YES ->", two_pc(["YES", "YES", "YES"]))
print("Votes YES, NO, YES ->", two_pc(["YES", "NO", "YES"]))
print("Messages for a commit with 3 participants:", 4 * 3)
site1 = {("T1", "T2")}
site2 = {("T2", "T1")}
def has_cycle(edges):
return any((b, a) in edges for a, b in edges)
print("Site 1 cycle:", has_cycle(site1))
print("Site 2 cycle:", has_cycle(site2))
print("Global cycle:", has_cycle(site1 | site2))
Output
Votes YES, YES, YES -> COMMIT
Votes YES, NO, YES -> ABORT
Messages for a commit with 3 participants: 12
Site 1 cycle: False
Site 2 cycle: False
Global cycle: True
Common mistakes
- Looking only at local wait-for graphs and missing a global deadlock.
- Letting a participant commit before the coordinator's decision.
- Forgetting to force the log to disk before replying YES.
- Assuming that 2PC never blocks.
Key takeaways
- 2PC: voting phase, then decision phase; 4n messages for n participants.
- A global wait-for graph is needed to find distributed deadlocks.
- Replica control uses read-one write-all or majority protocols.
- Logs allow redo and undo after a site crash.
Practice questions
- When does the coordinator commit in 2PC?
- Why is a global wait-for graph needed?
- What problem does 3PC solve?
Answers to the practice questions
- When all participants have voted YES.
- A deadlock across sites may not show up as a cycle in any single local graph.
- The blocking of 2PC when the coordinator fails.
Sources
- Official BCA syllabus Advanced Database Systems, course code 25BCA406DS04, Unit III
