# Exercise 2: Design Autoscaling for a Spiky Platform

`PetPal` on EKS runs:

| Workload | Traffic shape | Today |
|---|---|---|
| `api` (Node.js, stateless) | 200 rps nights → 3,000 rps evenings; TV ads cause 10× spikes within 60 s | 30 fixed replicas, requests 1 CPU / 1 Gi, avg CPU 12% |
| `thumbnail-worker` | SQS queue: bursts of 50,000 uploads after events, idle most of the day | 10 fixed replicas |
| `report-generator` | nightly batch at 02:00, 40 min | CronJob on dedicated on-demand nodes 24/7 |
| nodes | one managed node group of m5.2xlarge on-demand | 25 nodes, 18% utilization |

## Deliverables

1. **api**: HPA metric choice (CPU vs requests-per-second), targets, min/max, behavior; how to survive a 10× spike in 60 s (headroom, overprovisioning Pods, fast startup, pre-scaling before scheduled ads).
2. **Right-sizing**: how you'd use VPA in `Off` mode to fix requests first, and why that must happen before tuning HPA.
3. **thumbnail-worker**: KEDA ScaledObject on SQS (queue length per replica, min 0, max, cooldown), plus graceful shutdown for in-flight messages.
4. **report-generator**: run on Spot via Karpenter with retries; remove the 24/7 nodes.
5. **Nodes**: Karpenter NodePool(s) — capacity types, architectures, consolidation, limits, disruption budgets; which workloads may run on Spot.
6. **Safety**: PDBs, `maxReplicas`, cost guardrails, alerts (HPA at max, Pending Pods > 2 min).
7. Estimated **cost impact** (rough) versus today.

## Requirements Checklist

- [ ] HPA uses RPS or CPU with justified target and `behavior` (fast up, slow down); spike handling includes headroom/overprovisioning or scheduled pre-scaling.
- [ ] VPA recommendations used to correct requests (1 CPU at 12% → ~250m) before setting HPA targets.
- [ ] KEDA SQS trigger with scale to zero, sensible `queueLength`, max replicas tied to downstream limits, SIGTERM handling and visibility timeout.
- [ ] Batch on Spot with retries/checkpointing; no idle dedicated nodes.
- [ ] Karpenter NodePool with Spot + on-demand, arm64/amd64, consolidation, CPU limits; stateful/critical Pods excluded from Spot.
- [ ] PDBs and alerts defined; cost estimate shows the main savings (right-sizing, consolidation, Spot, scale to zero).
