Posted in Cloud & Infrastructure, Engineering

Two-Phase Commit vs Saga Pattern: Distributed Transaction Patterns Explained

Once an application splits into multiple services, each with its own database, a single business operation can no longer rely on one ACID transaction to keep everything consistent. An order might need to debit a wallet service, reserve stock in an inventory service, and schedule a shipment in a logistics service — three separate databases, no shared transaction log. Distributed systems engineers have two long-standing answers to this problem: the Two-Phase Commit (2PC) protocol, which tries to make a distributed transaction behave like a local one, and the Saga pattern, which gives up strict atomicity in exchange for availability and loose coupling. This article explains how each works, where each breaks down, and when to reach for one over the other.

AT A GLANCE

DimensionTwo-Phase Commit (2PC)Saga Pattern
Consistency modelStrong / atomic (all-or-nothing)Eventual consistency via compensation
CoordinatorSingle transaction coordinator, synchronousOrchestrator (optional) or choreography via events
Failure handlingBlocking — participants wait, holding locks, if coordinator fails after prepareNon-blocking — compensating transactions semantically undo prior steps
IsolationFull isolation while transaction is in-flight (locks held)No built-in isolation; anomalies possible, countermeasures needed
Best fitTightly coupled systems, short-lived transactions, same trust/network domain (e.g. XA-compliant databases)Microservices, long-running business workflows, cross-service transactions
Typical techXA transactions, JTA, some message brokersEvent-driven messaging, Step Functions, Temporal, Kafka + outbox

Short Verdict

Two-Phase Commit gives you a real atomic transaction across multiple resources, but it does so by blocking and holding locks until every participant agrees — a design that doesn’t survive the availability and decoupling goals of modern microservices. The Saga pattern accepts eventual consistency and pushes the complexity of “what if a step fails” onto explicit compensating transactions, but it keeps services independently deployable and available. For most microservice architectures built since the mid-2010s, Sagas have won; 2PC survives mainly inside single-vendor database clusters and legacy enterprise middleware.

How Two-Phase Commit Works

2PC coordinates a transaction across multiple participants (resource managers) through a dedicated transaction coordinator, in two rounds:

  • Phase 1 — Prepare/Vote: The coordinator asks every participant to prepare the transaction. Each participant does whatever work is needed to guarantee it can commit — writing to its own log, acquiring locks — then replies “yes” (ready) or “no” (abort).
  • Phase 2 — Commit/Abort: If every participant voted yes, the coordinator logs the decision and tells everyone to commit. If any participant voted no, or timed out, the coordinator tells everyone to abort.

Once a participant votes “ready” in phase 1, it has given up the right to unilaterally abort — it must wait for the coordinator’s final word. That’s precisely what makes 2PC powerful (every participant ends up in the same state) and precisely what makes it fragile: if the coordinator crashes after collecting votes but before broadcasting the decision, every participant that voted “ready” is stuck holding its locks, unable to safely commit or abort on its own, until the coordinator recovers. This is the classic blocking problem of 2PC, and it’s the reason participants keep other transactions waiting on the same rows for however long the coordinator is down. Three-Phase Commit (3PC) adds a timeout-driven pre-commit phase to reduce blocking, but it can’t fully eliminate inconsistency risk during network partitions, which is why most distributed databases now favor leader-election via Paxos or Raft for coordinator availability instead.

How the Saga Pattern Works

A Saga, as defined by Chris Richardson’s microservices.io pattern catalog, implements a business transaction that spans several services as “a reliably coordinated sequence of local transactions.” Each service commits its own local transaction and publishes an outcome; there is no cross-service lock and no global coordinator holding resources hostage. If a step fails partway through, the saga doesn’t roll back in the database sense — it runs compensating transactions that semantically undo the completed steps (for example, refunding a payment or releasing reserved inventory), which developers must design explicitly because sagas get none of the automatic rollback an ACID transaction provides.

