Autoscaling
Scale replicas on CPU/GPU/queue depth while protecting cold-start UX.
In plain words
Autoscaling adds more copies of your app when it gets busy and removes them when it's quiet, so people aren't kept waiting and you don't pay for idle machines.
Think of it like…
A restaurant adding tables and staff as the dinner rush arrives: if setting up a new table takes ten minutes, you must start before the room is full, keep a few spare tables ready and not dismiss staff the moment the line shortens.
Where you’ve seen it
An HPA with a CPU target of 60% and 4 replicas at 90% average CPU computes desired replicas = ⌈4 × 90 / 60⌉ = 6. But an LLM gateway waiting on provider calls shows only 25% CPU while requests pile up, so the team scales on in-flight requests instead. Load is 20 requests per second with a 3.4 s average duration, so about 68 requests are in flight (Little's law); each replica handles 8 concurrently, which needs ⌈68 / 8⌉ = 9 replicas at 100% utilisation, or ⌈68 / (8 × 0.7)⌉ = 13 at a 70% target. A replica takes 90 seconds to become ready (image pull and model load) and metrics lag adds 30 seconds, so when traffic doubles to 40 requests per second, 13 replicas (capacity 13 × 8 / 3.4 = 30.6 requests per second) fall behind by 9.4 requests per second for 120 seconds: a backlog of about 1,128 requests. The team keeps 18 replicas as a floor during business hours (capacity 42 requests per second), uses a queue-depth KEDA scaler for workers (min 2, max 40) and sets a 10-minute scale-down stabilisation window to prevent flapping. GPU node pools need node scale-up too, which adds 3 to 5 minutes, so GPU replicas use scheduled minimums for known peaks.
Words you’ll hear
- HPA / KEDA
- Kubernetes controllers that scale pods on resource, custom or event-driven metrics.
- Scale-up lag
- Delay from demand rising to new replicas actually serving traffic.
- Stabilisation window
- A period that prevents scale-down from reacting to brief dips.
- Headroom
- Spare capacity kept to absorb spikes during scale-up lag.
Visual diagram
Theory & deep dive
Signals, formulas and targets
The HPA computes desired replicas = ⌈current replicas × current metric / target metric⌉: 4 replicas at 90% CPU with a 60% target gives ⌈6.0⌉ = 6. The question is *which metric*. For CPU-bound services (embedding or reranking workers) CPU works. For I/O-bound gateways (waiting on provider APIs) CPU stays low while latency climbs, so use concurrent requests per replica (from the proxy or app metrics), requests per second per replica or queue depth and age for workers (via KEDA with SQS, Kafka, Redis or Prometheus scalers). For GPU inference use GPU utilisation, KV-cache occupancy, running and waiting request counts from the serving engine, and time to first token; scale before the queue grows. By Little's law, in-flight requests = arrival rate × duration (20 × 3.4 = 68), so replicas needed = ⌈in-flight ÷ (per-replica concurrency × target utilisation)⌉ = ⌈68 / (8 × 0.7)⌉ = 13.
Targets belong below 100%: 60 to 70% leaves room for variance and the lag before new replicas serve. Set minimum replicas for availability and baseline load, maximum replicas to cap cost and protect downstream providers (a scaled-out fleet can exceed provider rate limits), and use scale-down stabilisation (for example 5 to 10 minutes) and slower scale-down policies so brief dips don't trigger removal followed by an immediate scale-up. Scale-to-zero suits asynchronous workers and rarely used services but adds cold-start latency, so avoid it on latency-critical endpoints.
20 req/s × 3.4 s = 68 in flight. Per replica concurrency 8. At 100%: ⌈68/8⌉ = 9. At a 70% target: ⌈68 / 5.6⌉ = ⌈12.14⌉ = 13 replicas, giving capacity 13 × 8 / 3.4 = 30.6 req/s versus 20 req/s of load.
Dealing with cold starts, spikes and cost
Scale-up lag has several components: metric scrape and evaluation intervals (15 to 60 s), scheduling, node provisioning (3 to 5 minutes if a new GPU node is needed), image pull and model load (60 + 28 s in the container lesson), and readiness-probe delays. In the example a doubling from 20 to 40 requests per second with 13 replicas leaves a deficit of 9.4 requests per second for 120 seconds, a backlog of about 1,128 requests, unless headroom exists. Mitigations: headroom (keep 18 replicas so capacity is 42 requests per second), predictive or scheduled scaling for known peaks (a daily 09:00 surge, a launch), warm pools of pre-initialised nodes, faster startup (smaller images, node-local weight caches, lazy loading), queue-based load levelling and graceful degradation (shed low-priority traffic, route to a smaller model or a provider API) while capacity catches up.
Cost control: GPU replicas are expensive, so use schedules (scale non-production to zero at night), spot capacity for tolerant workers with checkpoints, bin-packing and right-sizing, and measure cost per request against utilisation. Watch for flapping (oscillating replica counts) and cascading limits (more replicas increase provider calls and trigger 429s: cap max replicas and use shared rate limiters). Test autoscaling with load tests that mimic spikes (doubling in 30 seconds), verify latency and error behaviour during scale-up, and alert on pending pods, queue age and SLO burn. Document the scaling policy and review it after traffic or model changes, since per-replica concurrency and startup time change with the model.
Capacity 30.6 req/s, load 40 req/s: deficit 9.4 req/s × 120 s ≈ 1,128 queued requests. With 18 replicas (capacity 42 req/s) the deficit is zero; the extra 5 replicas (13 → 18) cost about 38% more during business hours but avoid two minutes of degraded latency per spike.
How it works, step by step
Step 1 of 41. Measure capacity per replica
Load-test to find per-replica concurrency and the latency at 70% utilisation; record cold-start time.
- Little's law
- rate × duration
When to use it & trade-offs
What it is
Autoscaling adjusts the number of running replicas (or nodes) to match demand. For AI services the choice of scaling signal is critical: CPU often misleads for I/O-bound LLM gateways (they wait on providers), so teams scale on requests in flight, queue depth or age, GPU utilisation or tokens per second. The common Kubernetes tools are the Horizontal Pod Autoscaler (HPA), event-driven KEDA (including scale to zero) and node-level cluster autoscalers or Karpenter. The main design constraints are scale-up lag (metrics delay, pod scheduling, image pull and model load can take minutes), cost (GPU nodes are expensive) and stability (avoid flapping), which lead to targets below 100% utilisation, minimum replicas and scale-down cool-downs.
Why it matters: Fixed capacity either wastes money at night or fails during a spike. LLM traffic is bursty and cold starts are slow, so naive autoscaling reacts after users have already queued or timed out. Getting the signal, target, minimums and headroom right is the difference between a service that holds its latency SLO through a launch and one that falls over for two minutes every time traffic doubles.
When to reach for it
- LLM gateways with variable traffic
- Queue workers for embeddings and ingestion
- GPU inference fleets
When to avoid or be careful
- Scaling an I/O-bound service on CPU alone
- Targets at 100% utilisation
- Unlimited max replicas that blow provider quotas
Scaling approaches
Strengths: Built in; simple.
Watch out: Misleading for I/O-bound LLM gateways.
Strengths: Reflects true saturation; scale to zero.
Watch out: More setup and metric plumbing.
Strengths: Capacity ready before demand.
Watch out: Needs forecasting; can waste capacity.
Interview signals & numbers to know
When this topic comes up
- “Latency spikes when traffic doubles”
- HPA and KEDA
- Queue depth scaling
- Cold-start lag
- Scale-down flapping
Benchmarks & rules of thumb
- ⌈current × metric / target⌉
- HPA formula (4 × 90/60 → 6)
- ⌈68 / (8 × 0.7)⌉ = 13
- replicas for 68 in-flight requests at 70% target
- 1,128
- requests backlogged during a 120 s scale-up lag
Common mistakes & follow-ups
Common interview mistakes
- Ignoring that model load makes new replicas useless for 90 seconds.
- Scaling down immediately after a spike and scaling up again.
- Forgetting that node provisioning adds minutes for GPU pods.
Likely interviewer follow-ups
- What metric would you scale an LLM gateway on, and why?
- How do cold starts affect autoscaling design?
- How do you prevent flapping?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Autoscaling with generative AI interview Q&A and hands-on practice.