Sub topic 11
Stabilization, cooldown, and scaling limits
This chapter covers the policy layer of horizontal autoscaling: the rules that decide how fast replicas are added or removed, and how far they can go. It assumes you can read Kubernetes YAML, know what a Deployment and a HorizontalPodAutoscaler (HPA) are, and have already picked a scaling signal such as queue depth or concurrency. Choosing the signal tells the autoscaler what to react to. Policies decide how aggressively it reacts.
Why a raw signal is not enough
An autoscaler that sets replicas directly from the latest metric value will flap. Inference traffic is bursty, and a queue-depth metric can jump from 2 to 20 and back within seconds. Each replica also takes time to start (see the cold start chapter). If the autoscaler adds pods on every spike and removes them on every dip, you pay for startup repeatedly and the service loses capacity just before the next burst.
Three kinds of controls reduce this:
- Stabilization windows smooth the decision over time.
- Rate policies cap how many replicas change per period.
- Limits set a floor and a ceiling on the replica count.
The cooldown idea from older autoscalers is covered by the first two. The Kubernetes HPA does not have a single "cooldown" setting. It expresses the same idea through stabilization windows and policy periods.
How the HPA computes a replica count
The HPA control loop runs about every 15 seconds by default. For a metric of type Pods, it computes:
desiredReplicas = ceil(currentReplicas * currentMetricValue / targetMetricValue)If the average queue depth per pod is 8 and the target is 4, then 5 replicas become ceil(5 * 8 / 4) = 10. The result is clamped to minReplicas and maxReplicas. The HPA also skips changes when the ratio of current to target is within a tolerance (10% by default), which prevents tiny adjustments.
Stabilization windows
A stabilization window tells the HPA to look back over recent recommendations before acting. Each loop produces a recommended replica count. The HPA then picks from the recommendations in the window:
- For scale-down, it uses the highest recommendation seen during the window. The service only shrinks if every recent recommendation agreed it could.
- For scale-up, it uses the lowest recommendation seen during the window. The service only grows if every recent recommendation agreed it should.
Kubernetes defaults are 0 seconds for scale-up and 300 seconds for scale-down. That asymmetry is deliberate. Adding capacity late hurts the latency objective immediately. Removing capacity late costs only some extra spend.
The configuration from this tutorial's HPA example expresses it like this:
behavior:
scaleUp:
stabilizationWindowSeconds: 0
scaleDown:
stabilizationWindowSeconds: 300With a 300-second scale-down window, a traffic dip shorter than five minutes never reduces replicas. For models with slow startup, you may want a longer window. For cheap, fast-starting CPU models, a shorter one saves money.
Rate policies
Windows control when a change happens. Policies control how big it is. Each policy limits change within a period, and a selectPolicy field chooses how multiple policies combine.
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: Pods
pods:
metric:
name: inference_queue_depth
target:
type: AverageValue
averageValue: "4"
behavior:
scaleUp:
stabilizationWindowSeconds: 0
selectPolicy: Max
policies:
- type: Percent
value: 100
periodSeconds: 60
- type: Pods
value: 4
periodSeconds: 60
scaleDown:
stabilizationWindowSeconds: 300
selectPolicy: Min
policies:
- type: Pods
value: 1
periodSeconds: 120Reading this configuration:
- Scale-up may double the replica count or add 4 pods per minute, whichever is larger (
selectPolicy: Max). The Pods rule gives a fast start when you have 2 replicas. The Percent rule lets a large fleet grow quickly. - Scale-down removes at most 1 pod every 2 minutes. Combined with the 300-second window, the fleet shrinks slowly and only after demand has been low for a while.
selectPolicy: Minon scale-down chooses the most conservative option when several policies exist. Setting it toDisabledturns off that direction entirely.
The metric here is a custom metric. The HPA can only read it if a metrics adapter, such as the Prometheus adapter, exposes it through the custom metrics API.
Scaling limits
minReplicas and maxReplicas bound everything else. Set them from reasoning, not habit.
| Limit | What it protects | How to choose it |
|---|---|---|
| `minReplicas` | Latency during sudden bursts and availability during node failures | Cover your baseline load plus one replica of redundancy. Never 1 for a production service. |
| `maxReplicas` | Budget, GPU quota, and downstream systems | Peak expected load from capacity planning, plus margin. Check it against your GPU quota. |
Two failure modes come from badly chosen limits:
- A ceiling that is too low means the autoscaler saturates. Queues grow, p99 latency rises, and the HPA reports that it wants more replicas than it is allowed. Watch for the
ScalingLimitedcondition on the HPA. - A ceiling above real capacity means the HPA creates pods that stay
Pendingbecause the cluster has no free GPUs. The signal stays high and nothing improves. Pending pods also cost nothing but give a false sense of safety.
A third trap involves the floor. If minReplicas is lower than the number needed to meet your latency objective at normal load, every morning ramp-up passes through a period of poor latency while new replicas start.
Interaction with readiness and warmup
The HPA counts a pod's metrics only once it is ready, with some handling for pods that are still starting. A model server that reports ready before it finishes loading weights, or before the first slow inference compiles kernels, will receive traffic it cannot serve well. That raises the signal and triggers more scale-up. Use a readiness probe that succeeds only after the model is loaded and a warmup request has run. Then the autoscaler's picture of capacity matches reality.
Scale-up policies should also reflect startup time. If a new replica needs 90 seconds to become ready, adding 4 pods per 60 seconds is reasonable, but a reaction based on a one-minute-old signal is already behind. The window and the period together set how stale decisions can be.
A worked example of flapping and its fix
Consider a service with these properties: startup time of 90 seconds, a queue-depth target of 4, traffic that spikes every 3 minutes for 30 seconds.
Without policies (scale-down window 0), the sequence is:
- A spike raises queue depth. The HPA scales from 3 to 9 replicas.
- The spike ends 30 seconds later. The new pods are still starting.
- The HPA sees low queue depth and scales back to 3, killing pods that never served traffic.
- The next spike arrives with 3 replicas again.
With a 300-second scale-down window, step 3 never happens, because a recommendation of 9 appeared within the last five minutes. The 9 replicas stay, absorb the next spike, and shrink gradually when the pattern truly ends. You trade a few minutes of extra cost for stable latency.
Common errors
- Setting both windows to 0. The fleet follows every metric wiggle. Keep a nonzero scale-down window.
- Using a very long scale-up window. Capacity arrives late, and the latency objective is missed during ramps. Keep scale-up fast and limit the step size instead.
- Scaling on a noisy per-request metric. Prefer an average over a window, or a queue-depth gauge, over instantaneous values.
- Forgetting that policies stack with the window. A 1-pod-per-2-minute scale-down policy means going from 20 to 2 replicas takes at least 36 minutes after the window ends. If that is too slow, raise the rate.
- Running two autoscalers on one Deployment. An HPA and another controller that sets replicas will fight. Use one.
- Ignoring
maxReplicasduring a incident review. Check whether the HPA was pinned at the ceiling before blaming the signal.
Checking behavior
Inspect decisions with these commands:
kubectl describe hpa model-server
kubectl get hpa model-server --watchdescribe shows conditions such as AbleToScale, ScalingActive, and ScalingLimited, plus recent events with the reason for each change. Compare these against your queue-depth graph. If replica changes lag behind the metric by more than your window and period, the policies are doing what you configured. If changes do not match any configured rule, check the metrics adapter first.
Exercise
A model service has 10 replicas, a queue-depth target of 4 per pod, and a current average of 6 per pod. It uses a scale-down window of 300 seconds and a scale-down policy of 1 pod per 120 seconds. (a) What replica count does the HPA formula recommend now? (b) Later the average drops to 1 per pod and stays there. Roughly how long until the fleet reaches 3 replicas, assuming the recommendation has dropped below 10 for the whole window?
Show answer
(a) desired = ceil(10 6 / 4) = 15 replicas. (b) At an average of 1 per pod the formula recommends ceil(10 1 / 4) = 3, then keeps shrinking as the average per pod rises, but the target is 3 or fewer. After the 300-second window, the policy removes 1 pod per 120 seconds. Going from 10 to 3 needs 7 removals, which is about 840 seconds, so roughly 300 + 840 = 1140 seconds (about 19 minutes). The real number can differ slightly because the per-pod average rises as pods are removed, which changes the recommendation.
Exercise
Your service takes 90 seconds to become ready and has spikes that last 30 seconds. The HPA scales up on spikes and scales down immediately afterward, so new pods never serve traffic. Which setting do you change, and to what kind of value?
Show answer
Raise scale-down stabilizationWindowSeconds (for example to 300 or more) so the HPA keeps the higher recommendation from the spike in view. Optionally add a scale-down rate policy of one pod per period. Leave the scale-up window at 0 so capacity still arrives quickly.