Determinism: The Price of Replay

Deterministic means: same input, same result, every time. Workflow code has to be deterministic.

Why time.Now() Is a Bug: a Worked Example

"It's not deterministic" is true, but it doesn't explain much. Let's walk through what actually goes wrong:

// Workflow code. Looks fine. Will break.
if time.Now().Hour() < 17 {
    workflow.ExecuteActivity(ctx, SendToDayShift)
} else {
    workflow.ExecuteActivity(ctx, SendToNightShift)
}
  1. 16:58: the first run. time.Now().Hour() is 16, so the first branch runs. The SDK sends ScheduleActivityTask(SendToDayShift). The server saves the event ActivityTaskScheduled: SendToDayShift.
  2. 17:05: the activity finishes, and a new workflow task arrives. The worker replays the function from line 1 to rebuild its state. Now time.Now().Hour() is 17, so the else branch runs. The SDK produces ScheduleActivityTask(SendToNightShift).
  3. The SDK compares the two and refuses. The new command (SendToNightShift) doesn't match the saved event (SendToDayShift). The past can't be changed: the history is fixed, and the day-shift activity has already run. The worker raises a non-determinism error and fails the workflow task.

The clock is just the most obvious example. The real rule is wider: a replay must make the same decisions in the same order. So you can't use anything whose value or order isn't saved in the history.

What's Not Allowed, and What to Use Instead

Not allowed in workflow codeWhy it breaks replayUse instead
time.Now(), new Date()Gives a different value on every replayworkflow.Now(ctx). It returns the saved time of the workflow task, so it's the same on every replay.
rand.Int(), uuid.New()Gives a different value on every replayworkflow.SideEffect, which saves the value once as a marker event. Or use the SDK's seeded random generator.
time.Sleep(), Thread.sleepHolds up a real thread. It isn't saved, so it starts over on replay.workflow.Sleep(ctx, d), a durable timer (see Timers)
Goroutines, threads, Promise.all over raw promisesThey can run in a different order each time, so the commands come out in a different orderworkflow.Go / workflow.Selector. The SDK runs these on one thread, in a fixed order.
Looping over a Go map or a Java HashSetThe order is random on purpose, or depends on hashingSort the keys first, or use an ordered structure
HTTP calls, database queries, file I/OResults can change, and the effect happens again on every replayAn activity (see Workflows vs activities). Its result is saved as a history event.
Globals, statics, singletons, environment variablesThe next worker is a different process, with different memory and maybe a different configWorkflow input, a local variable, or an activity

What Happens When It Breaks (the Most Important Thing to Know in Production)

What happens on a non-determinism error. Replayed code produces command X, for example SendToNightShift, but the history records command Y, for example SendToDayShift, and the history is immutable. The mismatch is a non-determinism error: the workflow task fails, recorded as WorkflowTaskFailed. The execution stays RUNNING and the history is not corrupted. The server retries the workflow task with backoff, forever; until the code is fixed every retry fails the same way, so the execution waits. Fix the code and deploy: the next retry replays cleanly and the workflow resumes. This is not a workflow execution failure, which would be terminal.
A non-determinism error blocks the workflow; it does not kill it "Block, don't lose": a stuck workflow can be fixed with a deploy. A failed one usually can't. So set an alert on workflow_task_execution_failed.

Learn the difference between these two failures. It decides whether you have a quick fix or a real incident:

Workflow task failureWorkflow execution failure
Caused byA bug in workflow code: a panic, a non-determinism error, or a worker crashYour code returning an error on purpose, which the workflow doesn't handle
Saved asWorkflowTaskFailed, retried foreverWorkflowExecutionFailed, final
ResultThe workflow is stuck but alive. Fix the code, redeploy, and it carries on.The workflow is over. Only a reset or a new run brings it back.

Temporal's default rule is "block, don't lose." If there's a bug in the code, thousands of workflows wait in a retry loop instead of failing. That's because a stuck workflow can be fixed with a deploy, and a failed one usually can't. Set an alert on workflow_task_execution_failed. It's the metric that tells you a deploy went wrong.

  • Guarantees: If your workflow code is deterministic, replay rebuilds exactly the state the original run had. If something doesn't match, Temporal catches it instead of quietly corrupting the workflow.
  • Does not guarantee: That your code is deterministic. The mistake is caught at replay time, on a real workflow, maybe months after the code shipped. It is not caught when you compile.