Distributed Two-Phase Commit
There are two phases for the commit procedure to work:
Phase 1: Voting
- A “prepare message” is sent to each participating worker by the coordinator.
- The coordinator must wait until a response whether ready or not ready is received from each worker, or a timeout occurs.
- Workers must wait until the coordinator sends the “prepare” message.
- If a transaction is ready to commit then a “ready” message is sent to the coordinator.
- If a transaction is not ready to commit then a “no” message is sent to the coordinator and resulting in aborting of the transaction.
Phase 2: Completion of the voting result
- In this phase, the Coordinator will check about the “ready” message. If each worker sent a “ready” message then only a “commit” message is sent to each worker; otherwise, send an “abort” message to each worker.
- Now, wait for acknowledgment until it is received from each worker.
- In this phase, Workers wait until the coordinator sends a “commit” or “abort” message; then act according to the message received.
- At last, Workers send an acknowledgment to the Coordinator.
Atomic Commit Protocol in Distributed System
In distributed systems, transactional consistency is guaranteed by the Atomic Commit Protocol. It coordinates two phases—voting and decision—to ensure that a transaction is either fully committed or completely canceled on several nodes.