Sub topic 2
How MLflow Tracking Is Put Together
Prerequisites
This chapter assumes you know basic Python, context managers, and scikit-learn model training. You should also know what a train/test split is and how to calculate a metric such as accuracy. You will use a terminal and install Python packages with pip.
MLflow Tracking is a system for recording what happens during a machine learning experiment. It stores run information so that you can inspect, compare, and reproduce results later.
The four parts of Tracking
An MLflow Tracking setup has four main parts:
| Part | Responsibility | Example |
|---|---|---|
| Tracking client | Sends logging requests | Python code using `mlflow.log_metric()` |
| Tracking server | Receives requests and coordinates storage | A process listening on port 5000 |
| Backend store | Saves searchable run metadata | SQLite, PostgreSQL, or MySQL |
| Artifact store | Saves files produced by runs | A local directory, S3 bucket, or Azure Blob Storage |
The tracking client is usually part of your training script. It creates runs and logs parameters, metrics, tags, and artifacts.
The tracking server is the service that your client contacts. It provides one shared address for a team. The server knows where to store metadata and artifacts.
The backend store contains small, structured values. These include experiment names, run IDs, parameter values, metric values, tags, timestamps, and run status. A database works well for this information because MLflow must search and filter it.
The artifact store contains files. Examples include trained models, plots, confusion matrices, reports, and data files. An artifact store is usually a directory or cloud object store.
Start a local Tracking server
For learning, you can run all storage locally. The following command installs MLflow and starts a server. The SQLite database stores run metadata in mlflow.db. The mlartifacts directory stores files.
pip install mlflow scikit-learn
mlflow server --backend-store-uri sqlite:///mlflow.db --default-artifact-root ./mlartifacts --host 127.0.0.1 --port 5000Keep this command running. Open another terminal for your training script. Visit http://127.0.0.1:5000 in a browser to open the MLflow user interface.
127.0.0.1 means “this computer.” It prevents other computers from connecting directly to this development server. A shared server needs a reachable host name, authentication, access control, and durable storage.
Point a client at the server
Your Python process must know where the tracking server is. Set the tracking URI before creating a run. The experiment groups related runs under a name.
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("iris-baseline")
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
C = 1.0
with mlflow.start_run(run_name="logreg-c1"):
model = LogisticRegression(C=C, max_iter=200).fit(X_train, y_train)
mlflow.log_param("C", C)
mlflow.log_param("random_state", 42)
mlflow.log_metric("test_accuracy", model.score(X_test, y_test))
mlflow.set_tag("stage", "baseline")
print("Run recorded.")The client sends the parameter, metric, and tag information to the server. The server writes those values to the backend store. No model file is created in this example because the script does not log one.
Choosing a deployment layout
A local setup is useful for experiments on one computer. It is not a reliable team system if the database and artifact directory are stored on a developer’s laptop.
For shared work, place the backend store on a durable database and the artifact store on durable shared storage. Keep the tracking server available at a stable address. This separates searchable metadata from larger files and lets each storage system do the job it handles best.
Exercise
If a run appears in the MLflow UI but its saved plot cannot be downloaded, which part of the architecture should you investigate first?
Show answer
Investigate the artifact store and the tracking server's artifact configuration. The run metadata is reaching the backend store, but the file may be missing, inaccessible, or stored at an incorrect artifact location.