Sub topic 15
Scaling out with multiple replicas
This chapter assumes you have the FastAPI service from earlier chapters: a model loaded at startup, a /predict endpoint, a /health endpoint, and a Docker image. It also assumes you know the basics of containers.
What a replica is
A replica is one running copy of your service. Scaling out means running several replicas of the same image and spreading requests across them. Scaling up means giving one process a bigger machine.
Scaling out helps in two ways. Throughput grows, because more processes score requests at the same time. Availability improves, because one crashed replica doesn't take the service down.
What your service must satisfy
Replicas only work if the service is stateless. Any replica must be able to answer any request. Check these points:
- No request state in memory. If one replica remembers something between calls, the next call may reach a replica that doesn't.
- Same model everywhere. Every replica loads the same
model.joblibbaked into the same image. A mixed rollout can return different answers for the same input. - Memory multiplies. Each replica holds its own model copy. Ten replicas of a 2 GB model need about 20 GB in total.
- Startup is not instant. Loading the model takes time, so a new replica must not receive traffic until it is ready.
Processes on one machine
The simplest form of scaling out is several Uvicorn worker processes in one container:
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4Each worker is a separate process with its own copy of the model, loaded in its own lifespan startup. Four workers use roughly four times the model memory. A good starting point is one worker per CPU core, since model inference is usually CPU-bound.
Replicas across machines
For multiple containers, an orchestrator such as Kubernetes runs the replicas and balances traffic. This Deployment runs three copies and uses /health to decide when a replica can receive traffic:
apiVersion: apps/v1
kind: Deployment
metadata:
name: iris-api
spec:
replicas: 3
selector:
matchLabels:
app: iris-api
template:
metadata:
labels:
app: iris-api
spec:
containers:
- name: iris-api
image: iris-api:1.0
ports:
- containerPort: 8000
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
memory: "1Gi"
readinessProbe:
httpGet:
path: /health
port: 8000
initialDelaySeconds: 5
periodSeconds: 5The readiness probe calls /health repeatedly. The balancer sends traffic only to replicas that pass. Because the earlier health check reports model_loaded, you may want it to return an error status while the model is missing. Otherwise a replica can pass the probe before it can predict.
Common errors
- Setting memory limits below the model size. The container is killed when it exceeds its limit, then restarts in a loop.
- Scaling without measuring. If latency comes from one slow model call, more replicas raise throughput but don't make a single request faster.
- Ignoring cold starts. Autoscaling adds replicas that need seconds to load the model. Keep a minimum replica count for steady traffic.
- Writing local files. Logs or caches saved inside a replica disappear when it restarts. Send logs to standard output instead.
Exercise
Your model uses 1.5 GB of memory. You run 2 containers, each with 3 Uvicorn workers. Roughly how much memory do the model copies use in total, and why?
Show answer
About 9 GB. Each worker is a separate process that loads its own copy of the model, so 2 containers times 3 workers gives 6 copies, and 6 times 1.5 GB is 9 GB.