Sub topic 10
Rolling back a bad promotion
This chapter assumes you can run an MLflow tracking server with a database backend, register model versions, set aliases with MlflowClient, and load models with models:/ URIs. Those steps are covered in earlier chapters. Here the focus is on undoing a promotion safely, and on the failure modes that make a rollback less simple than it looks.
Why rollback is cheap in an alias-based registry
A promotion in the registry does not copy or move a model. It changes one pointer. The alias champion is a named reference to one version of one registered model. Serving code that loads models:/iris-classifier@champion asks the registry which version that name points to, and loads that version.
Because the pointer is the only thing that changed, undoing the promotion is the same operation in reverse. You repoint the alias at the previous good version. The model artifacts, version numbers, tags, and run links of both versions stay untouched. No application code changes, and no new version is created.
This is the main design benefit of aliases over hard-coded version numbers. If serving code loaded models:/iris-classifier/2, a rollback would require a code change and a redeploy. With the alias, the rollback is a registry call.
The rollback sequence
A safe rollback has four steps. Skipping the first or the last is the most common source of trouble.
- Identify the bad version and the last known good version.
- Move the alias to the good version.
- Verify what the alias now resolves to.
- Record why the bad version was rejected.
Step 1: Find the last known good version
Do not assume the good version is current - 1. A version number is assigned at registration time, not at promotion time. Version 3 may have been registered but never promoted, so the version that was champion before version 2 could be version 1.
Read the history from the registry rather than from memory. If your team tags versions with validation results, as in the earlier chapter on tags, those tags help here. You can also search for versions and inspect their tags.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
current = client.get_model_version_by_alias("iris-classifier", "champion")
print("champion is currently version", current.version)
for mv in client.search_model_versions("name = 'iris-classifier'"):
print(mv.version, mv.tags.get("validation_status"), mv.run_id)Look for the most recent version that was passed and previously served. If you keep a tag such as promoted_at or previous_champion, that tells you directly. The registry does not keep an alias history on its own, so if you do not record one, you must reconstruct it from your deployment logs or release notes.
Step 2: Repoint the alias
Setting an alias that already exists on another version moves it. An alias points to at most one version of a registered model at a time, so you do not need to delete it first.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
# Roll back: repoint the alias at the previous good version.
client.set_registered_model_alias("iris-classifier", "champion", "1")The call is idempotent. Running it twice with the same arguments leaves the registry in the same state, which makes it safe to put in a runbook or an automated script that might be retried.
Step 3: Verify the result
Read the alias back and compare it to what you intended. Do this in the same script that performs the change, and fail loudly if it does not match.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
expected = "1"
mv = client.get_model_version_by_alias("iris-classifier", "champion")
assert mv.version == expected, f"champion is {mv.version}, expected {expected}"
print("champion now resolves to version", mv.version)The version attribute is a string, so compare it with a string. Comparing it to the integer 1 is a common bug that makes the assertion fail even when the rollback worked.
Step 4: Record the reason
Tag the rejected version so the next person does not promote it again by accident. Tags on a model version are plain key-value strings, so use a consistent vocabulary.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
client.set_model_version_tag("iris-classifier", "2", "validation_status", "rejected")
client.set_model_version_tag(
"iris-classifier", "2", "rejection_reason", "accuracy drop on January holdout"
)Changing validation_status from passed to rejected overwrites the old value. That is acceptable here because the current status is what matters. If you need an audit trail of the earlier value, write it to your own log before overwriting.
Removing the alias instead of moving it
Sometimes there is no good version to return to. The first version ever promoted may be the bad one. In that case you can delete the alias.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
client.delete_registered_model_alias("iris-classifier", "champion")After deletion, models:/iris-classifier@champion no longer resolves. Loading it raises an error. That is often the correct outcome, because it forces the failure to be visible instead of serving a model nobody trusts. It is a worse choice for a live service that has no fallback, because requests fail until someone sets the alias again. Decide ahead of time which behavior your service needs.
What a rollback does not do
The registry change is instant, but the running system may not notice it. This is the most important edge case in this chapter.
Loaded models are not refreshed
mlflow.pyfunc.load_model("models:/iris-classifier@champion") resolves the alias once, at load time, and returns an in-memory model object. A process that loaded version 2 keeps serving version 2 after you move the alias. The pointer changed, but nothing told the process.
You have three common options:
| Approach | How it works | Trade-off |
|---|---|---|
| Restart or redeploy | New processes load the alias and get version 1 | Simple and reliable, but needs an orchestrated restart |
| Periodic reload | The service re-resolves the alias on a timer | No restart, but there is a delay and a window of mixed versions |
| Explicit reload hook | An endpoint or message triggers a reload | Fast, but you must build and secure it |
If you run several replicas, a periodic reload means replicas can serve different versions for a short time. Check that this is acceptable for your use case, for example when predictions must be consistent for one user session.
Caches and downstream copies
A model loaded from a registry URI may be downloaded to a local cache directory. If a deployment baked the artifacts into a container image, the image still contains version 2. Moving the alias does not change the image. In that case a rollback means redeploying a build that resolves to version 1.
Batch jobs have a different problem. Predictions already written by version 2 stay in your tables. The rollback stops new bad predictions but does not repair old ones. Tag those outputs with the model version that produced them, so you can find and reprocess them later.
Pinned loads are unaffected
Code that loads a fixed version, such as models:/iris-classifier/2, ignores the alias entirely. A rollback by alias does nothing for it. Search your code for pinned URIs before you rely on this procedure.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
follows_alias = mlflow.pyfunc.load_model("models:/iris-classifier@champion")
pinned = mlflow.pyfunc.load_model("models:/iris-classifier/2")Here follows_alias returns version 1 after rollback if it is loaded fresh, while pinned returns version 2 until you change the code.
Rolling back across environments
If you promote by copying a version from iris-classifier-staging into iris-classifier-prod, the picture differs. A copy creates a new version in the production registered model, with its own version number. Rolling back there means pointing the production alias at an earlier version of iris-classifier-prod, not at a version of the staging model.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
# Version numbers here belong to iris-classifier-prod, not to staging.
client.set_registered_model_alias("iris-classifier-prod", "champion", "4")Mixing up the two numbering schemes is a frequent mistake. Version 3 in staging and version 3 in production are usually different models.
Common errors
- Setting the alias on the wrong registered model. The call succeeds on any existing model name. Always read it back.
- Passing an integer version. Use strings such as
"1", as the API expects, and compare with strings. - Rolling back to a version that was never validated. An alias move has no gate of its own. Check the tags on the target version first.
- Assuming serving picked up the change. Confirm the version that the running service reports, not just the registry state.
- Forgetting the audit tag. Without a rejection tag, the bad version stays attractive and someone may promote it again.
Putting it together
The script below combines the steps into one function that you can run during an incident. It checks that the target version exists, moves the alias, verifies the result, and tags the rejected version.
from mlflow import MlflowClient
def roll_back(client, name, alias, good_version, reason):
bad = client.get_model_version_by_alias(name, alias)
good = client.get_model_version(name, good_version)
if good.tags.get("validation_status") != "passed":
raise RuntimeError(f"version {good_version} was not validated")
client.set_registered_model_alias(name, alias, good_version)
now = client.get_model_version_by_alias(name, alias)
assert now.version == good_version, f"alias resolves to {now.version}"
client.set_model_version_tag(name, bad.version, "validation_status", "rejected")
client.set_model_version_tag(name, bad.version, "rejection_reason", reason)
return bad.version, now.version
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
print(roll_back(client, "iris-classifier", "champion", "1", "accuracy drop on holdout"))The function refuses to roll back to an unvalidated version, which prevents one bad promotion from being replaced by another. It requires the alias to exist, so it raises an error if champion was already deleted. In that case, set the alias directly after checking the target version.
Exercise
A service loads models:/iris-classifier@champion once at startup. You roll back the alias from version 2 to version 1 and verify it with get_model_version_by_alias. Ten minutes later the service still returns version 2 predictions. Why, and what are two ways to fix it?
Show answer
The alias is resolved only when load_model runs, so the running process still holds version 2 in memory. The registry change did not notify it. Fix it by restarting or redeploying the service so it loads the alias again, or by adding a reload mechanism (a periodic re-resolve or an explicit reload trigger) that calls load_model again.
Exercise
After a rollback, get_model_version_by_alias("iris-classifier", "champion").version returns "1", but your check mv.version == 1 fails. What is wrong, and what should the check be?
Show answer
The version attribute is a string, so it never equals the integer 1. Compare against the string, as in mv.version == "1".