Auto scaling App Service in Azure - Time & Space Complexity
We want to understand how the time to adjust resources changes as demand grows for an App Service.
How does the system handle more users by adding more instances automatically?
Analyze the time complexity of this auto scaling setup.
// Define autoscale setting
az monitor autoscale create \
--name MyAppServiceScaleRule \
--resource-group MyResourceGroup \
--resource MyAppServicePlan \
--resource-type Microsoft.Web/serverfarms \
--min-count 1 \
--max-count 5 \
--count 1
az monitor autoscale rule create \
--resource-group MyResourceGroup \
--autoscale-name MyAppServiceScaleRule \
--metric-name CpuPercentage \
--operator GreaterThan \
--threshold 70 \
--time-aggregation Average \
--time-grain PT1M \
--scale-action-direction Increase \
--scale-action-type ChangeCount \
--scale-action-value 1 \
--cooldown-period PT5M
This sequence sets up auto scaling for an App Service based on CPU usage, scaling out by one instance when CPU goes above 70%.
Look at what happens repeatedly as load changes.
- Primary operation: Checking CPU metric and deciding to add or remove instances.
- How many times: This check happens continuously at regular intervals.
- Scaling action: Adding or removing one instance at a time when threshold is crossed.
As more users come, CPU usage rises and triggers scaling.
| Input Size (Users) | Approx. Scale Actions |
|---|---|
| 10 | 0-1 (likely no scale needed) |
| 100 | 1-3 scale outs over time |
| 1000 | Up to 4 scale outs, limited by max instances |
Scaling actions increase roughly linearly with load until the max instance limit is reached.
Time Complexity: O(n)
This means the number of scaling actions grows roughly in direct proportion to the increase in load.
[X] Wrong: "Auto scaling instantly adds many instances at once as load grows."
[OK] Correct: Auto scaling adds or removes instances one at a time based on rules and cooldowns, so scaling actions grow step-by-step, not all at once.
Understanding how auto scaling reacts to load helps you design systems that handle growth smoothly and efficiently.
"What if we changed the scale out increment from 1 instance to 3 instances? How would the time complexity change?"
Practice
Solution
Step 1: Understand auto scaling concept
Auto scaling means the system changes resources automatically without manual intervention.Step 2: Identify the purpose in Azure App Service
Azure App Service auto scaling adjusts the number of instances based on app demand to maintain performance and control costs.Final Answer:
Automatically adjust app resources based on demand -> Option AQuick Check:
Auto scaling = automatic resource adjustment [OK]
- Confusing auto scaling with manual scaling
- Thinking auto scaling backs up data
- Assuming auto scaling monitors user activity
Solution
Step 1: Identify correct metric for scaling out
CPU usage is a common metric to trigger scaling out when it exceeds a threshold.Step 2: Check the threshold and direction
Scaling out happens when CPU usage is high (e.g., > 70%), not low or unrelated metrics like disk space.Final Answer:
Set a rule to scale out when CPU usage > 70% -> Option CQuick Check:
Scale out triggers on high CPU [OK]
- Using memory or disk space incorrectly for CPU scaling
- Scaling in when CPU is still high
- Setting wrong thresholds for scaling
Solution
Step 1: Analyze CPU usage against scale out rule
CPU rose from 50% to 65%, which is above the 60% threshold for scaling out.Step 2: Apply scaling limits
Instances will increase by one but will not exceed the maximum of 5 instances.Final Answer:
The number of instances increases by one, up to 5 -> Option BQuick Check:
CPU > 60% triggers scale out [OK]
- Thinking CPU must be above 70% to scale out
- Confusing scale in and scale out rules
- Assuming immediate reset to minimum instances
Solution
Step 1: Check if scaling rules are active
If the scale out rule is disabled or not properly linked, scaling won't trigger despite high CPU.Step 2: Verify instance limits and metric support
Minimum and maximum instance counts are valid, and CPU is a supported metric, so these are unlikely causes.Final Answer:
The scale out rule is disabled or not linked to the App Service -> Option DQuick Check:
Active rules needed for scaling [OK]
- Assuming instance counts block scaling
- Believing CPU metric is unsupported
- Ignoring rule activation status
Solution
Step 1: Understand why scaling in doesn't happen
Without a cooldown period, rapid fluctuations can prevent scale in or cause instability.Step 2: Apply cooldown period to stabilize scaling
Adding a cooldown period delays scale in, allowing CPU to stay below threshold steadily before reducing instances.Final Answer:
Add a cooldown period to delay scaling in after peak hours -> Option AQuick Check:
Cooldown stabilizes scale in/out actions [OK]
- Raising minimum instances unnecessarily
- Removing scale out rule stops scaling out
- Setting scale in threshold too high causes no scale in
Ready to prove it?
Pass the test and this pattern moves unknown
