Workflows8 min read
Durable Workflows and the Saga Pattern
Multi-step, cross-system operations fail halfway. Persisted steps, idempotency keys and compensations turn that from an incident into a state.
A cross-chain deposit sounds like one action to a user. To the system it's seven: derive an account, reserve a quote, approve a token, burn on the source chain, wait for an attestation, mint on the destination, credit a balance. They span multiple chains, a third-party attestation service and your own database.
Any of them can fail. The process running them can crash between any two. The question isn't whether a deposit will be interrupted halfway — it's what state it's in when that happens.
Why a script isn't enough
The first version is always a function:
async function deposit(user: User, amount: bigint) {
const account = await deriveAccount(user);
const quote = await reserveQuote(amount);
await approve(account, amount);
const burn = await burnOnSource(account, amount);
const attestation = await waitForAttestation(burn);
await mintOnDestination(attestation);
await creditBalance(user, amount);
}If the worker restarts during waitForAttestation, all progress lives in memory and is gone. Re-running the function from the top would approve and burn again. That's not a bug report — that's an incident.
Ingredient 1: a journal
A durable workflow persists the outcome of every step before moving on. On restart, it reads the journal and skips completed steps:
for (const step of workflow.steps) {
const entry = await journal.get(runId, step.name);
if (entry?.status === "completed") continue; // already done — resume past it
const result = await runWithRetries(step, ctx);
await journal.put(runId, step.name, { status: "completed", result });
}The journal is the source of truth for "where are we?" — not the call stack.
Ingredient 2: idempotency keys
The journal alone has a gap: the process can crash after a step's side effect happened but before the journal recorded it. On resume, the step runs again.
The fix is to make every side effect idempotent. Each step gets a deterministic key — ${runId}:${stepName} — and effects are recorded against it:
async function execute(key: string, effect: () => Promise<Result>) {
const existing = await ledger.get(key);
if (existing) return existing; // dedup: the effect already happened
const result = await effect();
await ledger.put(key, result);
return result;
}Where possible, push the key all the way down to the external system (many APIs accept an idempotency header). For on-chain actions, deterministic inputs — nonces, salts, counterfactual addresses — play the same role.
Ingredient 3: retries for transient failures
RPC timeouts and rate limits are expected. Steps retry with exponential backoff before anything is considered failed. Only when retries are exhausted, or the error is permanent, does the workflow escalate.
Ingredient 4: compensation — the saga
When a step fails permanently, completed steps may have left things half-done: a reserved quote, an outstanding approval. A saga pairs steps with compensations and runs them in reverse:
const steps = [
{ name: "reserveQuote", run: reserve, compensate: release },
{ name: "approveUsdc", run: approve, compensate: revoke },
{ name: "burnOnSource", run: burn, irreversible: true },
// …
];If approveUsdc fails permanently, the engine runs release for the quote and stops cleanly.
The point of no return
Some steps can't be undone. Once USDC is burned on the source chain, there is no compensation that un-burns it. The only acceptable outcome is forward: get the attestation, mint, credit.
So after an irreversible step, the strategy changes. Failures don't trigger compensation; they trigger more retries, and if those run out, the run is parked for reconciliation — a separate process that finds stuck runs and resumes them, or flags them for a human.
Marking steps as irreversible makes this explicit in the workflow definition instead of buried in error handling.
Putting it together
- Journal → resume after crashes.
- Idempotency keys → retries and replays never duplicate effects.
- Retries with backoff → transient failures heal themselves.
- Compensations → permanent failures before the point of no return roll back cleanly.
- Parking + reconciliation → failures after it are driven to completion.
Queues like BullMQ give you reliable delivery and retries; the patterns above give you correctness on top.