Durable Execution Fundamentals: Workflows, Activities, Event History, and Deterministic Replay
Published
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
| Property | Workflow Code | Activity Code |
|---|---|---|
| Purpose | Orchestrate order, branches, timers, signals | Talk to databases, HTTP APIs, LLMs, file storage |
| Determinism | Strictly deterministic (replayed from history) | Non-deterministic (results recorded once in history) |
| Allowed APIs | Workflow SDK sleep, condition, uuid4 | Standard fetch, DB clients, crypto, fs |
| Retry Behavior | Replay on worker restart; never re-runs finished activities | Automatic 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:
- The new worker re-executes the workflow function from line 1.
- 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.
- Execution catches up to the exact suspension point in milliseconds and continues seamlessly.
Common Non-Determinism Traps
- Never call raw
Math.random(),Date.now(), orcrypto.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
continueAsNewwell 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?