Sub topic 3
Packaging the Service in a Container
Assume you know basic Python, HTTP, JSON, and scikit-learn model training. The service from the previous chapter has main.py and model.joblib.
Packaging the service in a container
A container packages the application, its dependencies, and the model artifact into a repeatable image. The image can run on a laptop, a test server, or a cloud platform.
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY main.py model.joblib ./
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]Build and run it with:
docker build -t iris-api .
docker run --rm -p 8000:8000 iris-apiCopying requirements.txt before the source files lets Docker reuse the dependency layer when application code changes.
Batch prediction and latency
A single-request endpoint minimizes waiting for one prediction. A batch endpoint accepts several instances and calls the model once. Batching often lowers the average cost per row, but a request may wait while the batch fills.
Use single predictions for interactive requests. Use batches for jobs, uploads, or pipelines.
Concurrency and blocking work
FastAPI can handle many connections, but scikit-learn prediction is synchronous, or blocking, work. A worker cannot process another task while it is performing that call. Keep predictions fast, use multiple workers when appropriate, and measure memory use because each worker may load its own model.
Versioning models and the API
Store model artifacts with explicit versions, such as iris-model-2026-11-28.joblib. Include the model version in health or response metadata when clients need to identify it. Version breaking API changes with a path such as /v2/predict.
Logging and monitoring predictions
Log request duration, endpoint, status, model version, and failure reason. Do not log raw personal data or sensitive feature values. Monitor error rate, latency percentiles, request volume, and prediction distributions. A shift in those distributions can reveal changing input data.
Authentication and abuse protection
Protect prediction endpoints with authentication, such as an API key or token. Apply request-size limits, rate limits, and input validation. Use HTTPS outside a trusted local network.
Scaling out with multiple replicas
Run multiple identical containers behind a load balancer when traffic exceeds one instance's capacity. Keep containers stateless. Store models in the image or retrieve immutable artifacts during deployment, rather than changing a running container.
When to use a dedicated model server
A hand-written FastAPI service works well for custom validation and simple models. A dedicated model server is useful when you need standard model loading, batching, GPU scheduling, metrics, or support for several model formats. Choose it when those features outweigh the extra operational complexity.
Exercise
Why can increasing the number of workers increase memory usage?
Show answer
Each worker may load its own copy of the model, so memory usage can grow with the worker count.