Sub topic 3
Stabilization, cooldown, and scaling limits
This chapter assumes you know how batching trades latency for throughput, can apply Little's law, and can read a Kubernetes HorizontalPodAutoscaler (HPA) manifest. Latency targets here are p99 values.
Stabilization and cooldown
A metric that crosses its target on every sample makes the replica count oscillate, a pattern called flapping. A stabilization window makes the HPA act on recent history. For scale-down, the HPA picks the highest recommendation within the window, so replicas leave only after load stays low for the full window.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: model-server
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: model-server
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
behavior:
scaleUp:
stabilizationWindowSeconds: 0
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 50
periodSeconds: 60Scale-up should react quickly. Scale-down should be slow and capped. The policies block limits how many replicas can be removed per period, and minReplicas and maxReplicas bound cost and failure exposure.
Cold start and warmup
A new replica must load weights and run a warmup pass before it meets p99 targets. Use a readiness probe so traffic reaches a pod only after warmup finishes.
Scale to zero
Scale to zero removes idle cost. The first request after an idle period, however, pays the full cold start. This suits batch jobs and internal tools with relaxed latency. It does not suit user-facing services with tight SLOs.
Sizing replicas
Little's law gives in-flight requests: 200 req/s × 0.12 s = 24. With 8 concurrent requests per replica at 70% usable capacity, each replica handles 5.6. Dividing 24 by 5.6 and rounding up gives 5 replicas. Add headroom for scale-up lag.
Metrics to watch
- Queue depth per replica
- p99 latency against the SLO
- Batch fill ratio: average batch size divided by max batch size
- Accelerator utilization
- Replica count over time
- Cold start duration
Tuning together
Set the max queue delay so batch wait plus model time fits the p99 budget. Size replicas from the result, then tune the HPA window. Change one knob at a time and measure after each change.
Common errors
- Scaling on CPU for an accelerator-bound model, which hides real saturation.
- Setting scale-down stabilization to zero, which causes flapping.
- Sending traffic to pods before warmup, which causes latency spikes.
- Setting max replicas too low, which hides queue growth behind a ceiling.
Exercise
Exercise
A service receives 100 req/s at 200 ms average latency, with 4 concurrent requests per replica and a 70% usable-capacity target. How many replicas does Little's law suggest?
Show answer
In-flight requests are 100 × 0.2 = 20. Usable concurrency per replica is 4 × 0.7 = 2.8. Dividing 20 by 2.8 gives 7.14, which rounds up to 8 replicas.