Sub topic 8
Autoscaling and scale to zero
This chapter assumes you can apply an InferenceService, know the difference between Serverless and RawDeployment mode, and understand Kubernetes Deployments and Services. It covers the fields that control replica counts, the metrics behind them, what changes between deployment modes, and what happens on a cold start.
Why model servers need their own scaling rules
CPU utilization is a poor signal for many model servers. A server waiting on a GPU or a slow model call can have idle CPUs while its request queue grows. KServe therefore scales on the load clients put on the service, usually the number of requests in flight, and treats CPU and memory as secondary options.
Two things do the work:
- In Serverless mode, Knative Serving runs the Knative Pod Autoscaler (KPA). It reads request metrics from a sidecar proxy in each pod.
- In RawDeployment mode, KServe creates a plain Deployment and a Kubernetes HorizontalPodAutoscaler (HPA).
The InferenceService fields look the same in both modes. The behavior behind them differs.
The scaling fields
These fields sit on the component (predictor, and also transformer) in the InferenceService spec.
| Field | Meaning |
|---|---|
| `minReplicas` | Lowest replica count. KServe's default is 1. In Serverless mode, 0 enables scale to zero. |
| `maxReplicas` | Upper bound on replicas. If it is unset or 0, the number of replicas is not capped by KServe. |
| `scaleMetric` | The metric to scale on: `concurrency`, `rps`, `cpu`, or `memory`. |
| `scaleTarget` | The value of that metric each replica should hold. |
| `containerConcurrency` | A hard limit on simultaneous requests per container. |
The following manifest scales on concurrency:
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: sklearn-iris
spec:
predictor:
minReplicas: 1
maxReplicas: 5
scaleMetric: concurrency
scaleTarget: 10
model:
modelFormat:
name: sklearn
storageUri: gs://kfserving-examples/models/sklearn/1.0/modelThe autoscaler aims for about 10 in-flight requests per replica. At an average of 35 concurrent requests it asks for 4 replicas, which is 35 divided by 10, rounded up. It never goes below 1 or above 5.
Soft target versus hard limit
scaleTarget and containerConcurrency are easy to confuse.
scaleTargetis a soft target. It tells the autoscaler when to add replicas. A traffic burst can briefly push a pod above it.containerConcurrencyis a hard limit. Knative's queue proxy will not send more than this many simultaneous requests to one container. Excess requests wait in the proxy until a slot frees up or the request times out.
Use containerConcurrency when your server breaks under parallel load. An example is a model that is not thread-safe, or one that runs out of memory if too many requests overlap. Leave it unset when the server handles parallelism well.
spec:
predictor:
containerConcurrency: 4
scaleMetric: concurrency
scaleTarget: 3
model:
modelFormat:
name: sklearn
storageUri: gs://kfserving-examples/models/sklearn/1.0/modelHere the target (3) is below the limit (4). The autoscaler adds pods before requests start queuing at the limit. If the target were higher than the limit, requests would queue before new pods appeared.
Choosing a scale metric
| Metric | Good fit | Caveat |
|---|---|---|
| `concurrency` | Most models, especially slow or GPU-bound ones | You must pick a target by measuring your own model. |
| `rps` | Fast models where each request takes a similar time | Requests per second ignores request duration. A slow request costs the same as a fast one. |
| `cpu` | CPU-bound models in RawDeployment mode | The target is a percentage of the CPU request. Without a CPU request on the container, the HPA cannot compute utilization. |
| `memory` | Rare, for servers whose memory grows with load | Memory often does not fall after load drops, so replicas may not scale back down. |
Concurrency is the usual starting point. To pick a target, load test one replica and find the concurrency at which latency starts to rise sharply. Then set scaleTarget somewhat below that point so there is headroom while new pods start.
Scale to zero in Serverless mode
Setting minReplicas: 0 lets Knative remove all pods when the service has no traffic.
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: sklearn-iris
spec:
predictor:
minReplicas: 0
maxReplicas: 5
scaleMetric: concurrency
scaleTarget: 10
model:
modelFormat:
name: sklearn
storageUri: gs://kfserving-examples/models/sklearn/1.0/modelWhen the last pod goes away, Knative's activator component takes over the route. A request that arrives with no pod behind it goes to the activator. The activator holds the request, asks the autoscaler to start a pod, and forwards the request once the pod is ready.
What the cold start costs
The first request after scale to zero waits for all of these steps:
- Pod scheduling. On a cluster with node autoscaling this may include provisioning a new node.
- Image pull, unless the node has the serving image cached.
- The storage initializer downloading the model from
storageUri. - The server loading the model into memory.
For a small scikit-learn model this may take several seconds. For a large model on a GPU node it can take minutes, and the request may time out first. Scale to zero suits internal tools, development environments, and models with many rarely used versions. It suits user-facing, latency-sensitive endpoints much less. For those, keep minReplicas: 1 or higher.
How quickly it scales down and up
Knative smooths its decisions with time windows. These are the usual defaults, which a platform administrator can change.
- The stable window is 60 seconds. The autoscaler averages the metric over this period before scaling down.
- When a service has had no traffic, the last pod stays for a grace period (30 seconds by default) before removal. Expect a service to hold its last pod for a minute or two after traffic stops.
- Panic mode uses a short window of about 6 seconds. If load reaches about twice the target, the autoscaler scales up quickly and will not scale down until the burst has passed.
You can override some behavior per service with Knative annotations, for example:
metadata:
name: sklearn-iris
annotations:
autoscaling.knative.dev/window: "120s"A longer window makes scaling calmer but slower to react.
RawDeployment mode
In RawDeployment mode there is no Knative. KServe creates a Deployment, a Service, and an HPA from the same fields. Two consequences follow:
- Scale to zero is not available with the standard HPA, which will not go below 1 replica.
minReplicas: 0does not give you scale to zero here. Some KServe releases support KEDA-based autoscaling for this case, so check the documentation for your version. cpuandmemoryare the supported metrics. Concurrency-based scaling relies on Knative's request proxy, so do not assumescaleMetric: concurrencyworks in RawDeployment mode. Check your release.
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: sklearn-iris
annotations:
serving.kserve.io/deploymentMode: RawDeployment
spec:
predictor:
minReplicas: 2
maxReplicas: 6
scaleMetric: cpu
scaleTarget: 70
model:
modelFormat:
name: sklearn
storageUri: gs://kfserving-examples/models/sklearn/1.0/model
resources:
requests:
cpu: "500m"
memory: 1GiThe CPU request matters. The HPA computes utilization as usage divided by the request, so 70 means about 350m per pod on average.
Watching scaling happen
Apply the manifest, then generate load with a tool such as hey. The command below reuses the input file and host variables from the first InferenceService chapter.
kubectl apply -f sklearn-iris.yaml
kubectl get pods -l serving.kserve.io/inferenceservice=sklearn-iris -w &
hey -z 60s -c 50 -m POST \
-host "${SERVICE_HOSTNAME}" \
-T "application/json" \
-D ./iris-input.json \
http://${INGRESS_HOST}:${INGRESS_PORT}/v1/models/sklearn-iris:predictWith 50 concurrent clients and a target of 10, expect the pod count to grow toward 5 in the first few seconds. After the load ends, the pods disappear after the stable window passes. With minReplicas: 0, the count falls to zero after the grace period.
Common errors
- Service never scales down to zero.
minReplicasis still at its default of 1, or a monitoring probe keeps sending requests. Check both. - Scaling stops at one replica in RawDeployment mode. The container has no CPU request, so the HPA reports unknown utilization. Set
resources.requests.cpu. - Requests time out after idle periods. The cold start takes longer than the request timeout. Raise
minReplicasor shrink the model load time. - Pods flap up and down. The target is too close to normal load, or the window is too short. Raise the target headroom or lengthen the window.
- Scaling caps at fewer replicas than expected. Check
maxReplicas, and then cluster quotas or unschedulable GPU pods.
Exercise
A Serverless InferenceService runs a model that handles at most 2 requests at the same time before running out of memory. You expect up to 40 simultaneous clients. Which fields do you set, and what values, so the service protects the model and still grows to meet demand?
Show answer
Set containerConcurrency: 2 as the hard limit so no pod ever receives more than 2 simultaneous requests. Set scaleMetric: concurrency with a scaleTarget at or just below the limit, for example 2 (or 1 for more headroom), so the autoscaler adds pods before requests queue. Set maxReplicas to at least 20, because 40 clients divided by 2 per pod needs about 20 pods. Choose minReplicas based on cold-start tolerance. A value above 0 avoids cold starts.
Exercise
A team deploys in RawDeployment mode with minReplicas: 0 and scaleMetric: cpu, and finds the service never drops below one pod and sometimes does not scale up. Give two likely causes.
Show answer
First, the standard HPA does not scale to zero, so minReplicas: 0 does not remove the last pod in RawDeployment mode. Second, the container probably has no CPU request, so the HPA cannot compute CPU utilization and may not scale. Setting resources.requests.cpu fixes the second problem. For scale to zero, use Serverless mode or an autoscaler such as KEDA if your KServe version supports it.