Video in progress
In Kubernetes, which metric do you tie a service's autoscaling to: CPU, request count or queue length?
CPU is a good signal only for services that are CPU-bound. For services that wait on I/O, request count or in-flight requests is better, and for workers that read from a queue, queue length or consumer lag is more accurate. And if a downstream dependency (the DB) is limited, scaling can take it down.
📖 Answer
The default HPA is CPU 70%. In many Spring services this doesn't work well: the service sits waiting on the DB or an external API, CPU is 20%, but the thread pool is full and latency is 3 seconds. The HPA says "everything is fine".
Which metric when:
- CPU — compute-heavy services (image processing, encryption, lots of JSON conversion).
- RPS or in-flight requests — if you know from a load test how many requests each pod handles well. Through Prometheus Adapter or KEDA.
- Queue length / consumer lag — the most accurate signal for workers. KEDA has ready-made scalers for Kafka lag, RabbitMQ queues and SQS.
# KEDA: by Kafka lag
triggers:
- type: kafka
metadata:
bootstrapServers: kafka:9092
consumerGroup: invoice-workers
topic: invoices
lagThreshold: "1000"
There's one ceiling with Kafka: if the number of consumers exceeds the partitions, the extras sit idle. Max replicas = the number of partitions.
A few more traps:
- The time until a new pod is ready. If a Spring application takes 40 seconds to come up, autoscaling won't catch a sharp spike — you need a minimum reserve (
minReplicas) and scaling ahead of time (for planned promotions). - Make scale-down slow (
scaleDown.stabilizationWindowSeconds) — otherwise the pod count saws up and down. - The dependency limit. People often forget this in interviews: you went from 10 pods to 40 — DB connections went up 4 times too. Sometimes the right answer is to cap max replicas based on the DB's capacity.
Sign up to mark studied questions and save your progress.