Skip to content

Durable Execution Fundamentals: Workflows, Activities, Event History, and Deterministic Replay

2 min read

To use durable execution safely, engineers must internalize one core mental model: Workflow code is deterministic orchestration; Activity code performs all non-deterministic I/O. Violating this boundary causes replay errors that stall live executions.

Workflows vs Activities

PropertyWorkflow CodeActivity Code
PurposeOrchestrate order, branches, timers, signalsTalk to databases, HTTP APIs, LLMs, file storage
DeterminismStrictly deterministic (replayed from history)Non-deterministic (results recorded once in history)
Allowed APIsWorkflow SDK sleep, condition, uuid4Standard fetch, DB clients, crypto, fs
Retry BehaviorReplay on worker restart; never re-runs finished activitiesAutomatic retry with configurable backoff policy

How Deterministic Replay Works

Every time a workflow starts a timer, receives a signal, or finishes an activity, the orchestration server appends an immutable event to the Event History. If the worker crashes or scales down and another worker picks up the workflow three hours later:

  1. The new worker re-executes the workflow function from line 1.
  2. Whenever the code reaches an already-completed activity or timer, the SDK does not call the external API again; it feeds the recorded result straight from the Event History.
  3. Execution catches up to the exact suspension point in milliseconds and continues seamlessly.

Common Non-Determinism Traps

  • Never call raw Math.random(), Date.now(), or crypto.randomUUID() inside workflow code: use the workflow runtime's deterministic equivalents (or generate values inside an activity).
  • Never iterate over unordered external inputs without sorting when issuing child tasks.
  • Avoid unbounded event histories: long-lived entity workflows running for weeks should checkpoint state and call continueAsNew well before hitting history size limits.
ts
// Safe separation: workflow orchestrates; activity performs external I/O
export async function syncCatalogWorkflow(sourceId: string): Promise<number> {
  const snapshot = await fetchRemoteCatalogActivity(sourceId);
  const diff = computeDeterministicDiff(snapshot);
  if (diff.changedCount > 0) {
    await applyCatalogUpsertActivity(sourceId, diff);
  }
  return diff.changedCount;
}

Next: ensure retried activities never double-send messages or double-charge users in Idempotency and Effectively-Once Side Effects.

Was this helpful?