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 time | Needs 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 type | Renaming an activity, or changing which one is called at a given point |
| Log lines, comments, and refactors that don't change any commands | Changing which branches a replay goes through, or how many |
| Adding a new signal or query handler | Removing 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)The downside is that version branches pile up in your workflow code. You can remove them, but only in this order:
- Deploy with both branches. Old runs take the old path. New runs take the new one.
- 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.
- 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
GetVersioncall 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 lives | In your workflow code, for a long time | In the routing. Your code stays clean. |
| Workers | One group, one version of the code | Several builds running side by side |
| Best for | A small, targeted change | Deploying often to workflows that change a lot |
| Cost | Leftover code that stays for a long time | More 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.