ProLesson 7.2Intermediate5 min readUpdated 31 October 2026
The Saga Pattern: Choreography vs Orchestration
Coordinate distributed transactions across microservices without blocking Two-Phase Commit using compensating transactions and Saga state machines.
In a monolithic application, maintaining data consistency across multiple database tables is trivial: wrap the business logic in @Transactional, and the relational database handles rollback automatically if an error occurs.
In a microservices architecture with a Database-per-Service pattern, an order checkout spans three independent databases:
Order DB(Stores order status)Payment DB(Stores customer charges)Inventory DB(Stores warehouse stock)
How do you guarantee that either all three succeed or all three roll back?
01Why Two-Phase Commit (2PC) Fails in Microservices
Decades ago, enterprise systems used Two-Phase Commit (2PC) coordinated by an XA transaction manager:
- Phase 1 (Prepare): The coordinator contacts all participant databases and asks: "Can you commit this transaction?" Each database reserves local row locks and votes YES or NO.
- Phase 2 (Commit): If all voted YES, the coordinator commands all databases to COMMIT.
The Fatal Flaws of 2PC in Modern Cloud
- Synchronous Blocking: Row locks are held across remote network boundaries during both phases. If the network hiccups, database locks remain held indefinitely, bringing all other transactions to a halt.
- Coordinator as Single Point of Failure: If the transaction coordinator crashes during Phase 2, databases remain frozen waiting for commit instructions.
- Incompatible with Modern NoSQL / Message Queues: Databases like MongoDB, Cassandra, and Kafka do not support XA/2PC protocols.
- Poor Availability: By the CAP theorem, 2PC is a strict CP protocol; if any single participant goes down, the entire system cannot commit.
Loading the rest of the lesson…