Sub topic 16
Scale to zero and its trade-offs
This chapter assumes you have read the earlier chapters on horizontal autoscaling, scaling signals, and cold start and warmup. You should know what a replica is, how an autoscaler picks a replica count from a metric, and why a new replica needs time before it can serve traffic.
What scale to zero means
Scale to zero lets an autoscaler set a model service's replica count to 0 when it has no traffic. No pods run, so no accelerator is reserved, and you pay nothing for compute until the next request arrives. When a request comes in, the platform starts a replica from nothing and serves the request once the replica is ready.
Most autoscalers keep at least one replica by default. The Kubernetes HorizontalPodAutoscaler (HPA) rejects minReplicas: 0 unless the HPAScaleToZero feature gate is enabled, and even then it needs object or external metrics. Tools such as KEDA, Knative, and managed serverless inference offerings support scale to zero directly.
Scale to zero is a cost decision that you pay for in latency. The first request after an idle period carries the full cold start.
Where the cost of the first request comes from
The earlier chapter on cold start and warmup covered the parts of a cold start. With scale to zero, all of them land on one waiting user, and a node may need to be created as well.
| Stage | Typical range | What affects it |
|---|---|---|
| Node provisioning (if no node is free) | 1 to 5 minutes for GPU nodes | Cloud capacity, node autoscaler speed |
| Image pull | 10 seconds to several minutes | Image size, registry location, node image cache |
| Model weight download and load | 5 seconds to many minutes | Model size, storage throughput, GPU memory transfer |
| Runtime initialization and warmup | 5 to 60 seconds | Kernel compilation, CUDA graph capture, first-batch warmup |
| Readiness probe passing | 5 to 30 seconds | Probe interval and thresholds |
A small CPU model may restart in a few seconds, and scale to zero then costs little. A multi-gigabyte GPU model can take minutes. No HTTP client will wait that long, so a request that arrives at zero replicas may time out before the first replica is ready.
What happens to the first request
There are three ways to handle a request that arrives when no replica exists. Each puts the delay in a different place.
- Buffer and hold. A proxy or activator accepts the request, triggers the scale-up, and forwards the request when a replica is ready. The caller sees one slow response. The proxy must hold connections open for as long as the cold start takes, and the client timeout must be longer than that.
- Fail fast and retry. The gateway returns 503 and triggers a scale-up. The client retries with backoff. This is simple, but every client must implement the retry correctly, and each user sees an error first.
- Queue asynchronously. The caller submits a job to a queue and gets the result later. The cold start becomes part of the normal job latency. This fits batch scoring and background tasks, and it does not fit interactive APIs.
The choice follows from your latency objective. If your service has a p99 target of 200 ms, no variant of scale to zero meets it for the first request after idle. Scale to zero fits workloads where the objective is expressed in seconds or minutes, or where the first request after idle may be excluded from the objective by agreement.
Scale-from-zero needs a signal that exists at zero
A common mistake is to scale on a metric that the model pods export themselves. Examples are the per-pod inference_queue_depth from the earlier HPA example, or GPU utilization. When there are zero pods, nothing exports the metric. The autoscaler sees no data, or a value of zero, and it never scales up.
At zero replicas, the signal must come from something that stays running:
- the gateway or ingress, for example requests received per second or in-flight requests
- a message queue, for example the number of pending messages
- an activator or proxy that counts held requests
The example below uses KEDA with a Prometheus query against the gateway, not against the model pods.
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: model-server
spec:
scaleTargetRef:
name: model-server
minReplicaCount: 0
maxReplicaCount: 20
pollingInterval: 15
cooldownPeriod: 600
triggers:
- type: prometheus
metadata:
serverAddress: http://prometheus.monitoring:9090
query: sum(rate(gateway_requests_total{service="model-server"}[1m]))
threshold: "20"
activationThreshold: "0.01"Two fields matter here:
activationThresholdis the value that moves the service from 0 to 1 replica. Above that point, thethresholdfield governs how many replicas run. Here, any traffic above 0.01 requests per second wakes the service, and then each replica is targeted at 20 requests per second.cooldownPeriodis how long KEDA waits after the last active signal before it scales from 1 to 0. This is your idle timeout.
Choosing the idle timeout
The idle timeout sets the balance between savings and cold starts. A short timeout releases the accelerator quickly but causes a cold start whenever traffic pauses longer than the timeout. A long timeout wastes money in the gaps and avoids more cold starts.
You can measure the effect on real traffic. The function below replays a list of request arrival times and counts how many requests would have hit a cold start for a given timeout.
def count_cold_starts(arrivals_s, idle_timeout_s):
"""Count requests that find zero replicas, given arrival times in seconds."""
cold = 0
last = None
for t in sorted(arrivals_s):
if last is None or t - last > idle_timeout_s:
cold += 1
last = t
return cold
arrivals = [0, 30, 400, 420, 2000]
for timeout in (60, 300, 900, 3600):
print(timeout, count_cold_starts(arrivals, timeout))For these arrivals, the output is 60 3, 300 3, 900 2, 3600 1. A 5-minute timeout does not help here, because two gaps are larger than 300 seconds. A 15-minute timeout avoids one cold start and a 1-hour timeout avoids two, but the service then stays up through long idle periods.
The function simplifies two things. It treats every request as activity that resets the timer, and it ignores how long requests take to serve. For a real decision, replay a week of production logs and include the daily and weekly cycle.
Deciding whether to use it
Compare the idle cost saved against the cold-start cost imposed. Scale to zero is a good fit when most of these hold:
- The service is idle for long, predictable stretches, such as overnight or between batch runs.
- Callers tolerate a delay of tens of seconds or more on the first request, or they use an asynchronous interface.
- The accelerator is expensive relative to the engineering cost of handling retries.
- The cold start is short, for example because the model is small and the image is cached.
- Many low-traffic models share a cluster, so zero-replica models free capacity for others.
It is a poor fit when:
- The service has a tight p99 objective that applies to every request.
- Traffic is steady or arrives in frequent small bursts, so the service would flap between 0 and 1.
- The cold start is several minutes, because clients and gateways time out first.
- A failed first request has a high business cost, such as payment fraud scoring.
A middle path is to set minReplicaCount: 1 and accept the cost of one idle replica. For many production services, one warm replica is the cheapest way to meet a latency objective. You can also use scheduled minimums: keep one replica during business hours and allow zero overnight.
Reducing the penalty
If you keep scale to zero, shrink the cold start:
- Bake model weights into the image, or cache them on a persistent volume or node-local disk, so the replica does not download them on every start.
- Use smaller, layered images and pre-pull them to nodes.
- Keep a small pool of warm spare nodes so scale-up does not wait for node provisioning.
- Run a warmup request during startup, and make the readiness probe pass only after warmup finishes. Otherwise, the first real request pays for kernel compilation.
- Load weights with a fast format and loader, and use storage with high sequential read throughput.
Each change lowers the penalty for the first request. None of them removes it.
Interaction with dynamic batching
Scale to zero changes what the batcher sees on the first request. The first replica starts with an empty queue and receives whatever backlog built up while it started. The batcher then forms full batches immediately, so the first requests after a cold start may see large batch sizes and high queue delay together. Watch the queue-delay metric during the first minute after a wake-up, and do not use that period to tune max_queue_delay.
Exercise
A GPU model service has a cold start of 90 seconds. Its clients time out after 30 seconds. It is idle overnight and busy during the day. The team wants to save money overnight without failing the first morning request. Which approach would you choose, and which signal should wake the service?
Show answer
Use scale to zero with a buffering proxy or activator whose hold time is longer than 90 seconds, and raise the client timeout or switch to fail-fast with retries that cover at least 90 seconds. A simpler alternative is a scheduled minimum: set minReplicas to 0 overnight and scale to 1 shortly before the morning load, so no user hits the cold start. The wake signal must come from a component that runs at zero replicas, such as the gateway request rate or a queue length, not a per-pod metric like inferencequeuedepth.