Three-Phase Commit Protocol (3PC) in Distributed Systems
Knowledge Point Description
The Three-Phase Commit Protocol (3PC) is a consensus protocol in distributed transaction processing, used to coordinate the commitment or abortion of a transaction among multiple participating nodes. It is an improvement over the Two-Phase Commit Protocol (2PC), primarily addressing the blocking issue and single point of failure risk in 2PC. By introducing a "Pre-Commit" phase, 3PC divides the commit process into three stages, reducing system uncertainty in case of coordinator failure.
Step-by-Step Explanation of the Process
Stage 1: CanCommit (Inquiry Phase)
- Coordinator sends CanCommit request: The transaction coordinator sends a CanCommit message to all participants, asking if they are ready to commit the transaction.
- Participants perform feasibility check: Each participant checks its own state:
- If local resources are sufficient and there are no conflicts, it returns a YES response.
- If resources are insufficient or conflicts exist, it returns a NO response.
- Coordinator collects responses:
- If all participants return YES, proceed to the next stage.
- If any participant returns NO or times out, the coordinator sends an Abort message to abort the transaction.
Purpose of this stage: Perform a lightweight check before formally locking resources to avoid premature resource occupation.
Stage 2: PreCommit (Pre-Commit Phase)
- Coordinator sends PreCommit request: When all participants agree, the coordinator sends a PreCommit message to all participants.
- Participants execute pre-commit operations:
- Participants write redo and undo logs to ensure fault recovery capability.
- Lock relevant resources, but do not formally commit yet.
- After completing the pre-commit, send an Ack response to the coordinator.
- Coordinator confirms pre-commit completion: After receiving Acks from all participants, proceed to the final commit stage.
Key Improvement: The PreCommit stage establishes an "intermediate state," providing a clear recovery path for the system in case of coordinator failure.
Stage 3: DoCommit (Execution Commit Phase)
- Coordinator sends DoCommit request: The coordinator sends a DoCommit message to all participants.
- Participants execute final commit:
- Participants formally commit the transaction and release locked resources.
- Send a HaveCommitted response to the coordinator.
- Transaction completion: After receiving all responses, the coordinator marks the transaction as complete.
Fault Handling Mechanism:
- If a participant times out after PreCommit, it automatically commits the transaction (since it has already received PreCommit).
- This "timeout auto-commit" mechanism solves the blocking problem in 2PC.
Key Differences Between 3PC and 2PC
- Timeout mechanism: 3PC sets timeouts for all stages, allowing participants to automatically proceed or commit after a timeout.
- Pre-Commit phase: Introduces a clear intermediate state, reducing uncertainty.
- Fault recovery: After a new coordinator is elected, it can explicitly decide to commit or abort based on the participants' states.
Limitations of 3PC
Although 3PC solves the blocking problem in 2PC, it still has the following limitations:
- Inconsistency may still occur in the case of network partitions.
- Implementation complexity is higher than that of 2PC.
- Requires more message exchanges, resulting in greater performance overhead.
Through the detailed division of these three stages, 3PC significantly improves the availability and fault tolerance of distributed systems while maintaining the ACID properties of transactions.