Messages pile up while CPU stays low The orders consumer reads from a Kafka topic. Most of its time goes to waiting on the database, so CPU stays near 20% even when the consumer group lags 5,000 messages behind.
A CPU-based HPA sees nothing to do. The number that matters is the backlog: how much work is waiting outside the Pods.
A ScaledObject names the target and its triggers You write a ScaledObject. scaleTargetRef points to the Deployment. triggers lists event sources: here kafka with bootstrapServers, consumerGroup, topic and lagThreshold. minReplicaCount defaults to 0 and maxReplicaCount to 100.
Other triggers follow the same shape: queue length for RabbitMQ or SQS, a Prometheus query with a threshold, or a cron window. KEDA ships many scalers like these.
Key idea One ScaledObject per workload. Do not also attach your own HPA to the same Deployment: two controllers would fight over replicas.
KEDA creates an HPA and serves it the metric The KEDA operator sees the ScaledObject and creates an HPA named keda-hpa-orders with an External metric. The KEDA metrics server registers as the external metrics API, so when the HPA asks for the value, it gets the current lag.
The HPA does the usual math. With an AverageValue target of 100 messages per replica, a lag of 5,000 asks for 50 replicas. For Kafka, KEDA also caps replicas at the number of partitions unless allowIdleConsumers is set, since extra consumers would sit idle.
Key idea KEDA does not replace the HPA. It feeds it. kubectl describe hpa keda-hpa-orders shows the value it got.
Activation decides 0 or 1, the threshold decides 1 to N Each trigger has two numbers. The activation threshold (activationLagThreshold for Kafka, activationThreshold for Prometheus, default 0) decides whether the workload should run at all. The operator itself handles 0 ↔ 1, because an HPA cannot go to zero.
The scaling threshold (lagThreshold) is the target per replica the HPA uses between 1 and maxReplicaCount. Activation only fires when the metric is above the value, not equal to it.
Key idea Set activation above the background noise, for example a few stray messages, so one message at night does not wake a Pod every few minutes.
Scale to zero saves Pods, the next message pays for it At night the lag stays at 0. After cooldownPeriod (300 seconds by default) with no active trigger, the operator sets replicas to 0. The HPA goes inactive.
When a message arrives, the operator sees it on its next poll (pollingInterval, 30 seconds by default) and sets replicas to 1. Then the Pod has to be scheduled, pull its image and start. Messages wait safely in the topic meanwhile. Plain HTTP traffic has no such buffer, which is why HTTP scale to zero needs something in front, like the KEDA HTTP add-on.
cooldownPeriod only covers the last step to zero cooldownPeriod applies only when going to 0. Scaling down from 12 to 3 is the HPA's job, and it follows the HPA scale-down stabilization window instead.
To tune that, set advanced.horizontalPodAutoscalerConfig.behavior in the ScaledObject. KEDA copies it into the HPA it manages. Editing the generated HPA directly does not stick.
Watch out Two clocks: cooldownPeriod for 1 → 0, HPA behavior for N → 1. Check both when Pods disappear too fast or linger too long.
Several triggers: the highest demand wins Add a cron trigger with timezone, start, end and desiredReplicas: keep 4 replicas from 08:00 to 10:00 before the morning batch lands. Add a prometheus trigger on request latency too.
Each trigger becomes one metric on the same HPA. The HPA computes a replica count per metric and takes the largest, so a quiet queue does not cancel the cron floor.
TriggerAuthentication keeps credentials out of the ScaledObject The Kafka cluster needs SASL credentials. A TriggerAuthentication maps trigger parameters such as sasl, username and password to keys in a Secret with secretTargetRef. The trigger points to it with authenticationRef.
ClusterTriggerAuthentication does the same for all namespaces. Instead of a Secret, it can also use a cloud workload identity through podIdentity.
Key idea If the ScaledObject is not Ready, check the auth first. A wrong key name looks the same as an unreachable broker.
ScaledJob runs one Job per message for long work Video encoding takes 20 minutes per message. If the HPA scales a Deployment down mid-encode, the work is cut off. A ScaledJob avoids that: it holds a Job template, and KEDA creates Jobs from the queue length, up to maxReplicaCount.
Each Job takes one message, finishes it, and exits. Nothing is scaled in, because a Job is never removed while it is still running work. Old Jobs are cleaned up by successfulJobsHistoryLimit and failedJobsHistoryLimit.
The broker is unreachable, and the metric disappears The Kafka brokers go down for maintenance. The scaler errors on every poll, the ScaledObject shows Ready=False, and the HPA gets no value, so it stops scaling on that trigger. Replicas stay frozen at whatever they were.
With spec.fallback (failureThreshold 3, replicas 6), KEDA serves a value that makes the HPA go to 6 replicas after 3 failed polls in a row. behavior static is the default. Others, like currentReplicasIfHigher, keep the current count when that is safer. Fallback works for Value and AverageValue triggers except CPU and memory, and not for ScaledJob.
Watch out Pick fallback replicas as the count that is safe without knowing the load. Alert on the scaler errors, not just on the backlog.
Scale on the work waiting, and plan for zero and for errors A ScaledObject lists triggers. The operator polls them, handles 0 ↔ 1 with the activation threshold, and creates an HPA that scales 1 ↔ N on the threshold through the KEDA metrics server.
Scale to zero trades cost for a cold start. cooldownPeriod guards only the step to zero. ScaledJob fits long one-message tasks. TriggerAuthentication holds secrets, and fallback decides what happens when the source is gone.
Summary Check kubectl get scaledobject for READY and ACTIVE, then kubectl describe hpa for the value the HPA really used.