Architectural Breakdown & AnalysisFAANG Aligned
When a business transaction spans service boundaries (e.g., Order Service → Inventory Service → Payment Service), local DB transactions cannot guarantee atomicity.
1. Two-Phase Commit (2PC)
- Prepare Phase: Coordinator asks participant DBs if they can commit. Participants lock records and reply Yes/No.
- Commit Phase: If all reply Yes, coordinator issues Commit; otherwise Abort.
- Drawback: 2PC is a blocking protocol. If the coordinator fails mid-process, resources remain locked indefinitely.
2. Saga Pattern (Eventual Consistency)
Executes a sequence of independent local transactions. Each service commits locally and publishes an event.
- Compensating Transactions: If a step fails (e.g., Payment declined), the Saga controller executes reverse compensating actions (e.g., release reserved inventory).
- Choreography: Decentralized services listen to event queues. Simple, but hard to trace.
- Orchestration: Central Saga coordinator manages flow and failure recovery explicitly.
TIP — Production Recommendation
Default to Saga Orchestration for distributed microservices. Avoid 2PC in high-throughput cloud environments due to network latency, lock contention, and SPOF risks.
Key Architectural Comparison
| Distributed Transaction Pattern | Coordination Style | Locking Duration | Network Partition Tolerance | Failure Recovery |
|---|---|---|---|---|
| Two-Phase Commit (2PC) | Centralized Coordinator. | Heavy (locks database rows across all phases). | Poor (coordinator failure locks entire cluster). | Rollback transaction. |
| Saga (Choreography) | Decentralized (Kafka events). | None (each service commits local ACID transaction). | High (services process events asynchronously). | Compensating transactions. |
| Saga (Orchestration) | Centralized Workflow Engine. | None (local transactions orchestrated sequentially). | High (orchestrator handles timeouts & retries). | Compensating transactions (Temporal/Cadence). |