Autoscaling
Adding instances without being asked
Section titled “Adding instances without being asked”Scaling out is only useful if it happens when it is needed, and nobody can watch the traffic all day. Autoscaling lets a policy add instances when the load rises and remove them when it falls. The Azure Architecture Center describes the aim as allocating resources to match performance requirements, and de-allocating them when demand slackens, to minimise cost.
A policy can follow a schedule, adding instances before the busy hours, or it can react to a measure such as utilisation. The commonest reactive policy is target tracking. You choose a metric and a target value, and AWS describes what the group then does as adding or removing capacity to keep the metric at, or close to, the target, in the way a thermostat holds a temperature.
How much capacity? The Kubernetes autoscaler states the rule as a formula: the desired number of replicas is the current number times the current metric over the desired metric, rounded up. In the lab below the group starts with 4 instances at 200% utilisation against a target of 60%, so it asks for 4 × 200% ÷ 60% = 13.3, rounded up to 14.
Mental model. An autoscaler is a thermostat with a slow furnace. It reads the temperature, works out how much heat is needed, and orders it. The heat arrives only after the furnace has started, so a sudden cold snap is felt in full before anything helps.
The settings
Section titled “The settings”The lab has five settings, and each of them comes from the way real autoscalers work.
- Target utilisation. The utilisation the policy aims to hold. AWS advises setting it as high as possible with a reasonable buffer for unexpected traffic increases. A lower target means more instances and more spare capacity.
- Start-up delay. The time from ordering an instance to its serving requests. AWS does not count an instance in its group’s metrics until its warm-up has expired, and Google’s autoscaler ignores usage data from instances still in their initialisation period, 60 seconds by default. In the lab an instance is paid for from the moment it is ordered.
- Scale-in delay. How long the load must stay low before instances are removed. Kubernetes waits for a stabilisation window, 300 seconds by default, and then chooses the highest recommendation within it, to avoid flapping. Google’s default is 10 minutes. The lab does the same: it shrinks to the highest size the load needed during the delay.
- Minimum instances and Maximum instances. The group never goes outside them. Azure advises limiting the maximum, to prevent a system from scaling out excessively and to avoid the cost of running many instances. Google’s workbook advises setting a minimum and a maximum bound, and making sure that you have enough quota.
The lab also has a cooldown of 60 seconds, which is not a control. After the number of serving instances changes, the group does not shrink for that long. It always grows when it needs to, which is how AWS describes scale-in cooldowns: a scale-out during the cooldown goes ahead at once.
Try it: a run of the lab
Section titled “Try it: a run of the lab”The lab runs a policy against traffic that changes over time. The group is 4 instances of 200 requests per second each, behind a load balancer, with 400 requests per second arriving. Choose a pattern, set the policy, and press Play to watch the run. Every figure below is read from the lab’s summary and its charts.
A group of identical API instances sits behind a load balancer. A policy adds instances when the load rises and removes them when it falls, but a new instance takes time to start. Choose a traffic pattern, set the policy, and play the run to see when the group falls behind.
Each dot stands for many requests. The numbers show the real rate.
The autoscaling run
- Traffic
- 1,6004× the base traffic
- Instances serving
- 4Starting up: 0
- Utilisation
- 200%OverloadedTarget: 60%
- 99th percentile
- 1.1 sGoal: 250 ms
- Requests rejected
- 800 per second50% of the traffic
At 5:00, 1,600 requests per second arrive and the 4 serving instances can answer 800. The rest, 800 per second, are rejected.
Traffic and capacity
- Traffic
- Capacity of the serving instances
Instances
- Serving
- Paid for
Utilisation
- Utilisation
- Target
Time to answer, 99th percentile
- 99th percentile
- Goal
Drag on a chart to move through the run. The moment slider does the same from the keyboard.
What the run came to
- Peak utilisation
- 200%
- Time above the target
- 95 s
- Requests rejected
- 76,0006.6% of all requests
- Time over the latency goal
- 95 s
- Instance-hours
- 3.8Most paid for at once: 14
- Scaling actions
- 21 out, 1 in
The run as text
What happened
- 5:00: demand goes above capacity. Utilisation reaches 200% with 4 instances serving.
- 5:05: the policy orders 10 instances. They start serving at 6:35.
- 6:35: demand is back within capacity, with 14 instances serving.
- 6:35: 10 instances start serving, 14 in all.
- 16:05: the policy removes 10 instances, and 4 remain.
| Moment | Traffic (req/s) | Serving | Starting up | Utilisation | 99th percentile | Rejected (req/s) |
|---|---|---|---|---|---|---|
| 0:00 | 400 | 4 | 0 | 50% | 92 ms | 0 |
| 2:30 | 400 | 4 | 0 | 50% | 92 ms | 0 |
| 5:00 | 1,600 | 4 | 0 | 200% | 1.1 s | 800 |
| 7:30 | 1,600 | 14 | 0 | 57% | 92 ms | 0 |
| 10:00 | 1,600 | 14 | 0 | 57% | 92 ms | 0 |
| 12:30 | 400 | 14 | 0 | 14% | 92 ms | 0 |
| 15:00 | 400 | 14 | 0 | 14% | 92 ms | 0 |
| 17:25 | 400 | 4 | 0 | 50% | 92 ms | 0 |
| 19:55 | 400 | 4 | 0 | 50% | 92 ms | 0 |
| 22:25 | 400 | 4 | 0 | 50% | 92 ms | 0 |
| 24:55 | 400 | 4 | 0 | 50% | 92 ms | 0 |
| 27:25 | 400 | 4 | 0 | 50% | 92 ms | 0 |
| 29:55 | 400 | 4 | 0 | 50% | 92 ms | 0 |
| Component | State | Utilisation | Offered (req/s) | Served (req/s) | Rejected (req/s) | p50 | p95 | p99 |
|---|---|---|---|---|---|---|---|---|
| Clients | Healthy | <1% | 1.6K | 1.6K | 0 | 0 ms | 0 ms | 0 ms |
| Load balancer | Healthy | <1% | 1.6K | 1.6K | 0 | 0.03 ms | 0.15 ms | 0.23 ms |
| API | Overloaded | 200% | 1.6K | 800 | 800 | 1 s | 1.1 s | 1.1 s |
- Traffic
- 1,600 req/s
- Successful
- 800 req/s
- Error rate
- 50%
- Mean latency
- 510 ms
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Requests arrive at random, as a Poisson process, at a constant average rate.
- Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
- Service times are random with the stated mean; by default they are exponentially distributed.
- Each node is analysed separately, with the traffic it receives treated as Poisson.
- The policy aims to keep utilisation at its target by adding as many instances as the traffic needs at that target, rounded up, and by removing them only when the load has stayed low for the scale-in delay.
- The load balancer spreads requests evenly, so the instances share one queue.
Simplifications
- Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
- Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
- A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
- A request calls its dependencies one after another. Parallel fan-out is not modelled.
- When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
- Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
- Retry feedback is solved from the healthy starting point, so it finds one stable outcome. Systems that stay stuck in overload after the cause is gone are not shown.
- A throughput limit (for example maximum writes per second) is modelled as slower service, not as a separate rate limiter.
- A caller’s timeout counts the mean time its dependencies take, not their full distribution.
- Each moment of the run is solved as a steady state: the traffic of that moment meets the capacity serving at that moment. A queue that builds and drains within a few seconds is not carried from one moment to the next.
- The policy reads the load once per step, every 5 seconds in the short runs and every minute in the daily wave, and it reads it exactly. A real policy reads a metric that is averaged over a period and reported with a delay, so it reacts later than this one.
- A new instance serves nothing until its start-up delay has passed, and is paid for from the moment it is ordered. An instance that is removed stops at once: requests it is working on are not drained or lost.
- Utilisation is the traffic over the capacity of the serving instances, and can exceed 100%. A real CPU reading stops at 100%, so a policy that watches it sees an overload as smaller than it is.
- After the group changes, it does not shrink for the cooldown (60 seconds here). It always grows when it needs to.
Parameter values are illustrative. They are not benchmarks of any real product.
Start with the defaults and the Sudden spike. At 5:00 the traffic jumps to four times as much, 1,600 requests per second, and the 4 instances can serve 800. Utilisation is 200%, the 99th percentile is 1.1 s, and 800 requests per second are rejected. At 5:05 the policy orders 10 instances, and they start serving at 6:35, 90 seconds later, with the panel reading Starting up: 10 in between. For those 95 seconds, 800 × 95 = 76,000 requests are rejected, which is 6.6% of all the requests of the run. The summary agrees: a peak utilisation of 200%, 95 seconds above the target and over the latency goal, 3.8 instance-hours, and two scaling actions, one out and one in. The spike ends at 11:00, and 300 seconds later, at 16:05, the policy removes the 10 instances.
Now change one setting at a time and read the summary.
- Start-up delay to 30 seconds: 28,000 requests are rejected. To 0: 4,000, which is the 5 seconds that the policy needs to read the load. To 300 seconds: 244,000, which is 21.2% of the run. The group ends up the same size. The delay decides how long it is short.
- Minimum instances to 14: nothing is rejected, because the group already has the capacity when the spike arrives, and it costs 7 instance-hours against 3.8. To 10: nothing is rejected either, utilisation peaks at 80%, and the cost is 5.7 instance-hours. Insurance has a price, and it is a matter of how much to buy.
- Target utilisation to 40%: the group is 5 instances at the start and grows to 20, and 57,000 requests are rejected. To 90%: it is 3 instances and grows to 9, and 95,000 are rejected, for 2.6 instance-hours. A lower target keeps more spare capacity, and the spare capacity absorbs part of the jump.
- Scale-in delay to 0: the instances go as soon as the cooldown allows, and the run costs 3 instance-hours. To 600 seconds: 4.7. What was rejected does not change, at 76,000, because the delay acts only after the spike.
- Maximum instances to 5: the group cannot grow past 5, and 235,000 requests are rejected, 20.4% of the run. To 8: the group is at exactly 100% for the six minutes of the spike, nothing more is rejected than before, but the 99th percentile is over the goal for 6 minutes.
- The Ramp, at the defaults: nothing is rejected, and utilisation peaks at 76%. Traffic climbs slowly, and the spare capacity of the target absorbs the growth during the start-up delay. Now set Target utilisation to 90% and Start-up delay to 300 seconds: 157,500 requests are rejected, 8%, with utilisation peaking at 158%.
- The Daily wave: nothing is rejected, the group goes between 2 and 6 instances, scaling out 4 times and in 4 times, and the day costs 94 instance-hours. A fixed group of 6, set with Minimum instances, costs 144. Autoscaling saves 35% here, because the traffic is slow enough for the policy to follow.
Why a spike still hurts
Section titled “Why a spike still hurts”The first run is the lesson. The group did what the policy says, and 76,000 requests were still rejected. A policy cannot act on load it has not yet seen, and the capacity it orders arrives later. For a jump from a group’s capacity to more, the loss is the shortfall times the time until the new instances serve: the policy’s reading interval plus the start-up delay. Here it is (1,600 − 800) × (5 + 90) = 76,000.
Azure’s guide says the same in its own terms. Autoscaling is not an instantaneous process, it might not be the most appropriate mechanism to handle a sudden burst, and the peak demand might pass by the time the extra resources are available. Google’s workbook says the autoscaler needs adequate time to react, particularly when new instances cannot start serving at once, and that autoscaling is one of several kinds of load management, with load balancing and load shedding.
What shrinks the loss, from the lab:
- A shorter start-up delay: 28,000 instead of 76,000 at 30 seconds.
- More spare capacity: a lower target, or a higher minimum, at the price of instances paid for all the time.
- Capacity ordered before the spike. Azure suggests a scheduled policy that starts enough instances before a load that is expected.
- Throttling the traffic while the group grows. Azure suggests it for a sudden burst, and a later module covers it.
The ramp shows the other side. When traffic grows more slowly than the group can follow, autoscaling works without loss. It is for gradual change, not for jumps.
What happens if you trust autoscaling with a flash sale?
Section titled “What happens if you trust autoscaling with a flash sale?”A team expects a sale to take traffic from 400 to 1,600 requests per second within seconds. It relies on the defaults. The first 95 seconds reject 76,000 requests, 800 a second for every shopper who arrives, and then the group is fine.
The team can pay to avoid it. A minimum of 10 instances keeps utilisation at 80% through the jump and rejects nothing, for 5.7 instance-hours instead of 3.8 over the 30 minutes of the run. A minimum of 14 does the same at 7. Whether that is worth it depends on what a rejected request costs, which only the team knows. The point of the model is to make the choice visible: the group was never going to be big enough in time, and the only question is who pays, in rejected requests or in instances.
The scaling lab above grows one group of instances and nothing else. The lab below is a whole shop, with a load balancer, API instances, a cache and a database, and in it nothing reacts: no policy adds instances, so you see what a spike does before anyone has answered it. Before you press Add 10x traffic, write down which part you expect to give way first. Then choose the fix that adds API instances and run the same spike again: the API copes, and a different component gives way next. Capacity has to grow along the whole path, not only at the first component that fails.
Add 10x traffic
The shop serves 900 requests a second. A campaign, a news story or a bug in a client multiplies the traffic by ten: 9,000 requests a second against an API that can serve 1,200. You add the traffic and watch where the system gives way. Then choose how to answer it, and notice what gives way next.
The failure lab needs JavaScript. The healthy system is shown below.
1. Break it
The healthy system is shown first. When you press the button the failure strikes, and the run follows the 30 seconds after it.
What the failure does: Traffic jumps to ten times what the system normally serves.
Each dot stands for many requests. The numbers show the real rate.
No bottleneck: every component has headroom.
Cause and effect
Each step leads to the next. A step that is changing right now is marked.
- Traffic900 req/sNormal steady
- API utilisation75%Normal steady
- API queuenone waitingNormal steady
- Latency (p99)96 msNormal steady
- Timeouts and rejections0%Normal steady
- Errors0%Normal steady
| Component | State | Utilisation | Offered (req/s) | Served (req/s) | Rejected (req/s) | p50 | p95 | p99 |
|---|---|---|---|---|---|---|---|---|
| Clients | Healthy | <1% | 900 | 900 | 0 | 0 ms | 0 ms | 0 ms |
| Load balancer | Healthy | <1% | 900 | 900 | 0 | 0.03 ms | 0.15 ms | 0.23 ms |
| API | Busy | 75% | 900 | 900 | 0 | 14 ms | 60 ms | 93 ms |
| Cache | Healthy | 1% | 810 | 810 | 0 | 0.14 ms | 0.6 ms | 0.92 ms |
| Database | Healthy | 20% | 252 | 252 | 0 | 5.5 ms | 24 ms | 37 ms |
- Traffic
- 900 req/s
- Successful
- 900 req/s
- Error rate
- 0%
- Mean latency
- 24 ms
Sources
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Requests arrive at random, as a Poisson process, at a constant average rate.
- Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
- Service times are random with the stated mean; by default they are exponentially distributed.
- Each node is analysed separately, with the traffic it receives treated as Poisson.
- The system starts healthy at 900 requests a second. Three API instances can serve 1,200 a second and the database about 1,250 operations a second. Nine in ten requests read through the cache, which answers four in five of those reads; the rest, and every write, go to the database.
- A failure strikes at one instant and nothing repairs it during the run: no restart, no autoscaling, no operator. The load balancer notices a dead instance at once.
- The instances of a component are identical, and each request is independent of the others.
Simplifications
- The traffic jumps to ten times at one instant and stays there. Real traffic ramps up, and autoscaling, which is not modelled, reacts to a ramp. The model shows what happens if nothing reacts.
- Every request costs the same. In practice a flood often arrives as a different mix of requests, such as many more reads than writes.
- A retry is a fresh, independent attempt with the same chance of failing as the last one. How long a real client library waits between attempts (backoff, jitter), and any retry budget or circuit breaker, are not simulated: attempts are counted, not scheduled.
- A client gives up when its first timeout runs out, so the latency shown stops at what a client would wait: the timeout of the first hop, times its attempts. After that the request counts as a failure.
- The cache's hit ratio is constant. Warming, stampedes and hot keys have their own labs.
- Nothing here models garbage collection, connection pools on the client, DNS, TLS handshakes, CPU throttling, noisy neighbours or cold caches. A real system fails in more ways, and in messier ones, than this model does.
- Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
- Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
- A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
- A request calls its dependencies one after another. Parallel fan-out is not modelled.
- When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
- Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
- A caller’s timeout counts the mean time its dependencies take, not their full distribution.
- Latency percentiles for a whole request are estimated, over every request, including those that are rejected and fail quickly. The mean is exact, and the percentiles come from a gamma distribution that has that mean and the variance of the time spent in each component.
Parameter values are illustrative. They are not benchmarks of any real product.
Seeing it in a real system
Section titled “Seeing it in a real system”The lab uses one set of rules that is a blend of those described above. Real products differ in the details. AWS’s target tracking scales out in proportion to how far the metric is above its target, rounds up, scales in more gradually than it scales out, and counts instances still warming up towards the capacity it has ordered. Its load balancer, when an instance is deregistered, waits 300 seconds by default for requests in flight, whereas the lab removes an instance at once.
The lab also reads the load exactly. A real policy reads a metric that is averaged over a period, so it reacts later than the lab does. Azure’s guide notes that the aggregation is deliberate, to prevent the system from reacting too quickly or oscillating, and that it means a spike can take longer to trigger scaling.
- Target tracking works out the instances needed to bring utilisation to the target, rounded up: 4 instances at 200% against a target of 60% ask for 14.
- Capacity arrives after a delay. In the default spike the group rejects 800 requests per second for 95 seconds, which is 76,000 requests, 6.6% of the run.
- A shorter start-up delay, a lower target and a higher minimum shrink the loss, and each costs something: a start-up delay of 30 seconds loses 28,000, a minimum of 10 loses nothing for 5.7 instance-hours against 3.8.
- The maximum caps the cost and the recovery: a maximum of 5 rejects 235,000 requests.
- The scale-in delay changes the cost after the spike, not the loss during it: 3, 3.8 and 4.7 instance-hours for 0, 300 and 600 seconds.
- Autoscaling follows gradual change well and sudden change badly. On the daily wave it uses 94 instance-hours against 144 for a fixed group of 6.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.