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.
- 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.
- The workflow task times out. Each workflow task has a time limit (by default, 10 seconds). When time runs out, the server adds a
WorkflowTaskTimedOutevent 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. - 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).
- 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
ActivityTaskCompletedevent. This all happens in memory on the worker. The worker makes no call to the server for each step, and no activity runs again. - 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
chargeholdsch_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 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.