ECS service auto scaling in AWS - Time & Space Complexity
When using ECS service auto scaling, it's important to understand how the number of scaling actions grows as the workload changes.
We want to know how the system reacts over time as demand increases or decreases.
Analyze the time complexity of this ECS auto scaling setup.
aws application-autoscaling register-scalable-target \
--service-namespace ecs \
--resource-id service/clusterName/serviceName \
--scalable-dimension ecs:service:DesiredCount \
--min-capacity 1 \
--max-capacity 10
aws application-autoscaling put-scaling-policy \
--service-namespace ecs \
--resource-id service/clusterName/serviceName \
--scalable-dimension ecs:service:DesiredCount \
--policy-name cpu-scaling-policy \
--policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration file://config.json
This sequence registers the ECS service for auto scaling and sets a policy to adjust the number of tasks based on CPU usage.
Look at what happens repeatedly as load changes.
- Primary operation: Scaling actions that adjust the desired task count.
- How many times: Each time the monitored metric crosses thresholds, a scaling action triggers.
As workload increases, the number of scaling actions grows roughly in proportion to how often the metric changes enough to trigger scaling.
| Input Size (n) | Approx. Scaling Actions |
|---|---|
| 10 | About 10 scaling checks, few actions |
| 100 | About 100 scaling checks, more actions |
| 1000 | About 1000 scaling checks, many actions |
Pattern observation: The number of scaling actions grows linearly with the number of metric evaluations.
Time Complexity: O(n)
This means the number of scaling operations grows directly with the number of metric checks or workload changes.
[X] Wrong: "Scaling happens instantly and only once regardless of workload size."
[OK] Correct: Scaling triggers repeatedly as metrics change, so more workload changes mean more scaling actions over time.
Understanding how auto scaling reacts over time helps you design systems that respond well to demand without overloading or wasting resources.
"What if we changed the scaling policy from target tracking to step scaling? How would the time complexity change?"
Practice
Solution
Step 1: Understand ECS service auto scaling function
ECS service auto scaling changes the number of containers automatically to match workload.Step 2: Compare options to this function
Only Automatically adjust the number of running containers based on demand describes automatic adjustment of container count based on demand.Final Answer:
Automatically adjust the number of running containers based on demand -> Option CQuick Check:
Auto scaling adjusts container count = Automatically adjust containers [OK]
- Confusing auto scaling with manual container management
- Thinking auto scaling creates clusters
- Believing auto scaling stores logs
Solution
Step 1: Identify correct metric type for CPU utilization
The predefined metric for ECS CPU is "ECSServiceAverageCPUUtilization" with a numeric target value.Step 2: Check option syntax and metric correctness
"TargetValue": 50, "PredefinedMetricSpecification": {"PredefinedMetricType": "ECSServiceAverageCPUUtilization"} uses correct metric type and numeric target value 50, which is valid.Final Answer:
"TargetValue": 50, "PredefinedMetricSpecification": {"PredefinedMetricType": "ECSServiceAverageCPUUtilization"} -> Option AQuick Check:
Correct metric and numeric target = "TargetValue": 50, "PredefinedMetricSpecification": {"PredefinedMetricType": "ECSServiceAverageCPUUtilization"} [OK]
- Using string instead of number for TargetValue
- Using wrong metric names like CPUUsage
- Mixing memory or network metrics for CPU scaling
{"MinCapacity": 2, "MaxCapacity": 5, "TargetValue": 60}What happens when CPU utilization stays at 70% for a long time?
Solution
Step 1: Understand scaling triggers from CPU target
TargetValue 60 means ECS tries to keep CPU near 60%. If CPU is 70%, it needs more containers.Step 2: Apply min and max capacity limits
Min is 2, max is 5. Scaling up is allowed up to 5 containers to reduce CPU load.Final Answer:
The service scales up to 5 containers to reduce CPU load -> Option AQuick Check:
CPU above target scales up containers = The service scales up to 5 containers to reduce CPU load [OK]
- Thinking service scales down when CPU is high
- Ignoring max capacity limit
- Assuming service crashes on high CPU
Solution
Step 1: Analyze scaling limits
If max capacity equals min capacity, no room to scale out exists.Step 2: Check other options
Target 100% means scaling triggers only at max CPU, but scaling can still happen. No running tasks or load balancer affect service but not scaling limits directly.Final Answer:
The maximum capacity is set equal to the minimum capacity -> Option DQuick Check:
Max equals min blocks scaling out = The maximum capacity is set equal to the minimum capacity [OK]
- Ignoring max and min capacity settings
- Assuming load balancer affects scaling triggers
- Confusing target CPU with scaling limits
Solution
Step 1: Set correct min and max container counts
MinCapacity must be less than MaxCapacity, so 3 and 10 is valid, 10 and 3 is invalid.Step 2: Use correct metric and target value for memory
Memory scaling uses ECSServiceAverageMemoryUtilization with target 70% to trigger scaling.Step 3: Verify options
MinCapacity: 3, MaxCapacity: 10, TargetValue: 70, PredefinedMetricType: ECSServiceAverageMemoryUtilization matches all requirements: correct min/max, target 70, memory metric.Final Answer:
MinCapacity: 3, MaxCapacity: 10, TargetValue: 70, PredefinedMetricType: ECSServiceAverageMemoryUtilization -> Option BQuick Check:
Correct min/max and memory target = MinCapacity: 3, MaxCapacity: 10, TargetValue: 70, PredefinedMetricType: ECSServiceAverageMemoryUtilization [OK]
- Setting max less than min capacity
- Using CPU metric for memory scaling
- Setting wrong target value for scaling
Ready to prove it?
Pass the test and this pattern moves unknown
