How-To Guide

How to Implement Durable Execution Workflows

Make multi-step workflows survive crashes with durable execution: persisted state, deterministic replay, activities and idempotent effects.

On this page

  1. Introduction
  2. The Problem with In-Memory Workflows
  3. Persisted History and Deterministic Replay
  4. Activities and Side Effects
  5. Compensation and Sagas
  6. Timers, Retries and Long Waits
  7. When to Use It
  8. Common Pitfalls and Conclusion

Introduction

A multi-step workflow, charge a card, reserve stock, send a receipt, can crash halfway and leave inconsistent state. Keeping it all in memory means a restart loses the progress; wrapping it in a database transaction means holding locks across network calls. Durable execution solves this by persisting the workflow’s progress as it runs, so after a crash it replays the recorded history and resumes where it left off. The workflow code reads like ordinary async code, but every step is recorded and every side effect is retried safely.

The Problem with In-Memory Workflows

A workflow held in memory loses its progress on a crash: the steps that completed are forgotten, and on restart it either repeats them (double-charging) or skips them (leaving state incomplete). Neither is acceptable for a workflow with side effects.

Holding a database transaction across the network calls is not the answer either: it holds locks and a connection for the whole workflow, and an external call cannot be rolled back. The workflow spans systems that no single transaction covers, which is why durable execution persists the workflow’s own progress instead.

# the crash problem
in-memory workflow: crash => progress lost => repeat (double charge) or skip
transaction across calls: holds locks + a connection for the whole workflow
# durable execution: persist progress, replay on restart, resume where it left
# side effects must still be idempotent (they run again on replay)

Persisted History and Deterministic Replay

A durable engine records each step’s result in a history as the workflow runs. On restart, it re-runs the workflow code from the beginning, but each step returns its recorded result instead of executing again. The workflow code reaches the same point and continues from there.

This requires the workflow code to be deterministic: given the same history, it must take the same path. Non-deterministic operations (reading the clock, a random number, a direct network call) must go through the engine so their results are recorded and replayed, not re-executed. That is what makes the replay reproduce the same sequence.

// Illustrative durable workflow: steps are recorded and replayed.
async function orderWorkflow(ctx: WorkflowContext, order: Order) {
  const charged = await ctx.activity('chargeCard', order);      // recorded
  if (!charged.ok) { await ctx.activity('releaseHold', order); return; }

  const reserved = await ctx.activity('reserveStock', order);   // recorded
  if (!reserved.ok) { await ctx.activity('refund', order); return; }

  await ctx.activity('sendReceipt', order);                     // recorded
}
// on restart, completed steps return their recorded result; the rest run

Activities and Side Effects

The deterministic part is the orchestration; the side effects happen in activities, which are the units the engine runs and records. An activity can call a network, write a database or send an email, and its result is recorded so a replay does not re-run it.

Activities must be idempotent, because the engine may run one and crash before recording its result, in which case it runs again on replay. An idempotency key passed to the activity lets the external system deduplicate, so a repeated charge does not charge twice. This is the same idempotency discipline as any retried effect, applied per activity.

// Activities are the side-effect units and must be idempotent.
async function chargeCard(order: Order, idempotencyKey: string) {
  return payments.charge({
    amount: order.amountCents,
    idempotencyKey,               // the external system dedupes a repeat
  });
}
// the engine records the result; on replay it does NOT re-run the activity,
// but a crash before recording => the activity runs again => must be idempotent

Compensation and Sagas

When a later step fails, the earlier steps’ effects must be undone, which is a saga: each step has a compensating action (charge -> refund, reserve -> release). The workflow runs the compensations in reverse for the steps that completed, so a partial failure leaves a consistent state.

Compensations are activities too, and they must be idempotent for the same reason. Designing each step with its compensation up front is what makes a long-running workflow able to fail safely at any point, rather than leaving a half-applied business transaction.

# saga: forward steps with compensating actions
chargeCard  <-> refund
reserveStock<-> releaseStock
sendReceipt <-> (nothing, or a "cancellation" notice)
# on failure at step N: run compensations for steps 1..N-1 in reverse
# compensations are activities => idempotent, recorded, retried

Timers, Retries and Long Waits

Durable workflows can wait for a long time without holding resources: a timer is recorded, and the workflow is resumed when it fires, days later if needed. A retry policy per activity (backoff, max attempts) is part of the workflow definition, and the retries are recorded so a replay continues the same retry sequence.

This is what makes a durable workflow different from a job queue: it can sleep for a week, wake, call an API, sleep again, and survive any number of restarts in between, because the whole progression is persisted. Use it for workflows that span long durations or many systems, not for a short job a queue handles.

// A durable timer: record the wake time, resume when it fires (days later).
await ctx.sleep(7 * 24 * 60 * 60 * 1000);   // waits a week, holds nothing
await ctx.activity('sendReminder', order);  // runs when the timer fires
// retries: defined per activity with backoff; recorded and replayed

When to Use It

Durable execution fits workflows that span multiple systems, have side effects that must not be lost or duplicated, and may run for a long time or wait for external events. Order processing, subscription billing, provisioning, and human-approval flows are the classic cases.

It is overkill for a single short operation, a stateless request, or a job a simple queue with retries handles. The durable engine adds a service to run, a workflow definition model to learn, and persisted history to store; adopt it where the multi-step, long-running, side-effecting nature justifies that.

# good fits
multi-system saga (charge + reserve + notify)
long or unbounded waits (approval, trial end, reminder)
side effects that must not be double-applied across restarts
# poor fits
a single short operation (a function call)
a stateless request
a short job a queue + retries handles

Common Pitfalls and Conclusion

Do not make non-deterministic calls directly in the workflow code; route them through the engine so the replay reproduces them. Do not make an activity non-idempotent; it may run again. Do not hold a resource across the workflow’s whole life. And do not use a durable engine for a job a queue handles.

Durable execution persists a workflow’s progress and replays it on restart, so a multi-step, side-effecting process survives crashes without losing or duplicating work. Keep the orchestration deterministic, put side effects in idempotent activities, define compensations for failures, use durable timers for long waits, and adopt it where the workflow’s complexity justifies the engine.

Sources: Temporal: Durable execution, AWS Step Functions, Sagas (Garcia-Molina & Salem).

FAQ

Why must activities be idempotent if the engine records their result?
Because the engine can run an activity and crash before recording its result, so it runs again on replay. An idempotency key lets the external system deduplicate the repeat, so a retried charge does not charge twice.
How is this different from a job queue?
A queue runs a job and retries on failure; a durable workflow persists the whole multi-step progression, so it can wait days, span many systems and resume after any restart. Use a queue for short jobs and durable execution for long, multi-step, side-effecting workflows.

Related Examples