NOTE
2.9 Two-Phase Implementation: 2PC
Two-phase commit first sends prepare requests to participating databases, then uses their responses to decide whether to commit or roll back.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is 2PC?
- 2PC: Two-Phase Commit.
- Divide the transaction into two phases:
- In the first phase, the transaction manager sends prepare requests to all databases.
- If all databases report success, commit in the second phase; if any reports failure, roll back in the second phase.

2. Problems with 2PC
- Single point of failure: if the transaction manager or any resource manager goes down, the entire transaction cannot execute.
- Synchronous blocking: each database starts locking resources in the first phase and does not release the locked resources until the second phase, remaining blocked the whole time.
3. Implementations of 2PC
3.1. XA
- The DTP model defines several roles:
- AP: our microservice.
- TM: global transaction manager.
- RM: database.
- CRM: communication middleware between TM and RM.
- A distributed transaction can be split into many local transactions running on different APs and RMs.
- The ACID properties of each local transaction are easy to implement, but the global transaction must ensure that all of its local transactions succeed together. If one local transaction fails, all other transactions must be rolled back. The problem is that while a local transaction is being processed, it does not know the running state of the other transactions. Therefore, CRM is needed to notify each local transaction and synchronize transaction-execution status.
- Therefore, communication among local transactions must follow a unified standard, otherwise different databases cannot communicate. XA is the interface specification for communication between TM and RM.
4. References
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub