19 — Scale & Optimize

Auto-Scaling Endpoints#

InfrastructurePractice 1 min read
◆ The PatternScaling with demand — and scaling back to save money

Horizontal Pod Autoscaler (HPA) adds replicas when CPU/memory/custom metrics exceed thresholds. Scale-to-zero (Knative, serverless) eliminates idle costs but adds cold start latency. The right strategy depends on traffic patterns — bursty vs steady, latency-tolerant vs strict SLO.

// Interactive — replica count vs traffic
Traffic Load50%
Replicas—
Pattern bridge: Auto-scaling is dynamic position sizing for infrastructure — scale up exposure when opportunity (traffic) increases, scale down when it drops. The Monte Carlo approach helps simulate traffic scenarios for capacity planning.
Worked example

1,200 requests a second at 80 ms each means 96 in flight (Little’s law). With 8 concurrent requests per replica and a 70% utilisation target, that needs 18 replicas. Kubernetes’ own rule gets there step by step: 10 replicas at 92% CPU against a 70% target become 14 on the next evaluation.

Where it misleads
  • Scaling reacts after load arrives; with cold starts of tens of seconds, a spike is served by the replicas you already had.
  • CPU is a poor signal for GPU or I/O-bound models; scale on queue length or concurrency.
  • Scale-to-zero saves money at the price of a slow first request; not every endpoint can afford it.
In code
import math
rps, latency_s, per_replica = 1_200, 0.080, 8       # traffic, time per request, concurrent requests per replica
in_flight = rps * latency_s                         # Little's law
replicas = math.ceil(in_flight / (per_replica * 0.70))   # target 70% utilisation
current, cpu, target = 10, 0.92, 0.70
hpa = math.ceil(current * cpu / target)             # Kubernetes HPA rule
Python, in your browser — edit the code and run it again

Assumes import numpy as np and import pandas as pd. Each snippet carries its own example numbers; the comments say which are made up or simulated.

Sources
  • Kubernetes documentation, “Horizontal Pod Autoscaling”, kubernetes.io — the replica formula
  • J. D. C. Little, “A Proof for the Queuing Formula: L = λW”, Operations Research 9(3), 1961
  • Site Reliability Engineering: How Google Runs Production Systems, B. Beyer, C. Jones, J. Petoff & N. R. Murphy (eds.), O’Reilly, 2016

Last reviewed October 2026 · How this site checks its content · Report a mistake

Linked from 1
← Previous
Caching & Prediction Stores
Open in the full reader, with the topic sidebar