Scale on queues, databases, and schedules
Zuletzt aktualisiert am
SCA can scale an application on events from outside it, not only on HTTP traffic: messages waiting in a queue, a stream’s consumer lag, the result of a database query, or the time of day. These custom scaling rules use the event scaler of KEDA, the open-source autoscaler SCA is built on.
A typical use is a background worker. It has no public access, takes jobs from a queue, and scales between zero instances when the queue is empty and several when work piles up.
How custom rules work
Section titled “How custom rules work”A custom rule has a scaler type, such as rabbitmq or postgresql, and the parameters that scaler needs.
- SCA checks the source on a periodicity defined by the KEDA trigger type.
- When the value goes above the activation value (0 by default), the application scales from zero to one instance.
- Above that, it runs one instance per target value, rounded up. With a target of 20 and 100 messages waiting, it runs 5 instances, up to the maximum you set.
- When the value stays at or below the activation value for 5 minutes, the application scales back to zero.
Keep credentials in secrets
Section titled “Keep credentials in secrets”Never put passwords or connection strings in rule parameters. Store them as environment secrets and map each one to the trigger’s authentication parameter, such as host, connection, or password, in the rule’s secretsMapping.
Add a custom rule
Section titled “Add a custom rule”Before you begin
Section titled “Before you begin”- You have an application in an environment, and the
sca.adminorsca.editorrole in the project. - You have created an environment secret for each credential the scaler needs.
Configure the rule
Section titled “Configure the rule”Set the rule through the SCA API by updating the application’s scaling configuration. This example applies the RabbitMQ rule described below:
curl -X PATCH \ "https://api.sca.eu01.stackit.cloud/v1beta/projects/<project-id>/regions/eu01/environments/<environment-id>/applications/<application-id>?updateMask=scaling" \ -H "Authorization: Bearer <token>" \ -H "Content-Type: application/json" \ -d @scaling.json{ "scaling": { "type": "SCALING_TYPE_AUTO", "autoScaling": { "minInstances": 0, "maxInstances": 10, "allowScaleToZero": true, "rules": [ { "name": "orders-queue", "type": "RULE_TYPE_CUSTOM", "customRule": { "type": "rabbitmq", "parameters": [ { "name": "protocol", "value": "amqp" }, { "name": "queueName", "value": "orders" }, { "name": "mode", "value": "QueueLength" }, { "name": "value", "value": "20" } ], "secretsMapping": [{ "secret": "rabbitmq-host", "parameter": "host" }] } } ] } }}customRule.typeis the scaler type from Supported triggers.parametersholds the scaler settings shown in the examples on this page.secretsMappingmaps an environment secret (secret) to the scaler authentication parameter (parameter).
Verify the rule
Section titled “Verify the rule”- Open the application in the STACKIT Portal. With an empty source and a minimum of 0, its status is Idle.
- Add work to the source, for example messages to the queue.
- Within the check interval plus the time an instance needs to start, the status changes to Running and the instance count follows the backlog.
Scale a worker on a RabbitMQ queue
Section titled “Scale a worker on a RabbitMQ queue”Runs one instance per 20 waiting messages on the orders queue, and none when the queue is empty.
| Setting | Value |
|---|---|
| Scaler type | rabbitmq |
protocol |
amqp |
queueName |
orders |
mode |
QueueLength |
value |
20 |
Secret: host |
Connection URL, see below |
Secret: tls |
enable, if your broker uses amqps |
Store the connection URL in the secret mapped to host:
amqps://user:password@your-broker:5671/vhostThe value is per instance. Modes other than QueueLength, such as MessageRate, need protocol set to http and access to the broker’s management API.
Scale on pending jobs in PostgreSQL
Section titled “Scale on pending jobs in PostgreSQL”Useful when jobs are rows in a table.
| Setting | Value |
|---|---|
| Scaler type | postgresql |
query |
The query below |
targetQueryValue |
10 |
Secret: connection |
Connection string, see below |
SELECT COUNT(*) FROM jobs WHERE status = 'pending'postgresql://user:password@your-host:5432/app?sslmode=require- The query must return a single number. If it can return null, wrap it in
COALESCE(..., 0). - The query runs at every check. Optimize query execution, index the columns it filters on, and use a database user that can only read that table.
Scale on a Redis
Section titled “Scale on a Redis”| Setting | Value |
|---|---|
| Scaler type | redis |
address |
your-host:6379 |
listName |
tasks |
listLength |
10 |
enableTLS |
true |
Secret: password |
Your Redis password |
Keep instances warm during business hours
Section titled “Keep instances warm during business hours”A schedule rule sets a number of instances for a time window. With a minimum of 0, the application scales to zero outside the window, 5 minutes after it ends.
| Setting | Value |
|---|---|
| Scaler type | cron |
timezone |
Europe/Berlin |
start |
0 8 * * 1-5 |
end |
0 18 * * 1-5 |
desiredReplicas |
2 |
This keeps two instances running on weekdays from 8:00 to 18:00, so users in that window never wait for an instance to start. start and end must differ. Do not set desiredReplicas to 0; use a minimum of 0 instances to reach zero.
Supported triggers
Section titled “Supported triggers”| Scaler type | Source |
|---|---|
rabbitmq |
RabbitMQ queue length or rates |
postgresql |
PostgreSQL query |
mysql |
MySQL query |
mongodb |
MongoDB query |
redis |
Redis or Valkey list length |
kafka |
Kafka consumer lag |
nats-jetstream |
NATS JetStream consumer backlog |
cron |
Time windows |
For every parameter a scaler accepts, see its page in the KEDA scaling documentation.
Considerations
Section titled “Considerations”- Changes in your source are picked up at the next check, plus the time an instance needs to start.
- Several different rules can be applied to the same application. The rule with the highest instance result is executed