Durable Execution & Workflow Engines
Build crash-proof multi-step workflows with Temporal and Restate using deterministic event replay, activity boundaries, durable timers, and safe versioning.
In this lesson (4 bite-sized sections)
Replacing hand-rolled state machines with durable replay
Multi-step sagas across microservices usually decay into fragile glue: a Postgres status column, polling cron workers, five Kafka topics, and hand-rolled retry timers. Durable execution engines such as Temporal and Restate replace that machinery with an append-only event history (journal). You write straightforward sequential code with loops, try/catch, sleeps, and compensations, while the engine logs the result of every step. If a worker process crashes mid-flight or restarts during a deploy, a surviving worker replays the recorded history to rebuild exact local variables and stack frames up to the last completed step.
Deterministic workflows vs side-effecting activities
Durable execution enforces a strict boundary between two layers:
- Workflow code (deterministic orchestration): decides what runs next, coordinates retries and compensations, and awaits external signals. Because it replays on every recovery, it never calls wall clocks, random number generators, or network APIs directly; it invokes engine wrappers (
workflow.now(),workflow.sleep()) whose outputs are logged once and replayed verbatim. - Activities (side-effecting work): execute database writes, payment APIs, emails, or LLM calls. The engine persists each activity's output into the event history so later replays return the recorded value without re-invoking the dependency. Because an activity itself can time out and retry before reporting back, external targets still require idempotency keys.
- Start workflowpersist input in event history
- Schedule activityworkflow emits command, yields
- Run side effectactivity worker executes with idempotency key
- Log completionengine appends result to journal
- Worker crashesin-memory state lost on host
- Replay and resumenew worker reads logged results, runs next step
Long-running timers and human-in-the-loop approvals
Because durable timers and external signals persist in the engine rather than holding open threads, a workflow can sleep for 30 days between billing cycles or wait a week for a manager's approval webhook while occupying zero worker memory. When the timer expires or the signal lands, any stateless worker replays the history in milliseconds and advances. For perpetual entity workflows (recurring subscriptions or long-lived order trackers), use Continue-As-New periodically to roll over compact state into a fresh execution so event histories stay bounded and replay latency never drifts upward. Note the workload boundary too: durable execution excels at complex per-entity orchestration and sagas at tens to thousands of workflows per second, whereas high-volume firehose ingestion (hundreds of thousands of clickstream events per second) still belongs in Kafka and Flink.
Versioning live histories: the operational trap
Deploying code changes while thousands of multi-day workflows are paused mid-flight is the main operational hazard. If v1 ran Activity A then Activity B, and v2 inserts a new Activity X before B, replaying a v1 history against v2 code raises a nondeterminism error because the expected command sequence no longer matches the recorded journal. Production teams guard live upgrades with explicit version branches (patched("add-step-x")) so older histories stay on the v1 path while new runs take v2, or worker build-ID routing so in-flight workflows drain safely on v1 workers. Running automated replay tests in CI against sampled production event histories catches accidental determinism breaks before a deploy reaches active runs.
Sources: Temporal Core Architecture · Temporal Workflow Versioning · Restate Durable Execution
Apply
Your turn
The task this lesson builds to.
Architect a durable execution workflow for an enterprise expense-reimbursement pipeline that validates receipts, waits up to 7 days for manager approval with a 48-hour reminder, disburses funds via bank ACH, and survives worker crashes and code deploys.
Think about
- Which logic belongs in deterministic workflow code versus side-effecting activities?
- How do durable timers and external signals pause for 7 days without holding worker memory?
- How will you deploy a new compliance-check step without breaking in-flight approvals?
Draft your design answer here Save your draft, compare against the model answer, and track progress.
Practice
Make it stick
A second problem on the same idea, so it survives past today.
Compare a Temporal/Restate durable execution engine against a Kafka consumer-group pipeline for two workloads: a 30-day subscription renewal lifecycle and a 500K events/sec real-time clickstream aggregator. Justify which engine owns each workload.
Think about
- Why does per-entity event-history persistence excel at low-to-medium QPS long-lived lifecycles?
- Why does persisting a durable workflow journal per click collapse at 500K events/sec?
- How should perpetual subscription workflows prevent unbounded history growth?
Draft your design answer here Save your draft, compare against the model answer, and track progress.