Inferensys

Glossary

Horizontal Pod Autoscaling (HPA)

A Kubernetes mechanism that automatically scales the number of running model replicas based on observed CPU utilization or custom metrics to maintain throughput under varying load.
ML engineer running AI model benchmarks, performance charts on multiple screens, late night home office setup.
KUBERNETES SCALING

What is Horizontal Pod Autoscaling (HPA)?

A core Kubernetes mechanism for automatically adjusting the number of running application replicas to match real-time demand.

Horizontal Pod Autoscaling (HPA) is a Kubernetes control loop that automatically scales the number of pods in a deployment, replica set, or stateful set based on observed resource utilization or custom metrics. It continuously monitors metrics like CPU usage or memory consumption against target thresholds defined by the operator, executing a control loop to add or remove replicas to maintain stable, performant service delivery under fluctuating load.

For latency-optimized model serving, HPA is critical for maintaining throughput SLAs. By integrating with the custom.metrics.k8s.io API, HPA can scale inference replicas based on application-specific metrics such as request queue depth, GPU utilization, or prediction latency. This ensures the serving infrastructure dynamically rightsizes itself, provisioning new pods to absorb traffic spikes and terminating idle replicas to conserve compute resources during low-demand periods.

KUBERNETES SCALING MECHANICS

Key Features of Horizontal Pod Autoscaling

Horizontal Pod Autoscaling (HPA) is a Kubernetes control loop that dynamically adjusts the number of running pods in a deployment or replica set based on observed resource utilization or custom metrics, ensuring model serving infrastructure maintains throughput under fluctuating inference loads.

01

Resource Metric-Driven Scaling

HPA natively monitors CPU utilization and memory consumption per pod against defined target thresholds. When the average CPU usage across all pods exceeds 80% of the requested limit, the controller calculates the desired replica count using the formula: desiredReplicas = ceil(currentReplicas * (currentMetricValue / desiredMetricValue)). This ensures each pod maintains sufficient headroom to handle request spikes without throttling.

  • Target utilization: Typically set to 70-80% for CPU, leaving buffer for sudden traffic bursts
  • Stabilization window: Default 5-minute cooldown prevents rapid scale-down oscillations after brief load spikes
  • Resource requests: Must be explicitly defined on pod templates; HPA cannot scale pods without known resource baselines
< 30 sec
Typical Scale-Up Response
5 min
Default Scale-Down Cooldown
02

Custom Metrics API Integration

Beyond CPU and memory, HPA supports custom metrics exposed through the custom.metrics.k8s.io API, enabling scaling decisions based on application-specific signals like requests-per-second, queue depth, or inference latency. The Prometheus Adapter is commonly deployed to translate Prometheus queries into Kubernetes metrics, allowing HPA to scale model replicas based on P99 prediction latency or active gRPC stream count.

  • Pod metrics: Per-pod values like http_requests_per_second averaged across all replicas
  • Object metrics: Cluster-level signals like message_queue_length from an external queue
  • External metrics: Non-Kubernetes signals such as cloud provider load balancer metrics or custom monitoring systems
03

Scale-To-Zero and Idle Cost Management

While native HPA cannot scale below minReplicas (typically 1), integration with KEDA (Kubernetes Event-Driven Autoscaling) enables true scale-to-zero behavior. KEDA acts as a metrics server that can scale deployments to zero replicas when no inference requests are present and rapidly scale back up when traffic arrives. This is critical for GPU-accelerated model serving where idle instances incur significant cloud compute costs.

  • Cold start mitigation: Combine with lightweight warm-up containers or model pre-loading scripts
  • Activation lag: Expect 5-15 seconds for pod initialization plus model weight loading time
  • Cost optimization: Reduces GPU compute costs by 60-80% for bursty inference workloads with long idle periods
60-80%
GPU Cost Reduction
5-15 sec
Cold Start Activation
04

HPA Algorithm and Tuning Parameters

The HPA controller uses a reconciliation loop that evaluates metrics every 15 seconds by default. The scaling algorithm applies hysteresis to prevent thrashing: it scales up immediately when thresholds are exceeded but scales down conservatively using a stabilization window. Key tunable parameters include --horizontal-pod-autoscaler-sync-period, --horizontal-pod-autoscaler-downscale-stabilization, and per-HPA behavior policies introduced in Kubernetes 1.18.

  • Scale-up policy: Can configure stabilizationWindowSeconds (default 0) and selectPolicy (Max, Min, Disabled)
  • Scale-down policy: Default 300-second stabilization prevents premature contraction after transient dips
  • Tolerance: 10% tolerance band around target metric prevents unnecessary scaling from minor fluctuations
05

Readiness Probe Coordination for Safe Scaling

HPA scaling decisions must coordinate with readiness probes to prevent routing traffic to pods still loading model weights. A newly scaled pod should only report as Ready after completing weight initialization and passing a health check. Without proper probe configuration, HPA can create pods that receive traffic before they can serve predictions, causing HTTP 503 errors and cascading failures under load.

  • Startup probe: Introduced in Kubernetes 1.16, allows extended initialization time before liveness/readiness checks begin
  • Readiness gate: Custom condition that blocks pod readiness until model weights are fully loaded into GPU memory
  • Graceful shutdown: Configure terminationGracePeriodSeconds to allow in-flight inference requests to complete before pod termination during scale-down events
06

Cluster Autoscaler Integration for Node-Level Scaling

HPA operates at the pod level, but when all nodes are at capacity, new pods remain in Pending state. The Cluster Autoscaler complements HPA by monitoring for unschedulable pods and automatically provisioning additional worker nodes. For GPU-accelerated inference, this requires configuring node selectors, tolerations, and GPU resource requests so the autoscaler provisions nodes with the correct accelerator type.

  • Node provisioning latency: Expect 2-5 minutes for new GPU nodes to become available in major cloud providers
  • Overprovisioning: Deploy low-priority placeholder pods to pre-warm spare capacity and reduce scheduling latency
  • GPU-aware scheduling: Use nvidia.com/gpu resource requests to ensure HPA-scaled pods land on GPU-equipped nodes only
HPA DEEP DIVE

Frequently Asked Questions

Clear, technical answers to the most common questions about using Horizontal Pod Autoscaling to maintain low-latency model serving under dynamic load.

Horizontal Pod Autoscaling (HPA) is a Kubernetes control loop that automatically scales the number of running Pods in a Deployment or StatefulSet based on observed resource metrics. The HPA controller queries a metrics server at regular intervals (default 15 seconds), compares the current metric value against a target threshold you define, and calculates the desired replica count using a simple ratio formula: desiredReplicas = ceil[currentReplicas * (currentMetricValue / desiredMetricValue)]. For a latency-optimized model serving system, this means your inference endpoints can self-heal under load: when prediction latency spikes due to a traffic surge, HPA provisions additional model replicas to absorb the queue, and when traffic subsides, it scales down to conserve GPU compute costs. The core components are the metrics source (metrics-server, Prometheus, or custom metrics API), the HPA resource defining the target, and the controller manager executing the scaling decisions.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.