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)
}- 16:58: the first run.
time.Now().Hour()is 16, so the first branch runs. The SDK sendsScheduleActivityTask(SendToDayShift). The server saves the eventActivityTaskScheduled: SendToDayShift. - 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 theelsebranch runs. The SDK producesScheduleActivityTask(SendToNightShift). - 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 code | Why it breaks replay | Use instead |
|---|---|---|
time.Now(), new Date() | Gives a different value on every replay | workflow.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 replay | workflow.SideEffect, which saves the value once as a marker event. Or use the SDK's seeded random generator. |
time.Sleep(), Thread.sleep | Holds 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 promises | They can run in a different order each time, so the commands come out in a different order | workflow.Go / workflow.Selector. The SDK runs these on one thread, in a fixed order. |
Looping over a Go map or a Java HashSet | The order is random on purpose, or depends on hashing | Sort the keys first, or use an ordered structure |
| HTTP calls, database queries, file I/O | Results can change, and the effect happens again on every replay | An activity (see Workflows vs activities). Its result is saved as a history event. |
| Globals, statics, singletons, environment variables | The next worker is a different process, with different memory and maybe a different config | Workflow input, a local variable, or an activity |
What Happens When It Breaks (the Most Important Thing to Know in Production)
Learn the difference between these two failures. It decides whether you have a quick fix or a real incident:
| Workflow task failure | Workflow execution failure | |
|---|---|---|
| Caused by | A bug in workflow code: a panic, a non-determinism error, or a worker crash | Your code returning an error on purpose, which the workflow doesn't handle |
| Saved as | WorkflowTaskFailed, retried forever | WorkflowExecutionFailed, final |
| Result | The 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.