Skip to content

< all problems97 · Level 08, Production

Fix the Autoscaler That Watches CPU

medium · debug · Production

The agent workers are autoscaled on CPU, the way the web tier is. Agent workers spend their time waiting on a model, so CPU sits at 12 percent while the queue grows to two hundred and every task misses its latency target. Fix scale(metrics, state).

metrics is {"tick", "queue_depth", "workers", "cpu"}. state is a dict that persists between calls, yours to use. CONFIG has min, max, target_per_worker and cooldown. The function returns the number of workers to run from this tick.

  1. The signal is the queue. The desired count is queue_depth / target_per_worker, rounded up, then clamped to [min, max].
  2. Scaling up happens at once, to the desired count, and records the tick in state["last_up"].
  3. Scaling down waits: it is allowed only when at least cooldown ticks have passed since the last scale-up, and it removes at most one worker per tick.
  4. With no scale-up on record yet, scaling down is allowed.

simulate(policy, arrivals) runs a day of traffic through your policy and reports breaches (ticks over the latency target) and worker_ticks (what you paid). The tests want zero breaches on DAY for fewer worker-ticks than running the maximum all day, and the cooldown rule on its own.

Kubernetes' HPA scales on CPU by default. For a service whose work is waiting, the right metric is a custom one, and the queue is the honest one.