Horizontal Pod Autoscaler (HPA) vs. Vertical Pod Autoscaler (VPA): Algorithms, KEDA custom metrics, and avoiding scaling conflicts.
Autoscaling Architecture & Algorithms
Kubernetes provides two mechanisms for workload autoscaling: horizontal scaling (adding more Pod replicas) and vertical scaling (adjusting CPU and memory requests/limits).
1. Horizontal Pod Autoscaler (HPA):
[ Metrics Server / KEDA ] ---> [ HPA Controller ] ---> Scales Replicas: [Pod] [Pod] [Pod]
2. Vertical Pod Autoscaler (VPA):
[ Metrics History (Prometheus) ] ---> [ VPA Recommender ] ---> Updates Pod Spec: [CPU/Mem ++]
1. HPA Mathematical Scaling Formula
The HPA controller queries the metrics API and executes this deterministic formula every 15 seconds: $$\text{DesiredReplicas} = \left\lceil \text{CurrentReplicas} \times \left( \frac{\text{CurrentMetricValue}}{\text{TargetMetricValue}} \right) \right\rceil$$ Example: If current replicas = 4, current CPU utilization = 80%, and target is 50%: $$\text{DesiredReplicas} = \lceil 4 \times (80 / 50) \rceil = \lceil 6.4 \rceil = 7 \text{ replicas}$$
2. Event-Driven Autoscaling with KEDA
Standard HPA is limited to CPU/Memory from Metrics Server. KEDA (Kubernetes Event-driven Autoscaling) allows Pods to scale from 0 to N based on external event queues (e.g., Kafka consumer lag, AWS SQS queue depth, Redis queue length):
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: order-processor-scaler
spec:
scaleTargetRef:
name: order-processor
minReplicaCount: 0
maxReplicaCount: 50
triggers:
- type: aws-sqs-queue
metadata:
queueURL: https://sqs.us-east-1.amazonaws.com/123456789/orders
queueLength: "50"
3. Preventing HPA and VPA Conflicts
Critical Rule: Never run HPA and VPA simultaneously on the same metric (e.g., CPU or Memory).
If CPU spikes, HPA tries to add more replicas while VPA tries to restart pods with larger CPU requests. The two controllers fight each other in an infinite oscillation loop.
Best Practice: Use VPA in
mode: "Off"(recommendation only) or use VPA for memory and HPA on custom business metrics / CPU.