Sub topic 2
How MLflow Tracking is put together
This chapter assumes you can write basic Python with context managers, train a scikit-learn model, install packages with pip, and use a terminal. You do not need to have used MLflow before. By the end you will know the four components behind every MLflow Tracking call, be able to start a local server, and recognize the errors that appear when those components are misconfigured.
The four components
MLflow Tracking is not a single program. It is four cooperating parts, and most setup problems come from not knowing which part is responsible for what.
| Component | What it does | Example |
|---|---|---|
| Client | The code that calls `mlflow.log_param` and similar functions, running inside your training script | The `mlflow` Python package |
| Tracking server | A web service that accepts client requests and serves the UI | `mlflow server` |
| Backend store | A database holding run metadata: experiments, runs, parameters, metrics, tags | SQLite, PostgreSQL, MySQL |
| Artifact store | A file location holding large outputs: models, plots, data files | Local directory, S3, Azure Blob, GCS |
The split exists because the two kinds of data behave differently. Metadata is small, structured and queried often ("show me runs where accuracy is above 0.9"), so a database suits it. Artifacts are large, opaque files that are written once and read rarely, so file or object storage suits them.
Starting a local server
The following commands install the packages and start a server that keeps metadata in a SQLite file and artifacts in a local directory.
pip install mlflow scikit-learn
mlflow server --backend-store-uri sqlite:///mlflow.db --default-artifact-root ./mlartifacts --host 127.0.0.1 --port 5000Each option maps to one component:
--backend-store-uri sqlite:///mlflow.dbselects the backend store. MLflow creates the filemlflow.dbin the current directory if it does not exist.--default-artifact-root ./mlartifactssets where artifacts go for new experiments.--host 127.0.0.1makes the server reachable only from your own machine. Use0.0.0.0only when you intend to expose it, and put authentication in front of it first, because a default MLflow server has no login.--port 5000sets the port. Openhttp://127.0.0.1:5000in a browser to see the UI.
Leave the server running in its own terminal. Run your training scripts from another one.
Pointing the client at the server
The client needs to know where the server is. You can set this in code or with an environment variable.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
print(mlflow.get_tracking_uri())The equivalent environment variable is MLFLOW_TRACKING_URI. It is useful because the same script can then run against a laptop server, a team server or a CI server without edits. If both are set, the call in code wins.
The tracking URI can take several forms, and each one changes the architecture:
| URI | Meaning |
|---|---|
| `http://host:port` | Talk to a tracking server. The server owns the backend store. |
| `sqlite:///mlflow.db` | No server. The client writes directly to a SQLite database file. |
| `./mlruns` or `file:./mlruns` | No server. The client writes plain files to a directory. |
| `databricks` | Talk to a Databricks workspace. |
If you never set a tracking URI, older MLflow releases write to a local mlruns directory next to your script. Newer releases may default to a local database instead, so check the release notes for your installed version. Either way, the result is that two scripts started from different directories can write to different, unrelated stores. This is the most common reason a run "disappears": it was logged, but to a store you are not looking at.
Server mode versus direct access
With an http:// URI, the client sends metadata calls to the server, and the server talks to the database. With a database or file URI, the client does everything itself. The second approach needs no server process, which is convenient for a single person. It has drawbacks for teams:
- Every client needs credentials for the database, and every client needs filesystem or object-store access.
- Nothing provides a shared UI unless someone also runs a server against the same store.
- SQLite handles concurrent writers poorly. Several training jobs logging at once can produce
database is lockederrors. Use PostgreSQL or MySQL for anything shared.
A server centralizes access. Clients need only the server URL, and the server holds the database credentials.
How artifacts travel
Artifacts take a different path from metadata, and this is where most confusion arises. The server does not always handle artifact bytes.
In the default setup, the server tells the client the artifact location for a run, and the client writes to that location itself. When the artifact root is ./mlartifacts, that location is a path on the client's machine. This works only when client and server share a filesystem, as they do on your laptop. If the server runs on another host, the client would write to a relative path on its own disk, and the UI on the server could not find the files.
The fix is proxied artifact access. You start the server with --serve-artifacts and an artifact root the server itself can reach. Clients then upload through the server over HTTP, and they need no direct credentials for the storage.
mlflow server \
--backend-store-uri postgresql://mlflow:secret@db-host:5432/mlflow \
--artifacts-destination s3://my-team-bucket/mlflow \
--serve-artifacts \
--host 0.0.0.0 --port 5000Here --artifacts-destination is where the server stores bytes. The trade-off is that all artifact traffic now passes through the server, which can become a bottleneck for large models. The alternative is to give each client direct bucket credentials and skip the proxy. Choose the proxy when you want simple credential management and moderate artifact sizes. Choose direct access when artifacts are large and you can manage per-user storage permissions.
Keep credentials out of the command line when you can, because they appear in process listings. Prefer environment variables or a secrets manager.
Where an experiment's artifact root is fixed
The artifact location is chosen when an experiment is created. Each experiment stores its own artifact root, taken from the server's default at that moment. Changing --default-artifact-root later affects only new experiments. Existing experiments keep their old location.
This matters when you move storage. If you restart the server with a new artifact root and the old experiment iris-baseline already exists, new runs in it still write to the old path. You must create a new experiment, or create the experiment explicitly with the location you want:
import mlflow
from mlflow import MlflowClient
mlflow.set_tracking_uri("http://127.0.0.1:5000")
client = MlflowClient()
experiment_id = client.create_experiment(
"iris-new-storage",
artifact_location="./mlartifacts/iris-new-storage",
)
print(experiment_id)MlflowClient is the lower-level API. Functions like mlflow.log_metric act on a global "active run", while MlflowClient methods take explicit identifiers. You will use the client when scripts manage experiments or registry entries rather than log inside a single run.
Checking that the pieces connect
Before debugging a training script, confirm each component separately. This short script logs one run and prints where everything went.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("connectivity-check")
with mlflow.start_run(run_name="ping") as run:
mlflow.log_param("check", "ok")
mlflow.log_text("hello", "hello.txt")
print("run id:", run.info.run_id)
print("artifact uri:", run.info.artifact_uri)Reading the output tells you the following:
- If the call fails with a connection error, the client cannot reach the server. Check the host, the port and whether the server is running.
- If the run appears in the UI but the artifact does not, the metadata path works and the artifact path does not. Compare the printed artifact URI with where the files should be.
- If both work, the run is in the backend store and
hello.txtis under the artifact URI.
Common errors
- Runs missing from the UI. The script and the UI use different tracking URIs. Print
mlflow.get_tracking_uri()in the script and compare it with the server you opened. database is locked. Several writers share one SQLite file. Move to PostgreSQL or MySQL, or serialize the jobs.- Artifact upload fails with a permission error or a path that does not exist. The client is writing to a server-side path, such as
/srv/artifacts, that does not exist on its own machine. Enable--serve-artifactsor fix the artifact root. - Old experiment still writes to the old location. The artifact root is fixed per experiment, as described above.
- Schema errors after upgrading MLflow. A newer client or server expects a newer database schema. Back up the database, then run
mlflow db upgrade <backend-store-uri>.
Choosing a setup
| Situation | Suggested setup |
|---|---|
| One person, quick experiments | Local server with SQLite and a local artifact directory |
| Small team on one network | Server with PostgreSQL, proxied artifacts to shared storage |
| Large models or heavy parallel jobs | Server for metadata, direct client access to object storage |
| CI pipelines | Tracking URI from an environment variable, pointing at the team server |
Exercise
You start a server on machine A with --default-artifact-root ./mlartifacts. A script on machine B sets the tracking URI to http://machine-a:5000 and logs a run with mlflow.log_text("hi", "a.txt"). The run's parameters appear in the UI, but the artifact is missing. Why does this happen, and what is the smallest change to the server command that fixes it?
Show answer
Without proxied access, the server returns the artifact location to the client and the client writes to it. The relative path ./mlartifacts resolves on machine B's disk, so the file never reaches machine A, which is where the UI looks. Metadata still works because it goes through the server to the backend store. Start the server with --serve-artifacts and an artifact destination the server can reach, for example --artifacts-destination ./mlartifacts. Clients then upload through the server over HTTP. Note that experiments created earlier keep their old artifact root, so create a new experiment to test the fix.
Exercise
Name the component that stores each of these: a run's C parameter, a pickled model file, and the list of experiments. Then say which two use the same store.
Show answer
The C parameter and the list of experiments are metadata, so both live in the backend store (the database). The pickled model is an artifact, so it lives in the artifact store. The parameter and the experiment list share the backend store.