Lesson 3 of 3 Deploying Applications to Azure Container Apps

Scaling Container Apps with KEDA

This lesson explains replica limits, how KEDA scale rules make decisions, the types of rule, and how scalers authenticate to event sources.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the effect of minimum and maximum replicas.
  • Describe KEDA polling and cooldown behaviour.
  • Choose a scale rule type for a workload.
  • Describe scaler authentication with secrets or managed identities.

Replicas and scale limits

A container app runs as a number of replicas, each a copy of the revision's containers. The revision's scale settings set a minimum and a maximum number of replicas and a list of scale rules. When no rule is defined, a default HTTP rule applies, with a minimum of 0 and a maximum of 10 replicas. The maximum can be raised up to 1,000, subject to the environment's core quota.

The minimum decides the cost and latency trade-off. With a minimum of 0, an idle app costs nothing for compute, but the first request after idling waits while a replica starts, which is a cold start. With a minimum of 1 or more, a replica is always ready. Background workers usually scale to zero; interactive APIs with latency targets usually keep one replica. Because scale settings are in properties.template, changing them creates a new revision.

Scenario questions

Minimum replicas

Exam skills 1.2.2

Choose the minimum replica count for each workload.

Log in to try this activity Interactive activities are free with an account.

How KEDA makes scaling decisions

KEDA is an open-source project that extends Kubernetes autoscaling with scalers, components that read a metric from an event source, such as the number of messages in a queue. Container Apps runs KEDA for you and exposes its scalers through scale rules. For each rule, the scaler reports a metric and a target value per replica, and the platform sets the replica count so that each replica handles about the target amount.

Two timing settings shape the behaviour. KEDA checks event sources every 30 seconds by default (the polling interval), so scaling from zero can take up to that long to begin. After the last event, it waits 300 seconds by default (the cooldown period) before scaling a revision down to its minimum, which prevents rapid flapping between zero and one. When several rules are defined, the platform uses the rule that asks for the most replicas.

  1. Poll

    Every polling interval, each scaler reads its metric, such as active messages in a queue.

  2. Compare

    The metric is divided by the rule's target value per replica.

  3. Scale out

    Replicas are added up to the maximum when demand exceeds capacity.

  4. Scale in

    Replicas are removed as demand falls; after the cooldown with no events, the revision scales to its minimum.

The following diagram shows a queue-based scale rule for an embedding worker and how its replica count follows the queue.

Event-driven scaling of an embedding worker. A custom azure-servicebus scale rule watches the queue's message count, and KEDA adds replicas up to the maximum of 50 when a burst of 10,000 messages arrives, then returns to zero after the queue empties and the cooldown passes. The scaler and the application authenticate with a managed identity, so no connection string is stored.
The figures are illustrative.
Fill in the value

KEDA numbers

Exam skills 1.2.2

Recall the default values and the arithmetic behind KEDA scaling.

Log in to try this activity Interactive activities are free with an account.

HTTP, TCP and custom rules

Container Apps supports three kinds of scale rule, and the right one depends on how the work arrives.

Rule typeMetricTypical workload
HTTPConcurrent HTTP requests per replica (default target 10)APIs and web front ends
TCPConcurrent TCP connections per replicaNon-HTTP protocols such as gRPC over TCP ingress
CustomAny metric that a supported KEDA scaler readsQueue workers, stream consumers, scheduled bursts

Custom rules name a KEDA scaler type and its metadata. Common scalers in AI back ends are azure-servicebus (active messages in a queue or subscription, with a messageCount target), azure-queue (Azure Storage queues, with a queueLength target), azure-eventhub (unprocessed events per partition) and cron (a fixed replica count during a time window). The metadata identifies the resource, such as queueName and namespace.

Match the pairs

Choosing a scale rule

Exam skills 1.2.2

Match each workload with the scale rule or KEDA scaler that fits it.

Log in to try this activity Interactive activities are free with an account.

Authenticating scalers

A scaler must read metrics from the event source, so it needs credentials separate from the application's own. There are two options. A secret reference maps an app secret, such as a Service Bus connection string, to a scaler parameter. A managed identity, supported for Azure scalers such as Service Bus, Storage queues and Event Hubs, lets the scaler authenticate with the app's system-assigned or a user-assigned identity, with no secret; the identity needs a data role that can read the metric, such as Azure Service Bus Data Owner on the namespace, because reading queue metrics requires management rights on the entity.

The following command adds a Service Bus scale rule that targets five messages per replica and authenticates with the system-assigned identity. The worker scales to zero when the queue is empty and to at most 20 replicas.

az containerapp update --resource-group RG --name embed-worker \
  --min-replicas 0 --max-replicas 20 \
  --scale-rule-name orders-queue --scale-rule-type azure-servicebus \
  --scale-rule-metadata queueName=orders namespace=SBNAMESPACE messageCount=5 \
  --scale-rule-identity system

Scaling is not message delivery

A scale rule only decides how many replicas run. The application code inside each replica must still connect to the queue and receive messages with its own SDK client and its own permissions; KEDA does not deliver messages to the container.

Worked example

Scaling an embedding worker

A worker reads document IDs from a Service Bus queue, generates embeddings and stores them in Cosmos DB. Each replica can process about 20 messages a minute. The queue is empty most nights but receives bursts of 10,000 messages after daily imports. The company wants to pay nothing when the queue is empty and to clear bursts quickly, without storing connection strings.

The worker should run in Container Apps with minimum replicas 0, a suitable maximum such as 50, and a custom scale rule of type azure-servicebus on the queue with a messageCount target that matches a replica's capacity per polling interval. The scaler and the application both authenticate with a managed identity. When a burst arrives, KEDA adds replicas within one polling interval; after the queue empties and the cooldown passes, the app returns to zero.

An HTTP scale rule would never see queue messages, so the worker would not scale. A minimum of one replica would add cost every night. A cron rule would scale at fixed times whether or not messages arrived.

Check yourself

A worker with minimum replicas 0 and an azure-servicebus rule scales out about half a minute after messages arrive. Why?

Show answer

KEDA checks event sources on a polling interval, 30 seconds by default, so scaling from zero begins only at the next poll.

Exam Tips

Remember for the exam

  • Default scale: HTTP rule, minimum 0, maximum 10 replicas.
  • KEDA polls every 30 seconds and waits a 300-second cooldown before scaling to the minimum.
  • Queue workers use custom rules such as azure-servicebus with a messageCount target.
  • Scalers authenticate with secret references or, for supported Azure scalers, a managed identity.