Sub topic 10
Horizontal Autoscaling of Model Replicas
Assume you already know how a model runs behind an HTTP or gRPC endpoint, how to read p50 and p99 latency, and how to edit a Kubernetes Deployment. Horizontal autoscaling adds or removes identical model replicas as traffic changes.
A replica is one running copy of the model service. If one replica cannot process incoming requests quickly enough, Kubernetes can start more replicas and distribute requests across them.
How horizontal autoscaling works
A horizontal pod autoscaler (HPA) periodically reads a metric and compares it with a target. It changes the replica count when the measured value stays above or below that target.
For example, suppose each pod should handle at most four queued requests on average. If the average queue depth rises to eight, the HPA may double the number of pods. If the queue remains near zero, it can remove pods.
Common scaling signals include:
- Queue depth: Requests waiting for model execution. This reacts directly to unmet demand.
- Concurrency: Requests currently being processed. This works well when each replica has a known concurrency limit.
- CPU or accelerator utilization: How busy the hardware is. This is simple, but high utilization does not always mean requests are waiting.
- Request rate: Incoming requests per second. This can work when request cost is predictable.
For inference, queue depth or concurrency often gives a clearer signal than CPU. A model can consume little CPU while keeping an accelerator busy, or it can have high CPU usage without violating the latency objective.
Configure an HPA
The following HPA uses a custom per-pod queue metric. The metric adapter must expose inference_queue_depth to Kubernetes. The HPA keeps at least two pods and allows up to twenty.
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
scaleDown:
stabilizationWindowSeconds: 300minReplicas protects against cold starts and provides baseline capacity. maxReplicas limits cost and prevents an uncontrolled scaling event from consuming all cluster capacity.
The scale-down stabilization window keeps Kubernetes from removing pods immediately after a short traffic drop. This avoids flapping, where the service repeatedly adds and removes replicas. A zero-second scale-up window allows the service to react quickly to a growing queue.
Choosing a target
Set a metric target from measurements, not from the model's theoretical maximum. Load-test one replica and record throughput, queue depth, and p99 latency. Increase load until p99 approaches the service-level objective (SLO), which is the latency or availability goal for the service. Use the corresponding safe concurrency or queue depth as the target.
A low target leaves spare capacity but costs more. A high target uses hardware efficiently but gives the autoscaler less time to react. Cold-start time also matters: if a new replica needs 30 seconds to load model weights, the target must trigger scaling before the existing replicas become overloaded.
> callout warning > Do not scale only on average latency. A healthy average can hide a severe p99 queueing problem. Inspect queue depth, concurrency, and tail latency together.
Exercise
Exercise
A service has a target queue depth of 4, but its measured average queue depth stays at 12. What should you investigate before increasing maxReplicas?
Show answer
Verify that the queue metric is accurate, that new pods become ready, that the load balancer sends traffic to them, and that the cluster has enough accelerator capacity.