Custom-metric HPA in Kubernetes: why CPU-based scaling fails for I/O-bound workloads
Kubernetes' Horizontal Pod Autoscaler, in its most common setup, decides how many replicas to run by watching pod CPU usage. For a service that does in-memory, CPU-heavy work, that signal holds up reasonably well. The problem shows up with a very different and very common kind of service: queue consumers, workers waiting on a database connection pool, services that spend most of their time blocked waiting on a response from somewhere else. There, CPU usage stays low even while the service is genuinely saturated, and the autoscaler never reacts, because it is watching the wrong signal.
We ran into this with a queue consumer feeding a processing pipeline. The message backlog grew steadily during traffic spikes, processing latency climbed along with it, and the CPU-based HPA sat still, because during the heaviest backlog the pods were spending nearly all their time waiting on a database response before they could acknowledge the next message. CPU was not the bottleneck. The number of messages waiting was, and that number lived nowhere the HPA could query.
Why CPU is a poor proxy for I/O load
CPU-based HPA assumes a direct relationship between pending work and processor cycles consumed. That relationship holds for compute-heavy services -serialization, compression, calculation- but breaks down for any service whose real work is waiting: waiting on a slow SELECT, waiting on an external service to respond, waiting for a connection to free up in the pool. A pod blocked on I/O reports low CPU usage even with a growing backlog behind it, because blocking does not cost processor cycles.
The practical consequence is that the autoscaler reacts late, or not at all. By the time CPU usage climbs enough to cross the configured threshold -usually because there are now so many messages in flight that the overhead of handling them starts to add up- the backlog has already been piling up for a while and the service is already running behind. Scaling at that point helps, but it arrives after the symptom, not before it.
What scaling on the real metric means
The fix is not to abandon the HPA, it is to change which metric it queries. Instead of CPU, you expose a metric that reflects pending work directly: queue depth, active connections against the pool limit, age of the oldest unprocessed message. That metric almost always exists somewhere already -the message broker exposes it natively, or it can be instrumented directly in the consumer's code- and the actual work is wiring it into the HPA.
Kubernetes has two reasonable paths for this: Prometheus Adapter and KEDA. They are not mutually exclusive, but they solve the problem with different approaches worth telling apart before picking one.
Prometheus Adapter: external metrics via the API
If Prometheus is already scraping the service or the broker, Prometheus Adapter exposes those series as external or custom metrics the HPA can query through Kubernetes' metrics API. The core piece of configuration is a rule that translates a PromQL query into a named metric:
rules:
- seriesQuery: 'rabbitmq_queue_messages_ready{queue="order-processing"}'
resources:
overrides:
namespace: {resource: "namespace"}
name:
matches: "rabbitmq_queue_messages_ready"
as: "pending_queue_messages"
metricsQuery: 'avg(rabbitmq_queue_messages_ready{queue="order-processing"})'
And the HPA references that external metric instead of CPU:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: order-consumer
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: order-consumer
minReplicas: 2
maxReplicas: 20
metrics:
- type: External
external:
metric:
name: pending_queue_messages
target:
type: AverageValue
averageValue: "30"
This does exactly what's needed: replica count now responds to the actual backlog, not to a side effect of the backlog on CPU usage. The cost is that you now maintain Prometheus, the adapter, and the PromQL translation rules as part of cluster infrastructure, and any change to the metric name the broker exposes silently breaks the rule until someone notices why the HPA stopped scaling.
KEDA: ready-made scalers for common sources
KEDA solves the same problem with less manual configuration when the metric source is one that already has a built-in scaler -RabbitMQ, Kafka, Azure Service Bus, Postgres, Redis, among many others-. Instead of writing a PromQL query and a translation rule, you declare a ScaledObject pointing straight at the matching scaler:
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: order-consumer
spec:
scaleTargetRef:
name: order-consumer
minReplicaCount: 2
maxReplicaCount: 20
triggers:
- type: rabbitmq
metadata:
queueName: order-processing
mode: QueueLength
value: "30"
authenticationRef:
name: rabbitmq-auth
KEDA generates and manages the HPA internally, so there is no HorizontalPodAutoscaler resource to hand-write. It also brings something Prometheus Adapter does not offer natively: scaling down to zero replicas when the queue is empty, useful for workers that do not need to run continuously.
What didn't work on the first attempt
The migration wasn't friction-free. The first attempt with Prometheus Adapter used too short an aggregation window in the PromQL query -a thirty-second average- and the result was an HPA that scaled up and down in short cycles every time the backlog had a normal burst and then settled, producing more pod churn than the service actually needed. Widening the aggregation window and raising stabilizationWindowSeconds in the HPA's behavior policy cut down that back-and-forth:
behavior:
scaleDown:
stabilizationWindowSeconds: 300
scaleUp:
stabilizationWindowSeconds: 60
With KEDA, the initial problem was different: the RabbitMQ scaler needs its own credentials via TriggerAuthentication, separate from the ones the consumer itself uses to connect to the queue, and the first configuration pointed at a secret that lacked the management-API read permissions the scaler needs, as opposed to just message-consumption permissions. The ScaledObject was created without any visible error in the resource's status; it only surfaced when checking the KEDA operator logs, which did show the authentication failure against RabbitMQ's management API.
What we check before moving a service to custom metrics
Before replacing CPU with a queue or I/O metric in a service's HPA, we first confirm the service genuinely spends most of its time waiting rather than processing, because if the real bottleneck is compute, changing the metric fixes nothing. We reach for KEDA when the source already has a built-in scaler and the use case benefits from scaling to zero; we reach for Prometheus Adapter when the metric is something custom-instrumented in the service itself and no native scaler exists for that source. Either way, we test the HPA's behavior policy against simulated traffic bursts before leaving it in production, because an HPA that scales up and down every minute creates more instability than it solves.