Versioning: Changing Code While Workflows Are Running

Why Deploys Are Risky Here

Say you add one activity call to an order workflow:

func Order(ctx workflow.Context, o Order) error {
    Charge(ctx, o)
    FraudCheck(ctx, o)   // ← new line, shipped Tuesday
    Reserve(ctx, o)
}

An order that started on Monday has the history Charge → Reserve. On Tuesday it wakes up for its next workflow task, and a worker with the new code replays it. Where the history says ActivityTaskScheduled: Reserve, the new code produces ScheduleActivityTask: FraudCheck. They don't match. That's a non-determinism error, and the workflow is blocked (see Determinism).

And it's not just one workflow. Every running workflow of that type is now blocked. The only quick fix is to roll back the deploy.

Safe to change any timeNeeds versioning
The code inside an activity. It is never replayed.Adding, removing, or reordering activity calls, timers, child workflows, or side effects
Adding a brand-new workflow typeRenaming an activity, or changing which one is called at a given point
Log lines, comments, and refactors that don't change any commandsChanging which branches a replay goes through, or how many
Adding a new signal or query handlerRemoving or renaming a signal handler that old runs still use

Option 1 — Patch in Place (GetVersion / patched)

You add a branch based on a saved version marker. The first time a workflow reaches this call, the SDK saves the current version as a MarkerRecorded event. On every replay after that, it returns the saved value. So old workflows always take the old branch, and new workflows always take the new one.

v := workflow.GetVersion(ctx, "AddFraudCheck", workflow.DefaultVersion, 1)
if v == workflow.DefaultVersion {
    // executions that started before this deploy: old path, unchanged
} else {
    FraudCheck(ctx, o)
}
Reserve(ctx, o)
A versioning decision. A new worker binary is deployed that contains both branches. Replay reaches GetVersion with change ID AddFraudCheck. The answer comes from history. Left branch, old executions: the history has no marker because the execution started before the deploy, so it takes the old path and its commands match the old history. Right branch, new executions: the history has marker v equals 1, or this is the first run of a new execution, which records MarkerRecorded v equals 1 the first time it gets here; it takes the new path and FraudCheck is scheduled. Both outcomes replay cleanly.
One binary, two behaviours — chosen by what is recorded in each execution's history Neither branch is an error. The old path keeps old workflows deterministic. The marker keeps new workflows on the new path for the rest of their lives.

The downside is that version branches pile up in your workflow code. You can remove them, but only in this order:

  1. Deploy with both branches. Old runs take the old path. New runs take the new one.
  2. Wait until every workflow that started before the patch has finished. Don't just wait "a while." Check. A visibility search by start time, or a count by workflow type, tells you when the last old run is gone. For a workflow that sleeps for 30 days, that means waiting at least 30 days.
  3. Deprecate, then delete. Mark the patch as deprecated, so new runs stop saving the marker, and deploy. Then, in a later deploy, remove the branch and the GetVersion call completely.

Option 2 — Give the Workflow Type a New Name

Register OrderWorkflow_v2 next to OrderWorkflow. Send all new starts to v2, and keep v1's code deployed until the last v1 run finishes. There are no branches and no markers, and each version's code is easy to read.

The cost: duplicated code, and workers that have to run both versions. This is the practical choice when the change is too big for a clean branch, like a restructure rather than one added line.

Option 3 — Worker Versioning (Build IDs)

This is the newer approach, and it works quite differently. You don't have one group of workers running old and new workflows together. Instead, you tag each worker deployment with a build ID, and the server keeps each workflow on the build that started it.

New workflows go to the new build. Running workflows keep going to workers with the code they started on. Once those workflows finish, you shut down the old workers.

Patching (GetVersion)Worker versioning (build IDs)
Where the branch livesIn your workflow code, for a long timeIn the routing. Your code stays clean.
WorkersOne group, one version of the codeSeveral builds running side by side
Best forA small, targeted changeDeploying often to workflows that change a lot
CostLeftover code that stays for a long timeMore to operate: winding down old builds, shutting them off, and paying to run them meanwhile
  • Guarantees: With a patch or a pinned build ID, a workflow that started on old code keeps making the old decisions for its whole life, even through many later deploys.
  • Does not guarantee: Any protection you didn't set up. Temporal doesn't catch a breaking change when you deploy. It catches it at replay time, on live workflows, one failed workflow task at a time.