There are two coordination styles:

  • Choreography: No central coordinator. Each service’s local transaction publishes an event; other services subscribe and react, triggering their own local transactions. This keeps services decoupled but makes the overall flow harder to trace as the number of steps grows.
  • Orchestration: A saga orchestrator explicitly tells each participant which local transaction to run, and decides whether to continue or trigger compensations based on the replies. AWS’s own prescriptive guidance recommends orchestration (for example, with AWS Step Functions) for sagas with more than a handful of steps, because it’s easier to visualize, test, and monitor than a web of choreographed events.

Because the saga’s steps complete asynchronously, the overall business transaction is only eventually consistent — a client typically learns the final outcome through a callback, a status endpoint it polls, or a notification, not an immediate response. Sagas also provide no isolation: another process can read intermediate, partially-applied state while the saga is still running, so designers need explicit countermeasures (e.g., semantic locks, commutative updates, or “pessimistic view” patterns) to avoid anomalies.

Pros and Cons

Two-Phase Commit — Pros

  • True atomicity: every participant ends up in the same committed or aborted state.
  • No application-level compensation logic to design or test.
  • Well-supported by XA-compliant databases and transaction managers (JTA/JTS).

Two-Phase Commit — Cons

  • Blocking: a crashed coordinator can leave participants holding locks indefinitely.
  • Poor fit across service/network/trust boundaries — assumes all participants are reachable and well-behaved.
  • Reduces availability and throughput under contention since locks are held across the whole round trip.
  • Rarely supported across heterogeneous message brokers and modern cloud-native databases.

Saga Pattern — Pros

  • No distributed locks; services stay available and independently deployable.
  • Scales naturally with asynchronous, event-driven architectures.
  • Failure handling (compensation) is explicit and testable business logic rather than infrastructure magic.

Saga Pattern — Cons

  • Only eventual consistency — clients can’t assume the operation is “done” immediately.
  • No built-in isolation; concurrent sagas can expose dirty reads unless you add countermeasures.
  • Compensating transactions must be idempotent and may not perfectly reverse side effects (e.g., an email already sent can’t be unsent).
  • Requires reliable messaging patterns (Transactional Outbox, event sourcing) so a service’s DB write and event publish don’t drift out of sync.

Who Each Is a Good Fit For

Two-Phase Commit still makes sense inside a single trust domain with low-latency, highly reliable links — for example, coordinating writes across shards of the same distributed database product, or XA transactions spanning an application server and a database in a traditional monolith. The Saga pattern is the default choice for microservice architectures where services own separate databases, deploy independently, and must stay available even when one downstream dependency is degraded — order processing, booking and travel reservations, and payment workflows are the textbook examples cited by both Chris Richardson and AWS’s own architecture guidance.

Bottom Line

2PC trades availability for atomicity; Sagas trade atomicity (and isolation) for availability and loose coupling. Neither is “wrong” — they solve different problems. If you’re building microservices with independently owned data stores, start by assuming you’ll need a Saga and design your compensations up front; reach for 2PC only when every participant lives in the same tightly controlled, low-latency domain and you genuinely need all-or-nothing semantics.

FAQ

Is the Saga pattern a replacement for ACID transactions?
No. It’s a workaround for situations where a single ACID transaction across services isn’t possible. It provides eventual consistency and requires explicit compensation logic, not the atomicity guarantees of ACID.

Why don’t modern cloud databases use 2PC more widely?
Because its blocking behavior is incompatible with high availability goals — a coordinator failure can stall transactions and hold locks until recovery. Distributed databases generally prefer consensus protocols (Raft/Paxos) for coordinator failover, or avoid cross-shard transactions altogether.

What’s the difference between saga orchestration and choreography?
Orchestration uses a central coordinator that explicitly directs each step and compensation; choreography has services react to each other’s events with no central controller. Orchestration is easier to monitor and debug; choreography is more decoupled but harder to trace as complexity grows.

Can you combine 2PC and Sagas in the same system?
Yes. It’s common to use 2PC/local ACID transactions within a single service’s own database, and Sagas to coordinate across services. The two aren’t mutually exclusive — they operate at different scopes.

What is a “compensating transaction”?
A business-level operation that semantically undoes the effect of a previously committed local transaction — for example, issuing a refund instead of reversing a payment at the database level. It must be designed to be idempotent since messages can be redelivered.

Sources

Unknown's avatar

Author:

Innovations, logic & design thinking