Skip to content

Scale on queues, databases, and schedules

Last updated on

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.

Your producers add work to a queue or database. The SCA platform checks the backlog on a schedule and sets the number of worker instances, from 0 to 10. The worker instances take the work from the queue themselves.

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.

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.

  • You have an application in an environment, and the sca.admin or sca.editor role in the project.
  • You have created an environment secret for each credential the scaler needs.

Set the rule through the SCA API by updating the application’s scaling configuration. This example applies the RabbitMQ rule described below:

Terminal window
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.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.type is the scaler type from Supported triggers.
  • parameters holds the scaler settings shown in the examples on this page.
  • secretsMapping maps an environment secret (secret) to the scaler authentication parameter (parameter).
  1. Open the application in the STACKIT Portal. With an empty source and a minimum of 0, its status is Idle.
  2. Add work to the source, for example messages to the queue.
  3. Within the check interval plus the time an instance needs to start, the status changes to Running and the instance count follows the backlog.

Runs one instance per 20 waiting messages on the orders queue, and none when the queue is empty.

Store the connection URL in the secret mapped to host:

secret value for host
amqps://user:password@your-broker:5671/vhost

The value is per instance. Modes other than QueueLength, such as MessageRate, need protocol set to http and access to the broker’s management API.

Useful when jobs are rows in a table.

query
SELECT COUNT(*) FROM jobs WHERE status = 'pending'
secret value for connection
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.

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.

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.

For every parameter a scaler accepts, see its page in the KEDA scaling documentation.

  • 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