Support percentage-based scaling tiers relative to maxReplicaCount with a custom external scaler
Summary
I’d like to implement percentage-based scaling tiers relative to maxReplicaCount using a KEDA external scaler. The current HPA algorithm appears to make this difficult, so I’m looking for guidance or a recommended pattern.
Use case
We run a live streaming business. Our team does not want continuous dynamic replica adjustments during a live stream. Instead, we want discrete scaling tiers based on a Prometheus metric (live_users_count).
Example with maxReplicaCount: 10:
- When the metric reaches 10% of the target, use 50% of
maxReplicaCount→ 5 replicas - When the metric reaches 100% of the target, use 100% of
maxReplicaCount→ 10 replicas
In other words, we want the replica count to be a percentage of maxReplicaCount, driven by the metric percentage.
Current approach
We are trying to implement this with a custom gRPC external scaler. The scaler implements:
GetMetricSpec: returnsmetricNameandTargetSizeFloatGetMetrics: queries Prometheus and returns a metric valueIsActive: decides whether to activate from zero
However, HPA calculates desired replicas using its native algorithm:
desiredReplicas = ceil(currentReplicas * (currentMetricValue / targetMetricValue))This is based on the current replica count and a linear metric-to-target ratio. It does not support expressing the desired replica count as a percentage of maxReplicaCount.
Even when we return a very large metric value to force maxReplicaCount, HPA normalizes it or shows something like 1k/1k (avg), and no scaling occurs.
Problem
maxReplicaCount is not available inside HPA’s calculation. An external scaler can return metrics, but HPA still applies its own linear algorithm. There seems to be no direct way to say:
“When the metric is 10% of threshold, use 50% of
maxReplicaCount.”
We also looked at scalingModifiers, but it does not appear to have access to maxReplicaCount.
Questions
- Is there a recommended KEDA pattern to achieve tiered scaling based on
maxReplicaCount? - Can
scalingModifiersbe used for this, or is it limited to metric-based formulas? - Is it possible for an external scaler to return the desired replica count directly? If so, what target type and
TargetSizeshould be used to avoid HPA multiplying by the current replica count? - Are there any examples of mapping a metric to an absolute replica count through an external scaler?
Environment
- KEDA version: 2.20
- Kubernetes version: [please fill in]
- HPA API:
autoscaling/v2 - Prometheus metric:
live_users_count - Deployment:
yx-trade-biz(example)
Additional context
We want to avoid frequent dynamic adjustments during live streams; tiered scaling is preferred. If this is not currently supported, could it be considered as a feature request—either to support percentage-based scaling relative to maxReplicaCount, or to allow external scalers to return desired replicas directly?
I’m happy to share the external scaler code if that helps.
Source: kedacore/keda