Kubernetes has a built-in Horizontal Pod Autoscaler (HPA) that watches a metric — like requests per second — and scales the number of replicas up or down to keep the metric at a target level. For example, if your target is 100 RPS per pod and you're seeing 400 RPS total, you want 4 replicas. If you're seeing 50 RPS total, you'd want 1 — but never below your configured minimum.
You're going to implement the core scaling decision as a pure function. No Kubernetes API calls — just math and bounds enforcement.