SystemExpertsSystemExperts
Pricing

Patterns

35 items

Horizontal Scaling Pattern

15mbeginner

Retry with Backoff Pattern

15mbeginner

Replication Pattern

25mintermediate

Caching Strategies Pattern

25mintermediate

Persistent Connections Pattern

20mintermediate

Load Balancing Pattern

20mintermediate

Fan-out Pattern

20mintermediate

Fan-in Pattern

20mintermediate

Circuit Breaker Pattern

20mintermediate

Eventual Consistency Pattern

25mintermediate

Queue-based Load Leveling Pattern

20mintermediate

Bloom Filters Pattern

20mintermediate

Time-Series Storage Pattern

20mintermediate

Bulkhead Pattern

20mintermediate

Batch Processing Pattern

20mintermediate

Write-Ahead Log Pattern

20mintermediate

API Gateway Pattern

20mintermediate

Backend for Frontend Pattern

20mintermediate

Sidecar Pattern

20mintermediate

Idempotency Pattern

20mintermediate

Rate Limiting Pattern

20mintermediate

Backpressure Pattern

20mintermediate

Pub/Sub Pattern

25mintermediate

Strong Consistency Pattern

30madvanced

Conflict Resolution Pattern

25madvanced

Leader Election Pattern

25madvanced

Consensus Protocols Pattern

30madvanced

CQRS Pattern

28madvanced

LSM Trees Pattern

25madvanced

Sharding Pattern

25madvanced

Event Sourcing Pattern

30madvanced

Stream Processing Pattern

25madvanced

Change Data Capture Pattern

25madvanced

Distributed Locking Pattern

25madvanced

Two-Phase Commit Pattern

25madvanced
System Design Pattern
Coordination2pctwo-phase-commitdistributed-transactionscoordinatoratomicityadvanced

Two-Phase Commit Pattern

Distributed transaction coordination

Used in: Database Transactions, Saga Pattern, XA Transactions|25 min read

Summary

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.

Key Takeaways

All-or-Nothing Atomicity

Either all participants commit the transaction or none do. Guarantees no partial failures. Essential when data must be consistent across systems.

Blocking Protocol

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.

Latency Penalty

Minimum 4 network hops: prepare request, prepare response, commit request, commit response. Plus time for logging. Significantly slower than local transactions.

2PC Success Flow

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

Summary

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.

Key Takeaways

All-or-Nothing Atomicity

Either all participants commit the transaction or none do. Guarantees no partial failures. Essential when data must be consistent across systems.

Blocking Protocol

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.

Latency Penalty

Minimum 4 network hops: prepare request, prepare response, commit request, commit response. Plus time for logging. Significantly slower than local transactions.

Coordinator is Single Point

Coordinator failure blocks entire transaction. Need coordinator HA (often itself using consensus). Adds complexity and failure modes.

Prepare Means Promise

Premium Content

Sign in to access this content or upgrade for full access.

When participant votes yes in prepare phase, it promises to commit if told. Must durably log this promise. Cannot unilaterally abort after yes vote.

Three-Phase Commit Addresses Some Issues

3PC adds pre-commit phase to reduce blocking window. More complex. Still not partition-tolerant. Rarely used in practice.

Pattern Details

2PC Success Flow

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

Trade-offs

AspectAdvantageDisadvantage