Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 6 of 28
❮Prev Next❯

Event-Driven Scaling with KEDA in Container Apps

Module 1: Containers ⏱ 9 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    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.

    Simulate it » No Azure account needed

    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.

    Azure portal: Scale a container app on Service Bus messages Add a Service Bus scale rule with min and max replicas.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading the portal…

    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
    1. This worker reads the orders queue, so HTTP traffic is the wrong signal. Open Scale and select Edit and deploy.
    2. On Scale and replicas, set min 0 and max 20, then add a rule.
    3. A custom rule with KEDA’s azure-servicebus scaler. messageCount: 5 means one replica for every 5 waiting messages.
    4. Select Create. Changing scale settings makes a new revision.
    5. Flood the queue with 100 orders and watch Metrics: replicas climb to the max of 20.
    6. 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.

    😟 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
    🛠️ Solved with: KEDA scale rules

    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.

    ▶ Explainer video See it in motion 7 steps · 1 min 42 sec

    An animation of a summarisation worker in Azure Container Apps with an azure-servicebus KEDA scale rule (messageCount 5, min 0, max 20): the queue fills, replicas scale from 0 to 3 and then to the maximum of 20, the queue drains, the cooldown passes and replicas return to zero, with a cost chart that rises and falls with the replica count.
    Read the video script
    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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.
    6. 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.
    7. 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

    TypeScales onScale to zero?
    HTTPConcurrent HTTP requests per replicaYes
    TCPConcurrent TCP connectionsYes
    Custom (KEDA scaler)An event source: azure-servicebus, azure-queue, kafka, redis, cron and moreYes
    CPU / memoryResource use of running replicasNo

    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 waitingmessageCountmaxReplicas
    0 (for 5 minutes)5200
    125203
    50052020

    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. 1 Idle

      0 replicas, KEDA polls the queue.

    2. 2 Burst

      500 jobs arrive; KEDA asks for up to 20 replicas.

    3. 3 Drain

      Replicas complete messages and the queue shrinks.

    4. 4 Cool down

      Queue empty for 300 s, back to 0.

    A morning burst
    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

    Insurance

    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.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    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.

    Question 1

    Question 1 of 5

    A container app processes messages from an Azure Service Bus queue named jobs. The app must add replicas as messages accumulate and scale to zero when the queue is empty. Which scale rule type should you configure?

    Question 2

    Question 2 of 5

    A container app has a Service Bus scale rule with messageCount set to 10, --min-replicas set to 0, and --max-replicas set to 5. The queue contains 200 messages. How many replicas will run?

    Question 3

    Question 3 of 5

    You configure a Service Bus scale rule for a container app. The scaler must authenticate to Service Bus by using a connection string. The connection string is stored as an app secret named sb-conn. Which parameter should you use?

    Question 4

    Question 4 of 5

    A container app scales to zero overnight. Users report a delay on the first request each morning. You must remove the delay while keeping event-driven scaling. What should you do?

    Question 5

    Question 5 of 5 · Select 2

    Which TWO statements about scaling in Azure Container Apps are true?

    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. 1.Which autoscaler does Container Apps use?Show

      KEDA.

    2. 2.Which scale rule type watches a Service Bus queue?Show

      azure-servicebus (a custom rule).

    3. 3.Which setting allows scale to zero?Show

      --min-replicas 0

    4. 4.Which parameter passes a secret to the scaler?Show

      --scale-rule-auth

    5. 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

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.