Custom Software Development

Saga vs Two-Phase Commit: Which Fits a Distributed Transaction?

Shubham Parmar8 min read
Saga vs two-phase commit — which distributed transaction pattern to use

In short: If the rule fits in one database, use a local transaction and do not distribute it. If the work crosses services, prefer a saga: each step commits locally, and a failure runs a compensating action you have actually written. Use two-phase commit only when every participant can prepare, the transaction is short, and a partial result is not acceptable. A saga is not a distributed lock. Other people can see the steps that have already committed.

What this means for you

  • One service, one database, one invariant: a local transaction. Stop there.
  • An order that reserves stock, takes payment, and books a shipment: a saga, with a compensation for each step.
  • Do not hold locks across services while a person, a carrier, or a payment provider decides.

Distributed transactions show up the first time an order is no longer one row. Stock is in one service, payment is in another, and the warehouse is a third system you do not operate. Something still has to make those steps succeed or undo together. The two serious options are two-phase commit and a saga. They do not offer the same guarantee, and picking the stronger-sounding name is how teams hold locks they cannot afford or ship compensations they have not defined.

What each protocol actually does

Two-phase commit

A coordinator asks every participant to prepare. Each participant does its work up to the point where it can still roll back, and promises to commit if asked. If every participant promises, the coordinator tells them to commit. If one refuses, the coordinator tells them to roll back. AWS's description of saga orchestration starts from this, because it is what a relational database already does inside one engine. Across services, the same two phases only work when every store can take part and something you run can coordinate them.

The cost is the lock. Oracle's protocol comparison puts it plainly: XA participants hold locks for the whole transaction. That is acceptable when the transaction is a short exchange between machines. It is a bad fit when a step waits on a person, a carrier, or a provider that may take minutes. A prepared transaction that loses its coordinator can also sit locked until someone intervenes. That blocking is the reason application teams are told not to stretch two-phase commit across a business process.

A saga

A saga is a sequence of local transactions. Each one commits in its own database, then asks the next step to run. If a step fails, the saga runs compensations in reverse: refund the charge, release the stock, cancel the booking. Azure's saga pattern describes those local transactions as atomic inside one service and explicitly not one ACID transaction across services. Temporal's guidance adds the entry condition: you can name a compensating action for each step, and the business accepts that observers will see intermediate states.

Coordination is either an orchestrator that calls the steps, or choreography where each service emits an event and the next service reacts. AWS notes that the orchestrator can become a single place the workflow depends on, which is why some teams prefer events. Events do not remove the need for a record of which steps have run. They move that record into the consumers. Kafka vs RabbitMQ is the transport choice if you go that way. It is not the consistency choice.

Try not to distribute the transaction

Both protocols are more machinery than a single database transaction. If the invariant — "this order is paid and the stock is reserved" — can live in one service, keep it there. Splitting the database per service and then rebuilding atomicity with a saga is a common way to pay twice. Event sourcing vs CRUD is a related trap: a log of every change is not a substitute for deciding which service owns the rule.

When a step in another system still has to happen, publish the fact reliably after the local commit. The saga, if you still need one, starts from that fact. It should not start from a hope that two databases will lock together.

Where a saga fails in practice

  • Compensation is not an undo button. Oracle points out that a compensating withdrawal can fail because a later withdrawal already took the funds. Azure says the same thing more generally: a compensation can fail and leave the system inconsistent. You need a path for that case — a person, a retry, a ledger entry — not a catch block that logs and moves on.
  • Steps must be safe to repeat. AWS lists idempotency as a requirement, because a crash can rerun a step that already committed. Idempotency keys are how the API side of that is usually done. A saga without them will double-charge on the retry that was supposed to save you.
  • There is no isolation. Two sagas can interleave. One reads stock the other has logically reserved but not yet compensated. AWS suggests semantic locking for that class of race. If the business cannot bear that window, a saga is the wrong tool and the data should not have been split.
  • You have to be able to see the stuck ones. A five-step order that stopped at step four is an operations problem. Tracing which step ran is part of the design, not a dashboard you add after the first incident.

A practical decision

SignalFavors
The invariant is one database you controlLocal transaction
Every participant can prepare, and the work finishes in a short machine callTwo-phase commit
A step is a third-party API with no prepare operationSaga
The process waits on a person or runs for minutes or longerSaga
You cannot name a compensation for a stepDo not saga that step; keep it in one transaction or change the process
Partial states must be invisible to every readerNot a saga; reconsider the service split

How we scope this

On custom software builds, especially order, billing, and ERP-style workflows, we start by asking which rule has to be atomic and which service owns the data for it. A saga is the design when that rule genuinely crosses a boundary and each step has a compensation. Two-phase commit stays inside the stores that already support it, for short work. How long the workflow engine and the compensations take to build depends on how many steps can fail and how visible those failures must be. That is set in discovery, not as a fixed timeline. If the product is really a system of record for the operation, ERP vs CRM is the earlier question: a CRM rarely needs this protocol, and an ERP often does.

Frequently asked questions

What is two-phase commit?

Two-phase commit is a protocol that tries to make several participants commit or roll back as one transaction. In the prepare phase, each participant promises it can commit. In the commit phase, the coordinator tells them all to commit, or tells them all to roll back if any participant refused. Participants hold locks from the prepare until the decision. Relational databases use this inside one engine. Across independent services, something has to play the coordinator, and every participant has to support the protocol.

What is the saga pattern?

A saga splits a business transaction into local transactions, one per service. Each local transaction commits on its own and then starts the next step, either by an event or through a coordinator. If a later step fails, the saga runs compensating transactions that undo the steps that already committed. The data is not locked across services for the whole workflow. Other users can see the intermediate state.

When should you use a saga instead of two-phase commit?

Use a saga when the work crosses services or databases, when a step calls a third party that cannot join a two-phase commit, or when the process runs long enough that holding locks would be unacceptable. Temporal's guidance fits a saga to those cases when you can define a compensating action and the business can tolerate eventual consistency. Order fulfillment, booking, and onboarding are the usual shape. A charge, a reservation, and an email do not share one database transaction.

When is two-phase commit the right tool?

When you need one atomic outcome, the transaction is short, and every participant is under your control and can prepare and commit. Oracle's comparison notes that XA holds locks for the whole transaction, so it belongs in short machine-to-machine work, not in a flow that waits on a person. If a participant is an external API with no prepare step, two-phase commit is not available, whatever the consistency requirement is.

What is the usual mistake when adopting a saga?

Treating compensation as a free rollback. A compensating action can itself fail — a refund that cannot be issued because the balance has already moved, a cancel that the other system rejects. Oracle's transaction-protocol note calls that a heuristic outcome: the saga stopped, and the data is not back where it started. The other mistake is using a saga for a rule that should have stayed inside one database transaction in the first place.

Sources

Need help putting this into practice?

Tech Programmer builds and ships this work for startups and enterprises. Tell us what you are trying to do and we will tell you what it takes.

Related reading