Consensus through prepare and commit phases
Two-phase commit (2PC) coordinates atomic changes across multiple independent systems. The coordinator sends a prepare request to every participant: can you commit this change? Each participant locks resources and reports yes or no. If all say yes, the coordinator sends commit and they apply the changes. If any say no, the coordinator sends abort and all roll back. This guarantees that either all systems commit or none do, even if failures occur between phases.
The blocking and failure problem
2PC has a fundamental weakness: if the coordinator crashes after prepare but before commit, participants remain locked indefinitely, unable to unilaterally decide to commit or abort. They must wait for the coordinator to recover. This blocks resources and transactions, degrading availability. Long-running transactions with 2PC can lock critical data for seconds or minutes, starving other requests. Most distributed systems avoid 2PC when possible.
Modern alternatives
Saga patterns break a distributed transaction into a series of local transactions with compensating actions for rollback. Event sourcing records every state change, enabling replaying to rebuild state. Both avoid global blocking. 2PC remains useful within a single database cluster or for occasional critical operations where availability loss is acceptable, but microservices architectures have largely moved away from it.