Replay: How a Crashed Workflow Comes Back

The Crash, Step by Step

Take the order workflow at event 10. The card charge has succeeded, and the worker is about to run the next line. Then someone shuts down the machine.

  1. The worker dies in the middle of a workflow task. It never sends back its commands. To the server, a workflow task started but never finished.
  2. The workflow task times out. Each workflow task has a time limit (by default, 10 seconds). When time runs out, the server adds a WorkflowTaskTimedOut event and schedules a new workflow task on the same task queue. Nothing is lost. The unfinished task never wrote anything, so the history is unchanged.
  3. Another worker picks it up. It can be any worker that listens on that task queue and knows this workflow type. It doesn't have to be the same machine. Temporal does prefer the same worker when it's still alive, but only to save time (sticky queues, see Workers and task queues).
  4. The new worker replays. First it gets the history. The 10 events arrive with the workflow task, and a very long history is fetched in pages. Then it calls your workflow function from the top, with the original input. Each time the function calls the SDK, the SDK matches that call to the next event and returns the saved answer. For an activity, the answer is the result stored in its ActivityTaskCompleted event. This all happens in memory on the worker. The worker makes no call to the server for each step, and no activity runs again.
  5. The history runs out, and the workflow is live again. As soon as the function asks for something the history has no answer for, replay is over. From then on, calls turn into real commands again. The variable charge holds ch_77, and the code is on the line right after the charge. The process that first ran that line has been dead for minutes.
Replay of the order workflow, matching history events to lines of the workflow function. Event 1, WorkflowExecutionStarted with input orderID A-91, supplies the argument to func Order(ctx, o). Event 10, ActivityTaskCompleted with result ch_77, makes charge := Charge(ctx, o) return instantly, with no HTTP call. Event 14, ActivityTaskScheduled with no result event yet, makes err := Reserve(ctx, o) block again. Then history ends: replay is over, the calls go live, and everything after that line issues real commands. Every line above the end of history is answered from history; every line below it is live.
Replay: each history event answers exactly one call, then live execution resumes

Replay happens all the time, not only after crashes. Every workflow task after the first one is a replay of everything before it. The worker rebuilds the whole run, finds the one new event at the end, and carries on. So crash recovery is not a special case. It is the normal process, just on a different worker.

What "Nothing Is Done Twice" Really Means

During replay, the SDK steps in on every call:

  • Activity calls return their saved results.
  • Timers count as already fired.
  • workflow.Now() returns the time saved in the event.
  • The workflow logger prints nothing.

Why the logger? Logging is the one "harmless" side effect people often put straight into workflow code. Without this, you would see the same log line ten times for one real run.

  • Guarantees: A workflow can resume on a different process with exactly the same local values. Any activity whose result is already in the history does not run again.
  • Does not guarantee: That code in your workflow function runs only once. It runs again in every workflow task, for the whole life of the workflow. So anything your workflow code does directly (I/O, metrics, logs, changing shared memory) happens many times.