Skip to main content

How to Set the Number of Workers in an MLflow Model Container

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A container from mlflow models build-docker doesn't start one model server worker, as mlflow models serve does, but one per CPU it can see, and every worker loads its own copy of the model. On a computer with many cores that's a lot of memory for a small model. The MLFLOW_MODELS_WORKERS environment variable sets the number. This guide runs the same image with the default and with two workers and compares them, with real output from MLflow 3.18.0.

Syntax​

Syntax
docker run -d --name NAME --platform linux/amd64 -e MLFLOW_MODELS_WORKERS=NUMBER -p 127.0.0.1:HOST_PORT:8080 IMAGE
PartWhat it means
-e MLFLOW_MODELS_WORKERS=NUMBERHow many model server workers the container starts; without it, one per CPU

The build-docker reference: "By default, the number of uvicorn workers is set to CPU count. If you want to set a custom number of workers, you can set the MLFLOW_MODELS_WORKERS environment variable".

Prerequisites​

  • Docker Desktop, running.
  • An image of a model, built with mlflow models build-docker: How to Build a Docker Image for an MLflow Model. This guide uses house-values, a linear regression on scikit-learn's California housing data, built from models:/m-77c2a50ff243494e8f38920c85e80d19 with -n house-values --env-manager local.

Step 1: Run It with the Default​

Terminal
docker run -d --name house-api --platform linux/amd64 -p 127.0.0.1:5002:8080 house-values
Output
98d5cdf6b4b511a22e6f3e6f422ecfcd65c6f0f4e16cc82a9f1835b1f5bde398

Each worker loads the model before it answers, so give them time: wait until /ping answers and the log has an Application startup complete line for every worker. Then read the memory:

Terminal
docker stats --no-stream house-api
Output
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
98d5cdf6b4b5 house-api 6.45% 2.78GiB / 7.651GiB 36.34% 12.7kB / 13.3kB 3.17MB / 131kB 225

And count the workers in the log. PowerShell's Select-String does the job of grep:

Terminal
docker logs house-api 2>&1 | grep "Started server process"
Output
INFO: Started server process [30]
INFO: Started server process [28]
INFO: Started server process [26]
INFO: Started server process [29]
INFO: Started server process [27]
INFO: Started server process [32]
INFO: Started server process [33]
INFO: Started server process [34]
INFO: Started server process [25]
INFO: Started server process [31]

The default container in a terminal: docker stats showing 2.78GiB of 7.651GiB memory used and 225 processes, and ten Started server process lines.

Ten workers, one per CPU that Docker Desktop gives its Linux machine here, and 2.78 GiB of memory, more than a third of what the containers may use. 2>&1 is needed because the workers write their lines to the container's error stream.

Stop and remove it:

Terminal
docker stop house-api
docker rm house-api

Step 2: Run It with Two Workers​

Terminal
docker run -d --name house-api --platform linux/amd64 -e MLFLOW_MODELS_WORKERS=2 -p 127.0.0.1:5002:8080 house-values
Output
3d4c9d5f85bb910567a3e84a907297620e781858456c591b4b211146ef332db4

When both workers have started, read the memory again:

Terminal
docker stats --no-stream house-api
Output
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
3d4c9d5f85bb house-api 1.83% 766.6MiB / 7.651GiB 9.79% 2.79kB / 1.86kB 0B / 340kB 57

And count the workers:

Terminal
docker logs house-api 2>&1 | grep "Started server process"
Output
INFO: Started server process [25]
INFO: Started server process [24]

The two-worker container in a terminal: docker stats showing 766.6MiB of 7.651GiB memory used and 57 processes, and two Started server process lines.

Two workers and 766.6 MiB, under a third of the default. The model still answers:

Terminal
curl http://127.0.0.1:5002/invocations -H "Content-Type: application/json" --data @two_districts.json
Output
{"predictions": [4.13164982707675, 3.9766064387043585]}

two_districts.json holds two districts in the dataframe_split layout, as in How to Send a dataframe_split Request to an MLflow Model Server. The last digits differ from mlflow models serve on a Mac, 4.1316498270767426: the container runs linux/amd64 code under emulation.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving the default on a computer with many coresOne copy of the model per CPU, so memory grows with the number of coresSet MLFLOW_MODELS_WORKERS to what you need
Setting it after the container startedThe workers start once, when the container startsPass -e to docker run
Expecting mlflow models serve's default of one workerThe container counts CPUs insteadSet the number explicitly in both

Troubleshooting​

SymptomLikely causeFix
docker logs ... | grep or Select-String finds nothingThe workers write to the error streamAdd 2>&1 before |
/ping takes longer to answer with more workersEvery worker loads the model before it's readyFewer workers, or wait longer

FAQ​

How many workers does a model need?​

One per request you want to answer at the same time, as long as the memory allows; for a small model on your own computer, one or two.

Is it the same setting as mlflow models serve --workers?​

The same number, but serve defaults to 1 and the container to one per CPU. MLFLOW_MODELS_WORKERS is also serve's default for --workers.

Conclusion​

The house-values container started ten workers, one per CPU, and used 2.78 GiB. With -e MLFLOW_MODELS_WORKERS=2 it started two and used 766.6 MiB, and still answered the same predictions.

Additional Resources​