
Two databases must either both reserve funds or both abort. A coordinator asks each participant to prepare, then tells every participant to commit only after all have voted yes. Two-phase commit (2PC) provides atomic commitment, but participants can hold locks and block when the coordinator or network fails.
This guide walks through prepare and commit, durable decisions, coordinator failure, blocking, timeouts, and alternatives. Continue from distributed transactions, Saga pattern, and database isolation levels.
Table of Contents
Open Table of Contents
The Two Phases
flowchart TD
A[Coordinator begins transaction] --> B[Prepare request]
B --> C[Participant A prepares and logs vote]
B --> D[Participant B prepares and logs vote]
C --> E{All votes yes?}
D --> E
E -->|Yes| F[Commit decision logged]
E -->|No or timeout| G[Abort decision logged]
F --> H[Commit messages]
G --> I[Abort messages]
H --> J[Participants release locks]
I --> J
During prepare, each participant validates the operation, writes enough durable state to recover, and holds the required locks or reservations. A “yes” is a promise that it can commit later. During commit, the coordinator broadcasts one global decision, and participants apply it and release resources.
Failure and Blocking
If a participant votes no, the coordinator can abort. The difficult case is a participant that voted yes but loses contact before learning the decision. It cannot safely commit or abort on its own, so it waits for the coordinator or recovery protocol while holding resources.
Coordinator failure after logging commit but before notifying every participant creates uncertainty. Participants need a durable coordinator log, replicated coordinator, or recovery query. A timeout alone cannot tell whether the global decision was commit or abort; choosing incorrectly breaks atomicity.
Coordinator Recovery
The coordinator writes its decision before sending phase-two messages. On restart, it reads the log and resends the same decision idempotently. Participants persist their prepared and completed state and answer status queries. Recovery must be deterministic: the same transaction ID always maps to one global decision.
If the coordinator log is unavailable, a participant may remain blocked. This is the central availability cost of classic 2PC. Some systems layer consensus around the coordinator, but the result still needs durable quorum and careful participant recovery.
Operational Trade-offs
2PC can provide strong atomicity across compatible resource managers, but it increases latency, lock duration, coordinator complexity, and failure coupling. Long transactions hold database resources while a slow participant or network waits. It is a poor fit for public APIs and long business workflows where a Saga can expose intermediate state and compensate.
Use timeouts to detect unhealthy work, not to invent a global outcome. Monitor prepared transaction age, coordinator decisions, participant votes, lock wait, abort rate, and in-doubt recovery. An old prepared transaction is both a correctness and capacity incident.
When to Choose 2PC
Use 2PC when participants support the protocol, the transaction is short, strong atomicity is non-negotiable, and the team can operate the coordinator and recovery path. Prefer a single database transaction, a distributed database with built-in consensus, an outbox, or a Saga when service boundaries, availability, and long-running work dominate.
Interview Questions
1. What happens in the prepare phase?
The coordinator asks every participant whether it can commit. Each participant validates, records a durable prepared state, holds required resources, and votes yes or no. A yes is a promise to commit if the final decision is commit.
2. Why can 2PC block?
After voting yes, a participant cannot decide independently without knowing the coordinator’s global decision. If the coordinator or network fails, it may hold locks while waiting for recovery. This uncertainty is the protocol’s availability cost.
3. Why must the coordinator log the decision first?
If the coordinator crashes after sending some commit messages but before persisting the decision, recovery might choose a different result and split participants. A durable decision log lets it resend the same outcome after restart.
4. Is a timeout enough to abort 2PC?
No. A timeout detects that progress stopped but cannot prove whether the coordinator committed. Aborting unilaterally after a participant voted yes can violate atomicity. Recovery must consult the durable coordinator decision or an agreed protocol.
5. Why are Sagas often preferred in microservices?
Sagas avoid holding cross-service locks and allow each service to commit locally. They trade atomic isolation for eventual consistency and compensating actions. That trade-off fits many business workflows but requires idempotency, durable state, and reconciliation.
Conclusion
Two-phase commit coordinates an all-or-nothing decision through durable prepare and commit phases. Its strength is atomicity; its weakness is blocking, lock duration, and coordinator recovery. Use it deliberately for short compatible transactions, and prefer local transactions plus outbox or Saga patterns for resilient service workflows.
References
- Microsoft: Two-phase commit pattern
- Baeldung: Two-phase commit vs Saga
- Gray and Lamport: Consensus on Transaction Commit
YouTube Videos
- “Distributed Transactions Explained: 2 Phase Commit vs Saga Pattern” - Hello Interview [https://www.youtube.com/watch?v=DOFflggE_0Q]