Temporal achieves durability and statelessness through Replay.
Replay Mechanism
When a worker does not have a workflow in its local cache (or needs to recover), it performs a "replay": it starts the Workflow code from the very beginning and feeds it the existing Event History serially. The SDK uses internal state machines for every command type (e.g., a Timer state machine) to reconstruct the exact state the workflow had previously reached.
The Determinism Requirement
A Workflow must always emit the same commands in the same sequence. Temporal enforces this by checking that the commands produced by the current execution match the "dual" events already present in the history.
Nondeterminism Errors (NDE)
If the code is modified such that it produces commands in a different order or produces different commands than what is recorded in the history, the SDK will throw a NondeterminismError.
Example of Nondeterminism:
If a workflow uses asyncio.gather(activity_future, timer_future) and the order of arguments is swapped to asyncio.gather(timer_future, activity_future), the replay will fail. The Timer state machine will encounter an ActivityTaskScheduled event when it was expecting a TimerStarted event, triggering an error because the sequence of commands no longer matches the history.