0
0
MLOpsdevops~5 mins

Why model versioning enables rollback in MLOps - Why It Works

Choose your learning style9 modes available
Introduction
When you update a machine learning model, sometimes the new version may not work as expected. Model versioning helps you keep track of each model version so you can easily go back to a previous one if needed.
When a new model update causes worse predictions and you want to quickly restore the old model.
When you want to compare different model versions to see which performs better.
When you deploy models in production and need a safe way to switch between versions.
When multiple team members work on models and you want to avoid confusion about which version is current.
When you want to keep a history of all model changes for auditing or debugging.
Commands
This command serves the model version stored in the MLflow run with ID 1234567890abcdef on port 1234 so you can test or use it.
Terminal
mlflow models serve -m runs:/1234567890abcdef/model -p 1234
Expected OutputExpected
2024/06/01 12:00:00 INFO mlflow.pyfunc.model_server: Starting MLflow model server for model runs:/1234567890abcdef/model on port 1234 2024/06/01 12:00:00 INFO mlflow.pyfunc.model_server: Listening on port 1234...
-m - Specifies the model URI to serve
-p - Sets the port number for the server
This command rolls back to an older model version by serving the model from a previous run ID fedcba0987654321 on the same port.
Terminal
mlflow models serve -m runs:/fedcba0987654321/model -p 1234
Expected OutputExpected
2024/06/01 12:05:00 INFO mlflow.pyfunc.model_server: Starting MLflow model server for model runs:/fedcba0987654321/model on port 1234 2024/06/01 12:05:00 INFO mlflow.pyfunc.model_server: Listening on port 1234...
-m - Specifies the model URI to serve
-p - Sets the port number for the server
Key Concept

If you remember nothing else, remember: model versioning lets you safely switch back to any previous model to fix problems quickly.

Code Example
MLOps
import mlflow

# Log a new model version
with mlflow.start_run() as run:
    mlflow.sklearn.log_model(sk_model=model, artifact_path="model")
    print(f"Model version logged with run ID: {run.info.run_id}")

# Serve the latest model version
import os
os.system(f"mlflow models serve -m runs:/{run.info.run_id}/model -p 1234")
OutputSuccess
Common Mistakes
Not saving each model version separately before deploying.
Without separate versions, you cannot identify or restore an older model if the new one fails.
Always log and save each model version with a unique ID or tag before deployment.
Overwriting the same model path without versioning.
This loses the history of previous models and prevents rollback.
Use versioned model URIs or run IDs to keep each model distinct.
Forgetting to stop the current model server before starting a rollback version.
Two servers on the same port cause conflicts and errors.
Stop the running model server before serving a different model version on the same port.
Summary
Model versioning saves each model with a unique ID to keep track of changes.
You can serve any saved model version to test or deploy it.
Rollback means serving an older model version to fix issues quickly.