microservice.guru
← All posts

Sagas: Orchestration or Choreography?

· Architecture · Microservices · Sagas · Consistency

By Alain Cyr, Founder, Koesion

microservice.guru — Sagas: orchestration or choreography? Three steps going forward, and compensations coming back after the payment fails.

Claim 4711 is approved for settlement. Three services are involved. Claims marks it approved, Reserving releases the money that was set aside, Payments sends the transfer. Then the bank rejects the IBAN.

In the old system this was one transaction and one ROLLBACK. With a database per service there is nothing to roll back. Claims committed. Reserving committed. Both are now wrong.

What a saga is

A saga is a business process cut into local transactions, where each step has a matching action that undoes it. When a step fails, you run the undo actions of the steps that already completed, in reverse order.

Settling a claim as a saga: approve, release the reserve, issue the payment. The payment fails, and compensations run backwards through the first two steps

Nothing is rolled back. The claim was approved, and now it is “approved, payment failed”. Both of those happened.

Two things about compensation are easy to miss.

It is a business action, not a technical one. Re-opening a reserve is something the Reserving team has to design, name and test. It may have its own rules. It might, for instance, require a second approval above a certain amount.

And some steps cannot be undone at all. You cannot unsend an e-mail or recall a transfer that has cleared. Put those steps last, after everything that might still fail.

There is also a property you have lost and will not get back, which is isolation. Between step one and the compensation, anybody who reads the claim sees it approved. The usual answer is to make the in-between visible, with a status such as SETTLEMENT_PENDING that other code knows to treat with care.

That leaves the question of who decides what happens next. There are two answers.

Choreography: everyone reacts

Each service listens for the previous event, does its part and publishes its own. Nobody is in charge.

Choreography: Claims publishes ClaimApproved, Reserving reacts and publishes ReserveReleased, Payments reacts and publishes PaymentFailed, which both Reserving and Claims must handle

It reads well on a slide. Now find where claim 4711 is stuck.

For two or three steps this is pleasant. There is nothing extra to build, and the services only share event names.

The trouble comes with growth. The process is not written down anywhere. It is implied by who subscribes to what, across several repositories. Adding a fraud check between approval and payment means changing the subscriptions of the services on either side. And each participant must know which failures of the others it has to compensate for, so the knowledge of the whole flow ends up smeared across all of them.

Orchestration: one place decides

One component holds the process. It sends a command to each participant, waits for the reply, records where it is, and decides the next step, including the way back.

Orchestration: a settlement saga inside Claims sends ReleaseReserve, then IssuePayment, receives a rejection, moves to COMPENSATING, sends ReopenReserve and ends in PAYMENT_FAILED

“Where is claim 4711?” is now a query on one table.

The orchestrator is not a new central service for all processes. It lives in the service that owns this process, which here is Claims. It is a state machine with a table behind it.

Its failure mode is well known. Because it is the easy place to add code, rules drift into it: the payment limit, the reserve calculation. In a year you have one clever orchestrator and three services that only store data. The discipline is the same as for a BFF. The orchestrator knows the sequence. The participants know the rules.

How I choose

Comparison of choreography and orchestration: where the flow lives, the cost of adding a step, finding a stuck instance, coupling, typical failure and best fit

Most systems end up using both, for different things.

My rule is not subtle. If the process has a compensation, or if support will ever ask where one instance is stuck, I use an orchestrator. Almost everything worth calling a saga meets one of those two conditions.

Choreography stays for reactions that are not steps: the notification, the search index, the analytics feed. If one of those fails, the claim is still settled.

What the orchestrator needs to be reliable

A short list, each item of which has bitten somebody:

You can write all this yourself. It is a few hundred lines and a table. You can also hand it to a workflow engine, which is where durable execution comes in, later in this series.

Use events for reactions. Use an orchestrator for anything that has an undo.

Where to go next

References