Event-Driven Scaling with KEDA in Container Apps
30-second answer
Azure Container Apps scales with KEDA (Kubernetes Event-driven Autoscaling). You add a scale rule: HTTP (concurrent requests), TCP, or a custom KEDA scaler such as azure-servicebus with metadata like queueName and messageCount. Replicas grow as the queue fills, up to --max-replicas, and shrink after the queue empties, down to --min-replicas, which can be 0. The scaler authenticates with a secret (--scale-rule-auth) or a managed identity.
Exam objective: 1.2.2 Implement event-driven scaling by using Kubernetes Event-driven Autoscaling (KEDA) in Container Apps. (Skills measured as of October 7, 2026)
Simulations
Feel it before you read it. No account needed, nothing leaves this page.
This worker reads the <code>orders</code> queue, so HTTP traffic is the wrong signal. Open <strong>Scale</strong> and select <strong>Edit and deploy</strong>.
👆 Click the glowing item in the portal to continue
Read the steps as text
- This worker reads the orders queue, so HTTP traffic is the wrong signal. Open Scale and select Edit and deploy.
- On Scale and replicas, set min 0 and max 20, then add a rule.
- A custom rule with KEDA’s azure-servicebus scaler. messageCount: 5 means one replica for every 5 waiting messages.
- Select Create. Changing scale settings makes a new revision.
- Flood the queue with 100 orders and watch Metrics: replicas climb to the max of 20.
- When the queue drains, KEDA scales in. With min 0 you pay only while there’s work.
🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.
🖱️ Drag to rotate · scroll or pinch to zoom
KEDA scales on events
Container Apps uses KEDA scale rules. A Service Bus rule adds replicas as the queue grows (for example one replica per 5 messages), up to max replicas. When the queue empties, it scales back to min replicas, which can be zero.
🤔 What happens if…
Read this tour as text
- Build an image, push it to the registry. Your Python API and everything it needs are packed into a container image. You push it to Azure Container Registry (ACR) as
ragapi:1.4.0. A tag names a version. A digest (sha256:…) names the exact bytes and never changes. - ACR Tasks build in the cloud. ACR Tasks build the image inside Azure, so you don’t need Docker on your laptop.
az acr buildis a quick task. A task can also run on every git commit or when the base image (saypython:3.12-slim) gets a security patch. - App Service runs one container. App Service pulls the image and runs it. App settings become environment variables inside the container. A setting can be a Key Vault reference, so the secret never sits in the image or in the portal in plain text.
- Container Apps environment. An Azure Container Apps environment is a secure boundary with its own virtual network and Log Analytics workspace. Several container apps share it. Ingress decides who can reach each app: external (internet) or internal (only the environment).
- Revisions and traffic splitting. Every change to the template (image, env vars, scale rules) creates a new revision, an immutable snapshot. In multiple revision mode you can send 80% of traffic to the old one and 20% to the new one, then roll forward or back.
- KEDA scales on events. Container Apps uses KEDA scale rules. A Service Bus rule adds replicas as the queue grows (for example one replica per 5 messages), up to max replicas. When the queue empties, it scales back to min replicas, which can be zero.
- AKS runs what the manifest says. On AKS you describe the app in YAML manifests: a Deployment (image, replicas, probes, resources) and a Service (a stable address).
kubectl apply -fsends them. Kubernetes keeps the real state equal to what you declared. - Troubleshoot: logs, events, connectivity. When it breaks, look in three places. Logs (
kubectl logs,az containerapp logs show) show what the app said. Events (kubectl describe,kubectl get events) show what the platform did. Connectivity checks (DNS, ports, ingress) show whether traffic can reach it.
An order worker should cost nothing when the queue is empty and fan out when orders pile up. Wire a Container Apps scale rule to a Service Bus queue, flood the queue and watch replicas go from zero to ten and back.
🎓 Education-only simulation. Your lab is saved in this browser, so you can leave and continue later.
😟 THE PROBLEM
Your summariser runs 10 replicas all night for an empty queue.
😊 THE SOLUTION
Scale on queue length with KEDA, down to zero when idle.
- Idle replicas cost money✓ Min replicas 0
- Queue backlog at 9 AM✓ Scale out on message count
- Scaler needs access✓ Managed identity auth
Start with a question
Why pay for ten servers at 3 a.m. when nobody's uploading documents?
Your summarisation worker reads jobs from a Service Bus queue. In the morning, 500 PDFs arrive in ten minutes. At night, nothing. If you size for the morning, you pay for idle servers all night. If you size for the night, the morning queue backs up for an hour.
KEDA scale rules in Azure Container Apps watch the queue itself. More messages, more replicas. Empty queue, back to zero.
Explain it like I’m 10
Visual: KEDA scaling in motion
Watch a Service Bus queue fill and drain, and the replica count (and the bill) follow it from zero to the max and back.
Read the video script
- Night: nothing to doHere's a summarisation worker in Container Apps. It's night, the queue is empty, and the app is at zero replicas. Nothing's running, so there's nothing to pay for.
- A KEDA rule watches the queueA KEDA scale rule watches the Service Bus queue for you. This one says: about one replica for every five messages waiting, between zero and twenty replicas. KEDA checks the queue every thirty seconds.
- 12 jobs arrive, 3 replicas startTwelve documents land in the queue. Twelve divided by five, rounded up, is three. So KEDA brings the app up from zero to three replicas.
- Morning burst: up to the maxThen the morning burst. Five hundred jobs. That would ask for a hundred replicas, but max replicas is twenty, so it stops at twenty. Max is your ceiling, for cost and for whatever the worker calls downstream.
- The queue drainsTwenty workers chew through the queue. As it shrinks, KEDA asks for fewer replicas. Your code should only complete a message after the work is done, so a replica that's removed mid-job doesn't lose anything.
- Cooldown, then zeroOnce the queue's empty, KEDA waits for the cooldown period, three hundred seconds by default. Nothing new arrives, so the last replica goes away and the app is back at zero.
- Cost follows replicasLook at the bill. It rises and falls with the replica count, and it's flat at zero overnight. One exam detail: CPU and memory rules can't scale to zero. You need min replicas zero and an event rule like this one.
The technical explanation
Scale rule types
| Type | Scales on | Scale to zero? |
|---|---|---|
| HTTP | Concurrent HTTP requests per replica | Yes |
| TCP | Concurrent TCP connections | Yes |
| Custom (KEDA scaler) | An event source: azure-servicebus, azure-queue, kafka, redis, cron and more | Yes |
| CPU / memory | Resource use of running replicas | No |
If an app has ingress and no rule, Container Apps adds a default HTTP rule. The replica count always stays between min and max (defaults 0 and 10).
A Service Bus rule with the CLI
az containerapp create --resource-group rg-ai --name summariser \
--environment env-rag \
--image myreg.azurecr.io/summariser:2.1.0 \
--secrets sb-conn="<service-bus-connection-string>" \
--min-replicas 0 --max-replicas 20 \
--scale-rule-name summarise-queue \
--scale-rule-type azure-servicebus \
--scale-rule-metadata queueName=summarise namespace=sb-rag messageCount=5 \
--scale-rule-auth connection=sb-conn
--scale-rule-auth connection=sb-conn maps the scaler’s connection parameter to the app secret sb-conn. To avoid a connection string, give the app a managed identity with the Azure Service Bus Data Owner role and pass --scale-rule-identity system instead of the auth line. For a topic, use topicName and subscriptionName instead of queueName.
Change rules on an existing app with az containerapp update ... --scale-rule-name ... --min-replicas ... --max-replicas .... Scale rules are part of the template, so this creates a new revision.
How the numbers work
KEDA checks the queue every 30 seconds. It aims for roughly queue length ÷ messageCount replicas, never above max:
| Messages waiting | messageCount | max | Replicas |
|---|---|---|---|
| 0 (for 5 minutes) | 5 | 20 | 0 |
| 12 | 5 | 20 | 3 |
| 500 | 5 | 20 | 20 |
After the queue is empty, the cooldown period (300 seconds) passes before the last replica goes away. Your code should complete messages only after the work is done, so a replica being removed mid-job doesn’t lose anything.
-
1 Idle
0 replicas, KEDA polls the queue.
-
2 Burst
500 jobs arrive; KEDA asks for up to 20 replicas.
-
3 Drain
Replicas complete messages and the queue shrinks.
-
4 Cool down
Queue empty for 300 s, back to 0.
Architect level Container Apps jobs for run-to-completion work
A worker app keeps replicas running and pulls messages in a loop. If each message is a self-contained job (summarise one PDF and exit), a Container Apps job with --trigger-type Event can fit better: the same KEDA scaler starts one job execution per batch of messages, each runs to completion, and you get execution history per job. Use --replica-completion-count, --parallelism and --polling-interval to tune it.
Real-world business scenario
Adatum Insurance (fictional)
- The problem
- Claims documents arrived in bursts after storms. A fixed pool of four workers left claims waiting for hours, then sat idle for days.
- What they used
- A summarisation worker in Azure Container Apps with an azure-servicebus scale rule, messageCount 5, min replicas 0 and max replicas 30, authenticating with a managed identity.
- Why that choice
- KEDA reacts to the queue length directly and scales to zero between storms, so the team only pays while claims are processed.
- The result
- Storm backlogs clear in minutes instead of hours, and the quiet weeks cost close to nothing.
When to use it and when not to
✓ Use it when…
- Queue or stream workers with bursty, unpredictable load.
- Back ends that should cost nothing when idle (min replicas 0).
- Scaling on business signals such as queue depth rather than CPU.
✕ Think twice when…
- Latency-sensitive APIs that can't tolerate a cold start; keep min replicas at 1 or more.
- Workloads with steady, predictable load where fixed replicas are simpler.
↔ Alternatives to consider
- Container Apps jobs with an event trigger for run-to-completion tasks.
- Azure Functions Flex Consumption for small queue handlers written as functions.
Test yourself with exercises
5 original practice questions in the AI-200 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.
Interview preparation
Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.
Beginner What does KEDA add that CPU-based autoscaling doesn't?
Model answer
It scales on the work waiting, not on how busy the current replicas look. A Service Bus rule sees 500 messages queued and adds replicas before CPU even rises. And because it watches the queue, it can scale all the way to zero and wake up when a message arrives.
Intermediate How do you scale a container app on a Service Bus queue?
Model answer
I add a custom scale rule with --scale-rule-type azure-servicebus, metadata queueName, namespace and messageCount, and auth for the scaler, either a connection-string secret through --scale-rule-auth or a managed identity. Then I set min replicas to 0 and a sensible max.
Interviewer follows up
If messageCount is 5 and 100 messages arrive, how many replicas?
You
KEDA targets about 100 divided by 5, so 20, capped by max replicas. If max is 10, I get 10.
Experienced developer Why might a CPU scale rule be wrong for a queue worker that should scale to zero?
Model answer
CPU and memory rules can't scale to zero, because there has to be a replica running to measure. For an idle-most-of-the-day worker I want an event source like the queue length, which KEDA can check with no replicas running.
Scenario Your worker scaled to zero, but the first message each morning takes a while to process. What can you do?
Model answer
That's the cold start of the first replica. If it matters, set min replicas to 1 so one replica stays warm. It costs a little more but removes the wait.
Quick revision
Key points
- Container Apps scale rules are HTTP, TCP or custom (any KEDA scaler, such as azure-servicebus or azure-queue).
- Replicas stay between --min-replicas and --max-replicas; min 0 allows scale to zero.
- Service Bus rules use metadata queueName (or topicName and subscriptionName), namespace and messageCount.
- Scalers authenticate with a secret via --scale-rule-auth or with a managed identity.
- CPU and memory rules can't scale to zero.
Important terminology
- KEDA
- Kubernetes Event-driven Autoscaling, the open-source scaler that Container Apps uses under the hood.
- Scale rule
- A rule on a container app that tells KEDA what to watch (HTTP, TCP or a custom event source) and when to add replicas.
- messageCount
- The azure-servicebus scaler setting for how many messages one replica should handle; KEDA adds replicas as the queue passes it.
- Min and max replicas
- The lower and upper bounds for the number of replicas; a minimum of 0 allows scale to zero.
- Cooldown period
- How long Container Apps waits after the last event before scaling down to zero (300 seconds by default).
Common mistakes
- Setting max replicas too low, so a big backlog drains slowly.
- Using a CPU rule for a worker that's meant to scale to zero.
Exam tips
- "Scale based on the number of messages in a queue" = custom KEDA rule (azure-servicebus or azure-queue).
- "No cost when idle" = --min-replicas 0.
- "Avoid cold starts" = min replicas 1 or more.
Five quick revision questions
-
1.Which autoscaler does Container Apps use?Show
KEDA.
-
2.Which scale rule type watches a Service Bus queue?Show
azure-servicebus (a custom rule).
-
3.Which setting allows scale to zero?Show
--min-replicas 0
-
4.Which parameter passes a secret to the scaler?Show
--scale-rule-auth
-
5.Can a CPU rule scale to zero?Show
No.
Official references
Finished this lesson? Tick it off. Your progress is saved in this browser.
Next up: Deploy to AKS with Manifest Files
Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges.