← Building blocks

Scale out, not up

Scaling & loadfundamental

A bigger machine is expensive, has a ceiling, and is still one machine.

Say it like this

Capacity should be a number I can turn up, so I run many identical stateless instances behind the balancer and autoscale on a signal that leads load, like request rate or queue depth, rather than buying a bigger box.

Basic solution

Add more commodity instances behind a balancer and autoscale on CPU, queue depth or request rate.

Kubernetes HPAAWS Auto ScalingECS

The autoscaling control loop

Metrics flow from the fleet to the autoscaler, which adds instances fast and removes them slowly, draining each one first. New instances take traffic as soon as their health check passes.

How it works

  1. The service is packaged as an image that boots to a ready state in seconds and registers itself with the load balancer or orchestrator, so a new copy can start taking traffic almost immediately.
  2. You set a target per instance, for example 70 percent CPU or 4K requests per second. The target is chosen so one instance can absorb a burst for the minute or two it takes the next instance to arrive.
  3. The autoscaler watches a metric (CPU, requests per instance, queue depth or p99 latency) and computes how many instances it wants: the current count multiplied by observed load divided by the target. If 12 instances are each seeing twice their target, it asks for 24.
  4. Scaling out is fast: a short cooldown of 1 to 2 minutes, and new instances receive traffic as soon as their health check passes. Scaling in is slow: a 10 to 15 minute cooldown, and each instance is drained (stops receiving new requests, finishes the ones in flight) before it is terminated.
  5. Limits bound the fleet in both directions: a minimum large enough to keep serving if one availability zone fails, and a maximum that the database, the cache and the budget can tolerate.

What has to be true first

  • Instances are stateless and interchangeable, they start fast, and the stores behind them (database, cache, queue) have headroom for the extra callers. Otherwise scaling the service tier only moves the bottleneck one layer down.

Worked example

Redirect tier on a viral day

  • Baseline: 4K redirects/s average, 12 instances at under 10 percent CPU; 12 is the floor for zone redundancy, not a load figure. Each instance tops out near 5K req/s.
  • A link goes viral: traffic climbs to 40K req/s over 10 minutes. Target is 3.5K req/s per instance (70 percent of 5K), so the desired count is 40,000 ÷ 3,500, about 12 instances, which the fleet already has; with the one-zone-down rule (keep capacity after losing a third) the fleet goes to 18.
  • Reaction time: metrics arrive every 15 s, the autoscaler decides every 60 s, instances boot in 40 s and pass health checks after 20 s. About 2.5 minutes from spike to extra capacity. Over those minutes the existing 12 run at about 3.3K req/s each, near 65 percent CPU, and p99 rises from 15 ms to 40 ms; had the spike overshot 5K per instance, admission control would shed the excess so successful requests stay fast.
  • Pre-scaling: because spikes arrive within minutes, the minimum is raised to 16 instances during the busiest hours by a scheduled policy, so the reactive scaler has less distance to cover.
  • The data layer: 40K req/s at a 95 percent cache hit rate is 2K database reads/s, within the replica's capacity; without the cache, scaling the API tier would have simply overloaded the primary.
  • Scale-in: traffic falls back after an hour; the scaler removes one instance every 5 minutes, draining each, back to 12.

Variants and when each wins

Reactive autoscaling on utilisation

  • The autoscaler adds instances when CPU or memory crosses a threshold. It wins for simplicity and works everywhere; it lags a fast spike by the few minutes it takes to notice, decide and boot.

Scaling on a leading signal

  • The autoscaler watches requests per instance, queue depth or consumer lag, which rise before CPU does. It wins for workers draining a queue and for any tier where you can measure demand directly.

Scheduled and predictive scaling

  • The minimum instance count is raised ahead of known peaks, such as business hours or a premiere. It wins for predictable load by removing the reaction lag entirely; pair it with reactive scaling for the unexpected.

Vertical scaling

  • One instance is replaced by a bigger one. It wins for stateful components such as a primary database, where there is no code change and no distributed concern; it has a hard ceiling, needs a restart to resize, and remains a single failure domain.

Recommendation: stateless services scale horizontally on requests per instance with a scheduled floor for known peaks; databases scale vertically first, then read replicas, then sharding.

Failure modes

Scaling moves the bottleneck

  • The fleet doubles, p99 latency does not improve, and the database sits at 100 percent. Every new instance opens its own connections and sends its own reads to a store that did not scale with it. Put a cache in front of the database, pool connections with a cap across the whole fleet, add read replicas, and size the data tier before raising the instance maximum.

Too slow to react

  • The first minutes of every spike produce errors. Instances take a long time to boot, and the metric and cooldown intervals add more delay before the autoscaler even decides. Build smaller images that reach readiness faster, scale on a leading metric, raise the scheduled floor before known peaks, and use admission control so the existing instances protect themselves while capacity arrives.

Flapping

  • The fleet grows and shrinks every few minutes without the load really changing. The scale-in cooldown is too short, or the scale-out and scale-in thresholds sit too close together, so normal fluctuation trips both. Scale out fast but scale in slowly (10 to 15 minutes), and keep a clear gap between the two thresholds.

Thundering herd of cold instances

  • Newly added instances time out under full load the moment they join the pool. They arrive with empty caches, un-warmed JIT code and empty connection pools, and the balancer sends them a full share of traffic immediately. Use slow-start weighting at the balancer so a new instance ramps up gradually, and run a warm-up step before readiness reports healthy.

Runaway cost

  • A bug or an attack scales the fleet to its maximum and holds it there for hours. Every request counted as demand, including abusive ones, so the autoscaler kept adding capacity. Set a sensible maximum, alert on fleet size, and rate limit at the edge so abusive traffic never reaches the metric the autoscaler watches.

Interviewer follow-ups

Answer out loud before you open each one.

What metric would you scale on and why?

Requests per instance for a request-serving tier, because it leads CPU and maps directly to capacity planning; consumer lag or queue depth for workers, because that is the backlog users feel. CPU is the fallback when nothing better is exported, but it lags the spike.

Why is scale-in slower than scale-out?

Adding capacity you did not need costs a little money; removing capacity you did need costs errors. So react fast upward, and remove instances slowly with draining, so a brief dip does not strip the fleet right before the next wave.

What should the minimum instance count be?

Enough to carry expected peak after losing one availability zone, plus the ramp time of the autoscaler. For three zones that means at least 1.5× the peak requirement spread evenly, and never fewer than three so a single failure is not a 50 percent capacity loss.

When is vertical scaling the right answer?

For stateful components that are hard to shard, a single primary database above all, and for anything where the simplicity of one node outweighs its ceiling. Buy the bigger box first, add replicas second, and shard only when writes exceed one machine.

Connections

Pairs with
Keep servers stateless · Spread traffic across servers · Admit only what you can finish · Pool database connections · Size the system from numbers
Pulls against
Default to a relational database

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Application Load BalancerLoad balancerCloud Load BalancingApplication Gateway, Azure Load BalancerNGINX, HAProxy, Envoy
Amazon EC2 Auto ScalingInstances ×12, Instance 13Compute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon CloudWatchMetricsCloud Monitoring, Cloud LoggingAzure MonitorPrometheus and Grafana, Datadog