Sub topic 9
Executing the Rollback Step by Step
This chapter assumes you have read the earlier chapters on versioned artifacts, the model registry, deployment pointers, and rollback triggers. You should be comfortable with Python, kubectl, and the idea that production serves one specific model version at a time. Here you will run the rollback itself, in order, with the checks that keep it from making an incident worse.
The shape of a rollback
A rollback has a small number of steps, and the order matters. The main risk is acting fast on the wrong assumption. For example, you might move a pointer to a version that cannot load, or "fix" a problem that a data change caused and a model change did not.
Step 1: Declare the incident and freeze changes
Before you touch anything, say in the incident channel that you are rolling back, to which version, and why. This takes one message and prevents the most common collision: a colleague promoting another version while you are reverting.
Freeze the release path as well:
- Pause CI/CD jobs that promote models or apply manifests to the affected service.
- Pause any scheduled retraining job that registers and auto-promotes a new version.
- Disable automated canary promotion, so a pipeline cannot move the pointer back to the bad version halfway through your work.
Write down the current state in the incident notes: the production version, the pointer's value, and the time. If the rollback fails, this record is what lets you restore the previous state instead of guessing.
Step 2: Choose the target version
The target is the last known good version. "Last known good" means a version that actually served production traffic and met its thresholds, which is not always the highest version number below the current one. A version that was registered, passed offline tests, and was then replaced after an hour in a canary is not known good.
Use the registry metadata you recorded at release time:
- The date range it served production traffic.
- Its evaluation results and the dataset they were computed on.
- The feature schema and preprocessing version it was trained with.
- The container image or artifact URI it was built from.
Then run the compatibility check from the previous chapter against the system as it is now. The system may have changed since that version last served. A feature may have been renamed, an encoder refit, or a downstream consumer updated to expect a new output field. If the check fails, go back and pick another candidate, or reconsider whether to roll forward.
Step 3: Move the pointer
How you move the pointer depends on where the version is defined. There are two common cases, and you must know which one your service uses before an incident starts.
Case A: the serving layer loads by registry alias
If the server resolves a model URI such as models:/fraud-detector@production, rollback means moving the production alias. The script below does that safely. It records the current state, refuses a target that is not ready, and moves the alias.
from mlflow import MlflowClient
NAME = "fraud-detector"
TARGET_VERSION = "3"
client = MlflowClient()
# 1. Record the current state before changing anything.
current = client.get_model_version_by_alias(NAME, "production")
print("Current production version:", current.version)
# 2. Refuse a target that cannot be served.
target = client.get_model_version(NAME, TARGET_VERSION)
if target.status != "READY":
raise SystemExit(f"Version {TARGET_VERSION} is not READY: {target.status}")
if target.version == current.version:
raise SystemExit("Target is already the production version")
# 3. Leave an audit trail on the version being rolled back.
client.set_model_version_tag(
NAME, current.version, "rolled_back_to", TARGET_VERSION
)
# 4. Move the alias.
client.set_registered_model_alias(NAME, "production", TARGET_VERSION)
restored = client.get_model_version_by_alias(NAME, "production")
print("Production now points to version:", restored.version)An alias change only updates the registry. It does not push anything to running servers. Whether traffic changes depends on how the server was built:
- A server that resolves the alias on every load, and reloads on a schedule or signal, picks up the change at its next reload.
- A server that resolved the alias once at startup keeps serving the old model until it restarts. In that case, trigger a restart of the pods after the alias change, for example with
kubectl rollout restart deployment/model-server.
Case B: the version is baked into the deployment
If the model version is part of the container image tag or the Deployment's pod template, the registry alias is irrelevant to serving. Roll back the Deployment instead. List the revisions, return to a known good one, and watch the rollout finish.
kubectl rollout history deployment/model-server
kubectl rollout undo deployment/model-server --to-revision=2
kubectl rollout status deployment/model-serverInspect a revision before you undo to it. kubectl rollout history deployment/model-server --revision=2 shows the image and environment of that revision, so you can confirm it carries the model version you intend.
A few limits apply to kubectl rollout undo:
- It reverts only the Deployment's pod template. ConfigMaps, Secrets, and feature-store contents are not reverted. If the new release changed a ConfigMap that the old version cannot read, the undo gives you an old image with new configuration.
- Kubernetes keeps only
revisionHistoryLimitold ReplicaSets, which is 10 by default. An old revision may no longer exist. - Rolling updates are gradual. During the rollback, old and new versions serve traffic side by side until the rollout completes. If the bad version is causing severe harm, that is a reason to prefer a fast switch such as a blue-green flip, if you have one.
- If Argo CD, Flux, or another GitOps controller manages the Deployment, it will notice the drift and reapply the manifest from Git, undoing your rollback. In that case, revert the commit in Git and let the controller apply it.
Case C: a canary or blue-green release is in progress
If the bad version is still a canary, rollback is a traffic change and not a deployment of anything. Set the canary weight to zero, or switch the blue-green router back to the previous environment. The old version never stopped running, so this is the fastest case. Leave the failed environment in place for diagnosis until the incident is over.
Step 4: Wait for the old version to actually serve
After the change, confirm each of these before declaring progress:
- Every replica reports ready.
kubectl rollout statusreturns only when the rollout completes, or fails after its progress deadline. - No replica is crash-looping or stuck loading the model. A model that loaded fine a month ago can fail now if a dependency, a base image, or an artifact bucket permission changed.
- The serving layer reports the version it is running. Expose the model version in a health endpoint, a response header, or a metric label. Without that, you are inferring state from the pointer.
A quick check against a model server might look like this:
kubectl get pods -l app=model-server
curl -s http://model-server.internal/healthzThe second command should return the model version you rolled back to. If your server does not expose it, add that before the next incident.
Step 5: Hand off to verification
The rollback is not finished when the pointer moves. It is finished when the metrics that triggered it have returned to baseline. The next chapter covers that in detail. For now, keep the freeze in place, keep watching the same trigger signals, and compare them with the rule that fired. For example, if a rule like the following fired, check that it now returns False on live data:
def should_roll_back(error_rate, baseline_error_rate, p95_ms, p95_limit_ms,
max_error_increase=0.02):
"""Return True when the new version breaches an agreed threshold."""
error_regressed = (error_rate - baseline_error_rate) > max_error_increase
too_slow = p95_ms > p95_limit_ms
return error_regressed or too_slow
# During the incident: breach.
print(should_roll_back(error_rate=0.07, baseline_error_rate=0.03,
p95_ms=180, p95_limit_ms=250))
# After the rollback: back within limits.
print(should_roll_back(error_rate=0.031, baseline_error_rate=0.03,
p95_ms=170, p95_limit_ms=250))The first call prints True and the second prints False.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Registry shows the old version, metrics unchanged | Servers resolved the alias at startup and never reloaded | Restart or reload the servers, then confirm the served version |
| Pods crash-loop after the undo | Old image needs configuration or a dependency that has since changed | Check pod logs, compare the ConfigMap to what the old version expects, and consider rolling forward |
| Rollback reverts itself minutes later | GitOps controller or a pipeline re-applied the new version | Revert in Git and keep the freeze in place |
| `rollout undo` reports the revision does not exist | History was pruned by `revisionHistoryLimit` | Redeploy the old image tag explicitly from the registry's recorded artifact URI |
| Predictions are errors or nonsense after rollback | Old version expects features or preprocessing that no longer exist | Stop, re-run the compatibility check, and consider a fix-forward |
| Some requests served by the old version, some by the new | Rolling update still in progress, or two replicas pools behind one service | Wait for the rollout to complete, or check that all pods share the same selector |
Edge cases to plan for
- Stateful side effects. A model that writes to a cache, a feature store, or a decision log may have written values the old version cannot interpret. Identify them before rolling back, and decide whether to purge them.
- Batch jobs. A batch scoring job that already ran with the bad version wrote its outputs. Rolling back the online service does not repair them. Rerun the jobs or flag the affected outputs.
- Multiple models in one request path. If a ranking model and a re-ranker were released together, rolling back one can leave them mismatched. Roll them back as a pair.
- Rollback of the rollback. If you may need to return to the version you just left, do not delete it or its artifacts. Keep the recorded state from Step 1.
Exercise
You move the production alias in the MLflow registry back to version 3 and the script prints "Production now points to version: 3". Fifteen minutes later the error rate has not changed. Name two checks that distinguish a pointer problem from a problem with the old version itself.
Show answer
First, check which version the running servers report, for example through a health endpoint, response header, or prediction logs. If they still report the newer version, the servers never reloaded and need a restart or reload. Second, look at replica state and logs. If the pods are healthy and report version 3 but the errors continue, the old version is failing against the current features, preprocessing, or consumers, so re-run the compatibility check or consider rolling forward.
Exercise
Your Deployment is managed by Argo CD. You run kubectl rollout undo deployment/model-server --to-revision=2, and a few minutes later the service is back on the bad version. Why, and what should you have done?
Show answer
Argo CD compares the live state with the manifest in Git and reapplies Git's version when they differ, so it overwrote the undo. The rollback should have been made in Git, by reverting the commit that introduced the bad version, and the controller then applies it. Pausing automated sync for the incident is also reasonable.