Sub topic 3
Pointing production at a version with aliases or config
This chapter assumes you know how a trained model is packaged and served, that you can read Python and shell commands, and that you have used a container platform such as Kubernetes. It builds on the previous chapters: every deployable model is an immutable, versioned artifact, and a registry tracks those versions.
Why a pointer matters
A registry holds many versions of a model. Something must decide which one serves traffic right now. That something is the deployment pointer: a small, mutable reference that maps a stable name, such as "production", to one immutable version.
The artifacts never change. Only the pointer moves. This is what makes rollback a routine operation. If version 4 misbehaves, you do not rebuild or retrain anything. You point production back at version 3.
There are three common places to keep the pointer:
| Pointer location | Example | Changed by | Takes effect when |
|---|---|---|---|
| Registry alias | `models:/fraud-detector@production` | Registry API call | The serving layer next resolves the alias |
| Config reference | A ConfigMap value or a config file in Git | Config change and apply | The server reloads or restarts |
| Image or deployment spec | `model-server:v4` in a Deployment | New rollout or `kubectl rollout undo` | Pods are replaced |
Each has a different speed, audit trail, and failure mode. The rest of the chapter covers them.
Registry aliases
An alias is a named, mutable reference to one model version. A version can have several aliases, but a given alias points to only one version of a registered model at a time. Older MLflow releases used fixed stages such as "Staging" and "Production". Aliases replace them and let you choose your own names, such as "production", "champion", "challenger", or "canary".
The following script reads the current production version and moves the alias back to a known good version.
from mlflow import MlflowClient
client = MlflowClient()
name = "fraud-detector"
# Which version does production currently point to?
current = client.get_model_version_by_alias(name, "production")
print("Current production version:", current.version)
# Roll back by moving the alias to the last known good version.
client.set_registered_model_alias(name, "production", "3")
restored = client.get_model_version_by_alias(name, "production")
print("Production now points to version:", restored.version)Setting an alias that already exists reassigns it. Nothing is deleted. Version 4 stays in the registry with its metadata, so you can inspect it during the incident review.
Moving the alias does not by itself change what is running. This is the most common surprise. The alias is only a lookup entry in the registry. A serving process that loaded version 4 into memory at startup keeps serving version 4 until something tells it to load again.
How the serving layer consumes an alias
The serving layer resolves the alias in one of three ways. Each one determines how long a rollback takes.
- Resolve at startup. The server loads
models:/fraud-detector@productiononce. A rollback requires a restart or a rolling restart of the pods. - Poll periodically. The server checks the alias every N seconds and reloads when the resolved version changes. A rollback takes effect within one polling interval, with no restart.
- Resolve per request. The server asks the registry on each call. This is rarely a good idea, because it makes the registry a hard dependency of every prediction.
The loading code for option 1 looks like this:
import mlflow.pyfunc
model = mlflow.pyfunc.load_model("models:/fraud-detector@production")
print(model.metadata.run_id)For option 2, a small background loop can compare the resolved version with the loaded one:
import threading
import time
import mlflow.pyfunc
from mlflow import MlflowClient
NAME = "fraud-detector"
ALIAS = "production"
POLL_SECONDS = 30
client = MlflowClient()
state = {"version": None, "model": None}
def refresh_once():
version = client.get_model_version_by_alias(NAME, ALIAS).version
if version != state["version"]:
# Load first, swap second. If loading fails, the old model keeps serving.
new_model = mlflow.pyfunc.load_model(f"models:/{NAME}/{version}")
state["model"] = new_model
state["version"] = version
print("Now serving version", version)
def poll_forever():
while True:
try:
refresh_once()
except Exception as exc:
print("Refresh failed, keeping current model:", exc)
time.sleep(POLL_SECONDS)
refresh_once()
threading.Thread(target=poll_forever, daemon=True).start()Two details matter here. The loop loads by explicit version number, not by alias, so the loaded model is exactly the version that was resolved. If the alias moved between the lookup and the load, loading by alias could give you a different version than the one you logged. The loop also loads the new model before replacing the old one, so a failed load does not leave the server without a model.
Gotchas with aliases
- Stale caches. Registry clients, proxies, and sidecars may cache lookups. Know every cache between the registry and the server, and confirm that a rollback invalidates them.
- Multiple replicas disagree briefly. With polling, replicas refresh at different moments. For up to one interval, some replicas serve the old version and some the new one. Add jitter to the interval so all replicas do not hit the registry at the same moment.
- Registry outage. If the server resolves the alias only at startup and the registry is down during a restart, the pod cannot start. Cache the last resolved model on local or shared storage, or resolve the alias in a deployment step and pass an explicit version to the pod.
- Permissions. Moving the production alias is a production change. Restrict who and what can call
set_registered_model_alias, and record who did it. - Alias names are conventions. Nothing in the registry stops someone from pointing "production" at a version that failed validation. Add checks in your promotion and rollback tooling.
Config-based pointers
Many teams do not let the server talk to the registry at all. Instead, a deployment step resolves the version and writes it into configuration, which the server reads. The pointer then lives in Git, a ConfigMap, a feature flag service, or a parameter store.
A small config file might look like this:
model:
name: fraud-detector
version: "3"
artifact_uri: s3://ml-artifacts/fraud-detector/3/This approach has clear benefits:
- The change goes through code review and has a Git history. A rollback is a revert commit, which is easy to audit.
- The server needs no registry credentials or network path at runtime.
- The environment is reproducible. The file says exactly what should be running.
It also has costs. The change must pass through your delivery pipeline, which may be slower than an API call. If the pipeline itself is broken during an incident, you need a documented manual path.
With Kubernetes, a ConfigMap value can hold the version. Pods read it as an environment variable, which is fixed when the pod starts. Changing the ConfigMap does not update the environment of running pods, so you must restart them:
kubectl patch configmap model-config --type merge -p '{"data":{"MODEL_VERSION":"3"}}'
kubectl rollout restart deployment/model-server
kubectl rollout status deployment/model-serverIf the ConfigMap is mounted as a file instead, Kubernetes eventually updates the mounted file, but the application must notice and reload it. Do not assume this happens. Test it.
Baking the version into the deployment
The third option puts the version directly into the image tag or deployment spec. The model is either packaged in the container image or the image receives an explicit version argument. A new model release produces a new Deployment revision. Kubernetes keeps revision history, so you can list revisions and return to an earlier one:
kubectl rollout history deployment/model-server
kubectl rollout undo deployment/model-server --to-revision=2
kubectl rollout status deployment/model-serverkubectl rollout undo restores the Deployment's pod template to that revision. The rollout status command waits until the new pods are ready or the rollout fails, which gives you a clear signal.
Limits to keep in mind:
- Rollout history is limited by
revisionHistoryLimit, which defaults to 10 revisions. Old revisions beyond that are gone. - Revision numbers are cluster-local. They do not match model version numbers. Record the mapping, for example with an annotation on the Deployment or with a label such as
model-version=3on the pods. rollout undoreverts the whole pod template, not only the model. If the same release also changed the container image, resource limits, or environment variables, those revert too. This is correct if the whole release is bad. It is wrong if only the model needs to go back.- If a GitOps tool such as Argo CD or Flux manages the Deployment, a manual
kubectl rollout undomay be reverted by the tool, since Git still says the new version. In that case, revert in Git.
Comparing the three approaches
| Question | Registry alias | Config reference | Baked into deployment |
|---|---|---|---|
| Rollback speed | Seconds to one polling interval, if the server reloads | Minutes, through the pipeline and a restart | Minutes, as pods roll |
| Audit trail | Registry history, if enabled | Git history | Cluster revision history and Git |
| Runtime dependency on registry | Yes, unless cached | No | No |
| Risk of drift between intent and reality | Higher, because alias and running version can differ | Medium | Lower, because the spec is the running version |
| Needs reload logic in the server | Yes | Yes, or restart | No |
No option is best in every case. A common combination is to use the registry alias as the source of truth for intent, and have the deployment pipeline resolve that alias into an explicit version in the Deployment spec. You get the registry's metadata and one place to change intent, together with the reproducibility of a pinned version.
Making the running version visible
Whatever pointer you choose, the server must report which version it actually runs. Without this, you cannot tell whether a rollback took effect. Expose the version in at least three places:
- A
/healthor/versionendpoint that returns the model name and version. - A response header or field in each prediction, such as
X-Model-Version. - A metrics label, so dashboards can split error rate and latency by version.
The next chapters on triggers and verification rely on this. A pointer tells you what should run. The reported version tells you what does run. A rollback is finished only when the two agree on every replica.
Choosing a design
Use these criteria when you decide how to point production at a version:
- If you need the fastest rollback and you can build safe reload logic, use an alias with polling. Add a version endpoint and test the reload path.
- If auditability and reproducibility matter most, use a config reference in Git, and make sure the pipeline has a fast path for emergencies.
- If your model ships inside the image and the service is simple, use the Deployment spec and
kubectl rollout undo, with a recorded mapping to model versions. - Whichever you choose, rehearse it. A pointer that has never been moved under test is an assumption.
Exercises
Exercise
You run set_registered_model_alias(name, "production", "3") to roll back, but dashboards still show errors from version 4 five minutes later. The server loads models:/fraud-detector@production once at startup. What is the most likely cause, and what are two ways to fix it?
Show answer
The alias moved, but the server never reloaded, so the pods still hold version 4 in memory. Fix it now by restarting the pods, for example with kubectl rollout restart deployment/model-server. Fix it for the future by adding polling or reload logic that detects a changed alias and loads the new version, or by having the deployment step resolve the alias to an explicit version and roll the pods.
Exercise
A team keeps the model version in a Kubernetes ConfigMap exposed to pods as an environment variable. They edit the ConfigMap to roll back, and kubectl rollout status reports nothing changing. Why, and what should they do?
Show answer
Environment variables are read when a pod starts, so editing the ConfigMap does not change running pods. They should run kubectl rollout restart deployment/model-server, then kubectl rollout status, and confirm the version through the server's /version endpoint.
Exercise
Name two risks of using kubectl rollout undo --to-revision=2 for a model rollback, and one way to reduce each.
Show answer
First, it reverts the whole pod template, so unrelated changes in the same release, such as resource limits or environment variables, also revert. Reduce this by keeping model changes in separate releases or by rolling back through the model pointer. Second, revision numbers do not match model versions and old revisions can be pruned by revisionHistoryLimit. Reduce this by labeling or annotating each revision with the model version and keeping a sufficient history limit. A GitOps tool reverting the manual change is also a valid risk, mitigated by reverting in Git.