Back to 20 Concepts
distributed-dataExpert

Distributed Transactions: 2-Phase Commit (2PC) vs Saga Pattern

2-Phase Commit (Prepare -> Commit) guarantees ACID consistency across microservices but suffers from blocking coordinator bottlenecks. The Saga Pattern uses asynchronous choreography or orchestration with compensating transactions.

Intuitive Mental Model

The Wedding Booking Saga: You reserve the flight, book the hotel, and rent the car. If the car rental fails, the Saga triggers compensating actions: cancel hotel booking and refund flight ticket.

Architecture Blueprint & CodeProduction Standard
// Saga Orchestrator Pattern:
// 1. OrderService: Create Pending Order -> Success
// 2. PaymentService: Charge Credit Card -> Success
// 3. InventoryService: Reserve Items -> FAILED!
// 4. Compensating Action: PaymentService.refund() -> OrderService.cancel()

Key Architectural Takeaways

  • 2PC Blocking Problem: If the coordinator crashes during the Commit phase, all participant databases remain locked indefinitely.
  • Sagas Tradeoff: Eventual consistency with compensation rather than synchronous distributed ACID locks.
Common Architectural Pitfall

Attempting to use distributed 2PC locking across dozens of independent microservices.

Production Best Practice

Use Saga Orchestrators with idempotent compensating transactions.