Distributed Commit Protocols
Updated on 2021-04-18Properties of transactions: Atomic, Consistent, Isolated, Durable.
Distributed transaction needs unanimous agreement: only if every participant agrees, then they must all commit, otherwise every participant must abort.
Two phase commit protocol
The 2PC has one coordinator, and every participant keeps write-ahead log in stable storage. We assume systems recover from failure (systems discover their pre-death state from the WAL), and messages always arrive.
Phase 1
the coordinator writes a “prepare to commit” to its log
the coodinator sends “can commit?” messages to all participants
participants receives “can commit?” messages; when ready, each participant writes “agree to commit” or “abort” to the log, then sends the same message to the coodinator
Phase 2
once messages from all the participants are received, the coordinator writes “commit” or “abort” to its log and sends messages to participants
participants receive “commit” or “abort”, write to the log, commit or abort, then send “done” to the coodinator
the cordinator receives “done” from all participants and done
2PC is a blocking protocol, requiring all participants to recover from failure eventually.
Three phase commit protocol
3PC is an extension of 2PC that avoids blocking problems. It adds timeouts that result in abort to each phase in 2PC, and introduces an extra step in the 2nd phase of 2PC:
the coordinator sends “prepare” messages to all participants, when it receives “agree to commit” from all participants in Phase 1
participants send back acknowledgement, but don’t commit yet
if the coordinator receives acknowledgements from all, it sends “commit” to all. Otherwise it aborts and sends “abort” to all.
By informing participants about the intension to commit, in case that the cordinator dies, a new coordinator can ask the status of the protocol from other participants. 3PC is not resilient against network partions.
Consensus-based commit
Run a consensus algorithm on the commit/abort decision of each participant.
Eventual consistency
If no updates are made to a data item, eventually all accesses to that item will return the last updated value.