When one business operation spans several services, each with its own database, there is no single transaction that covers all of it. The usual options are two-phase commit (2PC) and the saga pattern, and sagas come in two flavours: orchestration and choreography.
Comparisons of these usually list properties in a table. I find it more useful to follow one concrete operation through each approach and watch what happens when something fails.
The operation
Placing an order touches three services:
- Payments: charge the customer's card.
- Inventory: reserve the items.
- Shipping: create a shipment with the carrier.
The failure we will follow: payment succeeds, inventory reservation succeeds, then shipping fails, because the carrier's API rejects the address. The order cannot be fulfilled. The customer must not be left charged for an order that will not ship, and the stock must be released.
Approach 1: two-phase commit
How it works
A coordinator asks every participant to prepare: do all the work, write it durably, hold the locks, and promise to commit if asked. If every participant votes yes, the coordinator tells them all to commit. If any votes no, it tells them all to roll back.
Relational databases implement this through XA (MySQL XA START/XA PREPARE/XA COMMIT) or prepared
transactions (PostgreSQL PREPARE TRANSACTION/COMMIT PREPARED), and transaction managers coordinate
across them.
Following the failure
- Coordinator: prepare. Payments' database prepares the charge record. Inventory prepares the reservation.
- Shipping's prepare calls the carrier, which rejects the address. Shipping votes no.
- Coordinator: roll back everyone. No charge record, no reservation. Nothing to clean up.
That looks ideal. The problems are in the details.
What goes wrong in practice
- The card charge is not in a database. The real charge happens at a payment provider over HTTP. The provider does not take part in your XA transaction. You can prepare a local "charge pending" row, but you cannot prepare the charge itself. In practice, 2PC covers your databases, not the third parties that do the irreversible work.
- Locks held across the network. Between prepare and commit, every participant holds its locks. If the
coordinator is slow or crashes after prepare, participants are stuck "in doubt", holding locks, until it
recovers. In MySQL these show up in
XA RECOVER; in PostgreSQL inpg_prepared_xacts, and they block vacuum and other transactions. - Every participant must support it. Message brokers, caches, search indexes and most HTTP APIs do not.
- Availability. The operation succeeds only if every participant is up at the same moment.
2PC gives strong atomicity, but only among participants that support it, and at the cost of availability and lock duration. In a microservices system with external providers, that is a narrow fit.
Approach 2: orchestrated saga
How it works
A saga is a sequence of local transactions, one per service, each committed immediately. If a later step fails, the saga runs compensating actions for the steps already done, in reverse order. There is no global atomicity; there is eventual consistency, with explicit business-level undo.
In an orchestrated saga, one component, the orchestrator, tells each service what to do and decides what happens next.
Following the failure
Orchestrator (order 4471)
1. Payments.charge(4471, 59.90) -> ok, charge ch_91a
2. Inventory.reserve(4471, items) -> ok, reservation r_33
3. Shipping.create(4471, address) -> FAILED: address rejected
compensate:
2'. Inventory.release(r_33) -> ok
1'. Payments.refund(ch_91a) -> ok
order 4471 -> FAILED (reason: address rejected), customer notified
The orchestrator records every step in its own durable state (a saga log), so if it crashes after step 2, it resumes from the log rather than starting over.
What the customer sees
A brief window where the order exists and the card shows a charge, followed by a refund and an email: "We could not ship to this address." Depending on the card network, the customer may see a pending charge and a reversal. That is a business consequence of the design, and it is why we moved address validation before the charge (see "ordering" below).
What goes wrong in practice
- Compensations are not rollbacks. A refund is a new transaction, visible to the customer, possibly with fees. An email already sent cannot be unsent. Compensations must be designed per step, with the business.
- Compensations can fail too. The refund call can time out. Compensations must be retried until they succeed, which means they must be idempotent.
- Intermediate states are visible. Between step 1 and the refund, other parts of the system can see a
paid order. Readers must understand states like
PAYMENT_TAKENandCOMPENSATING. - The orchestrator is a central component that must be durable and highly available.
Approach 3: choreographed saga
How it works
No central coordinator. Each service listens for events and reacts by doing its step and publishing the result.
Following the failure
Orders publishes OrderPlaced(4471)
Payments hears it -> charges card, publishes PaymentCaptured(4471, ch_91a)
Inventory hears it -> reserves, publishes StockReserved(4471, r_33)
Shipping hears it -> carrier rejects, publishes ShipmentFailed(4471, "address")
Inventory hears it -> releases r_33, publishes StockReleased(4471)
Payments hears it -> refunds ch_91a, publishes PaymentRefunded(4471)
Orders hears both -> marks order 4471 FAILED, notifies customer
What goes wrong in practice
- Nobody owns the flow. To understand what happens to an order, you must read the event handlers of four services. Adding a step means changing several of them.
- Cycles and gaps. It is easy to create an event that no one handles in a failure case, or two services waiting for each other's events.
- Harder to answer "where is order 4471?" You need to correlate events from several services, usually through tracing or an event store.
- Same compensation and idempotency requirements as orchestration.
Its strength is loose coupling: services do not call each other, and a slow service just processes its events later.
Side by side, for this failure
| 2PC | Orchestrated saga | Choreographed saga | |
|---|---|---|---|
| Customer charged then refunded? | no (if the charge could be prepared, which it usually cannot) | yes, briefly | yes, briefly |
| Locks held during the operation | yes, across all participants | no | no |
| Works with external HTTP APIs | not as participants | yes | yes |
| Where the flow is defined | coordinator | orchestrator code | spread across handlers |
| Behaviour when a service is down | operation fails | saga waits and retries | events wait in the queue |
| Debugging a stuck order | check in-doubt transactions | read the saga log | correlate events across services |
Ordering matters more than the pattern
Whatever approach you choose, the cheapest improvement is ordering the steps so that the ones most likely to fail, and the ones that are cheapest to undo, run first:
- Validate first. Address validation, stock availability checks, fraud checks: anything that can say "no" without side effects.
- Reversible steps next. A stock reservation is easy to release.
- Irreversible or expensive steps last. Capturing a payment. Many providers support authorising first and capturing later, which turns the charge into a reversible step (void the authorisation) until the very end.
After we reordered our flow to validate the address, reserve stock, authorise payment, create the shipment, and capture payment last, refunds caused by failed orders dropped by over 90 percent. Most failures now happen before any money moves.
How we choose
- 2PC only within one database technology we control, for short operations, where all participants support it. In practice: rarely, and never across services.
- Orchestrated saga for business-critical flows with several steps and compensations: orders, payouts, account deletion. We want one place that shows the flow and its state.
- Choreography for loosely related reactions where no one needs to coordinate: sending emails, updating search, analytics. Each reaction is independent; none needs to be undone if another fails.
Summary
- 2PC gives atomicity across participants that support it, at the cost of locks, availability and compatibility. External APIs cannot take part.
- Sagas commit each step locally and undo with compensations, which are business actions that must be designed, idempotent and retried.
- Orchestration centralises the flow; choreography distributes it through events.
- Order steps so failures happen before irreversible actions, and use authorise-then-capture for payments.
References: PostgreSQL PREPARE TRANSACTION, MySQL XA transactions, Microsoft Azure Architecture Center, Saga pattern.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.