Question #8 of 60•Microservices•Hard• ~3 min read

How do you achieve consistency across multiple microservices (Sagas vs. 2PC)?

Executive Takeaway

Understand the blocking nature of Two-Phase Commit (2PC) and how the Saga Pattern orchestrates distributed, eventual consistency transactions.

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 PatternCoordination StyleLocking DurationNetwork Partition ToleranceFailure 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).
Previous QuestionWhich database should you choose for User Data, Transactions, Logs, and Analytics?Next Question How does Consistent Hashing minimize data movement during node scaling?

Continue Learning on Being SDE

Explore deep-dive system architecture modules, low-level object-oriented designs, and interview cheat sheets.