Patterns
35 items
35 items
Distributed transaction coordination
Two-Phase Commit (2PC) is a protocol for achieving atomic transactions across multiple database nodes or services. Phase 1 (Prepare): Coordinator asks all participants if they can commit. Phase 2 (Commit/Abort): If all say yes, coordinator tells all to commit; if any says no, all abort. 2PC ensures all-or-nothing: either all participants commit or none do. However, 2PC has significant drawbacks: blocking during coordinator failure, increased latency, and scalability limits. Modern systems often prefer Saga pattern or eventual consistency.
Either all participants commit the transaction or none do. Guarantees no partial failures. Essential when data must be consistent across systems.
If coordinator fails after sending prepare but before commit/abort, participants are stuck holding locks. Cannot proceed or rollback safely. This is 2PC's biggest weakness.
Minimum 4 network hops: prepare request, prepare response, commit request, commit response. Plus time for logging. Significantly slower than local transactions.
Phase 1 - Prepare: 1. Coordinator sends PREPARE to all participants 2. Participants execute transaction, acquire locks 3. Participants log PREPARE record (must survive crash) 4. Participants vote YES (can commit) or NO (must abort)
Phase 2 - Commit/Abort: - If all vote YES: Coordinator sends COMMIT - If any vote NO: Coordinator sends ABORT - Participants commit/abort and release locks
When participant votes yes in prepare phase, it promises to commit if told. Must durably log this promise. Cannot unilaterally abort after yes vote.
3PC adds pre-commit phase to reduce blocking window. More complex. Still not partition-tolerant. Rarely used in practice.
Phase 1 - Prepare: 1. Coordinator sends PREPARE to all participants 2. Participants execute transaction, acquire locks 3. Participants log PREPARE record (must survive crash) 4. Participants vote YES (can commit) or NO (must abort)
Phase 2 - Commit/Abort: - If all vote YES: Coordinator sends COMMIT - If any vote NO: Coordinator sends ABORT - Participants commit/abort and release locks
| Aspect | Advantage | Disadvantage |
|---|---|---|