The saga pattern on Google Cloud with Workflows
Write the saga as one Workflows definition: push each undo onto a list before its call, run the list newest first in one except block, and keep Failed for an undo that gave up. Starting each saga once, choreography over Pub/Sub, and when to merge services instead.
ON THIS PAGE
On Google Cloud, write a saga as one Workflows definition: each step adds its undo to a list before calling its service, and one except block runs that list newest first. Orchestrate like this when an order crosses services that cannot share a transaction and someone will ask where it stopped. Choreograph over Pub/Sub only when the steps belong to different teams and failures mostly roll forward. And if every undo deletes rows your own team wrote, you need a transaction, not a saga.
The undo list is data, not control flow#
A saga swaps one transaction you cannot have for a chain of local ones, each with an action that reverses it. The obvious Workflows version gives every step its own try whose handler repeats every earlier undo, so adding a participant means editing every handler below. Keep the compensations as data instead: list.prepend puts each step's undo at the front of a new list, and one loop later runs them in reverse:
- remember_release:
assign:
- undo: ${list.prepend(undo, stock_url + "/release")}
- reserve_stock:
call: http.post
args:
url: ${stock_url + "/reserve"}
auth:
type: OIDC
body: ${order}Mind the order: the undo goes on the list before the call. A reservation that timed out may still have happened, and a saga that records undos only on success forgets exactly the step most likely to need reversing. The price is that every undo must treat "nothing to undo" as success, which retries demand anyway. Assign undo: [] before the try so the handler can read it.
Undo until it works, then fail loudly#
The handler walks the list newest first, retrying each undo, then returns "cancelled":
except:
as: e
steps:
- compensate:
for:
value: target
in: ${undo}
steps:
- call_undo:
try:
call: http.post
args:
url: ${target} # auth and body as in the forward call
retry:
predicate: ${http.default_retry_predicate}
max_retries: 10
backoff:
initial_delay: 2
max_delay: 300
multiplier: 2
- cancelled:
return: "cancelled"The predicate is the idempotent one, which also retries 502s, 504s and timeouts; the non-idempotent policy skips those because the first request may have landed. Undos are safe to repeat, so they take the stronger policy and should outlast any forward step: these settings keep trying for over eighteen minutes. An idempotency key lets the payment call take that policy too.
Returning means a compensated order's execution succeeds. Failed then means one thing: an undo gave up and the books are wrong. Alert on that alone. As written, a stuck undo also stops the loop; in production, catch its final error, finish the list, then raise.
Start each saga once#
A Workflows execution gets a generated ID, so you cannot name it after the order, and a retried start runs a second saga. Start it from an Eventarc trigger on the order topic and Workflows deduplicates events for 24 hours, so a redelivered message does not start it twice. If a client publishes the same order twice, the first step, outside the try, inserts the order ID as a primary key and returns early if it exists. Store sys.get_env("GOOGLE_CLOUD_WORKFLOW_EXECUTION_ID") in that row, and on-call goes from order 81723 straight to its step entries.
Eventarc retries no event whose workflow fails or never starts, so recovery lives inside the workflow. A saga waiting on a callback still holds one of the default 10,000 concurrent executions per region. Past that, a saga started from the topic is refused with HTTP 429 and never runs: Workflows queues Executions API and Cloud Tasks starts, up to 100,000, but not Pub/Sub ones. Alert on those 429s and raise the quota early.
Orchestrate, choreograph, or merge#
Choreography over Pub/Sub has no coordinator: each service reacts to the previous one's event and listens for the failures it must reverse. It suits a few steps owned by different teams whose failures mostly roll forward. The saga then exists only as messages, delivered at least once, so every handler must expect duplicates (idempotent consumers and dead-letter topics), and no single page shows where an order stopped. Once undos must run in order, use Workflows (event-driven architecture on Google Cloud compares the alternatives).
Cost will not decide it. Calls to Cloud Run on run.app URLs are internal steps at $0.01 per thousand after 5,000 free a month (Workflows pricing, November 2026), so 100,000 orders through a twelve-step saga cost about $12 a month. Behind a custom domain, calls are external steps at $0.025 per thousand.
Before either, read the undo list. If its entries delete rows your own team wrote a second earlier, the saga is paying for a service boundary drawn through one transaction. Merge those steps behind one service and one Cloud SQL or Spanner transaction, which also restores the isolation a saga lacks, and keep the saga for what you cannot share a transaction with: the payment provider, the courier, another team's ledger. When not to go event-driven makes the same case for Pub/Sub.
On AWS and Azure#
AWS Step Functions runs the saga as a Standard workflow with Catch and Retry on each task, and an execution named after the order cannot be started twice. On Azure, a Durable Functions orchestrator is ordinary code: compensation is a try/catch that calls undo activities, and the caller picks the instance ID. For choreography, Pub/Sub's counterparts are EventBridge on AWS and Service Bus or Event Grid on Azure.
The diagram#
An Eventarc trigger starts the order saga, which reserves stock, takes payment and books a courier on Cloud Run; when the booking call fails it cancels the booking, refunds, then releases stock, paging on-call only if an undo gives up. Open it in archhy and make a copy to adapt it.
Sources: catch errors, retry steps, quotas, Eventarc triggers.
