Distributed Transactions: 2PC vs Sagas
Understand atomic multi-service state transitions, analyze the blocking risks of Two-Phase Commit, and design resilient event-driven Sagas.
In a monolithic architecture with a single relational database, maintaining transactional integrity is simple. You execute BEGIN TRANSACTION, modify five different tables, and execute COMMIT. The database guarantees that either all mutations take effect or none do.
In a modern microservices architecture, this simplicity disappears. Following the Database-per-Service pattern, the Order Service has its own database, the Payment Service has its own database, and the Inventory Service has its own database.
Executing an order checkout requires atomic state transitions across three separate physical databases over the network. How do you guarantee consistency when a service crashes halfway through the workflow?
Two distinct paradigms solve this problem: the classic Two-Phase Commit (2PC) protocol and the modern Saga Pattern.
01The Classic Two-Phase Commit (2PC) Protocol
Two-Phase Commit is a synchronous distributed consensus protocol designed to ensure that multiple participating databases agree to commit a transaction atomically.
The protocol relies on a central Transaction Coordinator and multiple participating resource managers.
Phase 1: The Prepare Phase
- The client asks the Coordinator to begin a distributed transaction.
- The Coordinator executes the business logic across the participants.
- The Coordinator sends a
PREPAREmessage to all participants: "Can you commit this transaction?" - Each participant checks constraints, acquires necessary exclusive database row locks, writes changes to its local transaction log (WAL), and votes:
VOTE_COMMIT: "I am ready and have locked the rows."VOTE_ABORT: "I encountered a conflict or constraint failure."
Phase 2: The Commit / Abort Phase
- The Coordinator evaluates all returned votes:
- If all participants voted
VOTE_COMMIT, the Coordinator writes aCOMMITdecision to its persistent log and broadcasts aGLOBAL_COMMITmessage. - If even one participant voted
VOTE_ABORT(or timed out), the Coordinator broadcasts aGLOBAL_ABORTmessage.
- If all participants voted
- Each participant receives the decision, applies or rolls back the changes, releases its row locks, and sends an acknowledgment back to the Coordinator.
The Fatal Flaw of 2PC: Blocking Locks
While mathematically sound, Two-Phase Commit is an anti-pattern for modern cloud microservices due to its blocking nature.
What happens if the Coordinator crashes immediately after sending PREPARE messages?
- All participating databases have voted
VOTE_COMMITand are holding exclusive row locks on their local tables. - Because a participant cannot know whether the Coordinator decided to commit or abort, it must remain locked indefinitely until the Coordinator recovers and replays its log.
- Any other incoming requests that touch those same rows will block, leading to catastrophic thread pool exhaustion and cascading system failure across the entire enterprise.
Furthermore, 2PC latency is bounded by the slowest network round trip across all participants, reducing system throughput to a crawl.
Loading the rest of the lesson…