Rapid elasticity in the cloud is automatic scaling: the cloud platform adds resources when load increases, removes them when load decreases, and the scaling is configured (not manual). The mechanisms: AWS Auto Scaling (EC2 instances, ECS tasks, Lambda concurrency), Kubernetes HPA (pods), GCP MIGs, Azure VMSS. The team that uses elasticity matches capacity to demand and saves money vs fixed-size clusters that over-provision most of the time.
Table of contents
- The four scaling mechanisms
- AWS Auto Scaling (the canonical example)
- Kubernetes HPA
- GCP and Azure equivalents
- The right metric to scale on
- The implementation checklist
- How this fits the rest of the stack
- FAQ
The four scaling mechanisms
Horizontal (scale out/in) - add or remove instances. The most common pattern. Stateless services scale linearly; stateful services need extra thought.
Vertical (scale up/down) - increase or decrease the size of an instance. Less common because of downtime (resize usually requires a reboot), but useful for databases (which are hard to scale horizontally).
Diagonal - combine horizontal and vertical. Scale out first, then scale up the existing instances when the horizontal limit is reached.
Auto-scaling - automate one of the above based on a metric (CPU, memory, request count, queue depth, custom business metric).
AWS Auto Scaling (the canonical example)
AWS Auto Scaling has three modes:
Target tracking - the simplest. Set a target value for a metric (e.g., 70% average CPU), and Auto Scaling adds/removes instances to maintain it. The team that uses this has predictable scaling.
Step scaling - add/remove N instances when a metric crosses a threshold. The team that has a clear step pattern (alert -> scale by 2) uses this.
Scheduled scaling - scale at a specific time. The team that has predictable traffic patterns (business hours, weekends) uses this.
Predictive scaling - use ML to predict load and pre-scale. The team that has spiky traffic and history uses this.
For ECS, Fargate, Lambda: similar concepts but the scaling unit is a task (ECS), a pod (EKS), or a concurrent execution (Lambda).
Kubernetes HPA
The Horizontal Pod Autoscaler scales the number of pods in a Deployment, ReplicaSet, or StatefulSet based on a metric.
Simple:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: myapp
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
This keeps the deployment at 2-10 pods, with the average CPU at 70%. As load increases, more pods spin up; as load decreases, they spin down.
For more advanced: KEDA (event-driven autoscaling, scales based on queue depth, Kafka lag, etc.), VPA (vertical, adjusts CPU/memory requests).
GCP and Azure equivalents
GCP Managed Instance Groups (MIGs) - similar to AWS Auto Scaling. Target CPU, custom metrics, scheduled scaling, predictive autoscaling.
Azure VMSS (Virtual Machine Scale Sets) - autoscale rules based on metrics. Azure Monitor provides the metrics; autoscale rules trigger the scaling.
Both integrate with their respective load balancers (GCP HTTP(S) Load Balancer, Azure Load Balancer) to distribute traffic to the new instances automatically.
The right metric to scale on
CPU - simple, universal, but a lagging indicator. The team that has a CPU-bound workload uses this.
Memory - similar to CPU but harder to monitor (containers share memory). The team that has a memory-bound workload uses this.
Request rate (RPS) - leading indicator for web services. The team that has a web API scales on requests per second.
Queue depth - the right metric for async workers. The team that has a BullMQ/Sidekiq/Celery worker scales on the queue length.
Custom business metric - the right metric for some workloads (e.g., scale on the number of pending orders, not on CPU). Requires a metrics pipeline (Prometheus, CloudWatch, etc.).
The team that picks the right metric scales the right way. The team that scales on CPU when the real bottleneck is the database scales the frontend but the database stays the bottleneck.
The implementation checklist
-
Make the workload stateless - horizontal scaling only works for stateless services. The team that has sessions in local memory cannot scale out; the team that has sessions in Redis or in JWTs can.
-
Set min and max replicas - min ensures availability (at least N instances), max ensures cost control (no surprise bills).
-
Use a meaningful metric - not just CPU. Pick the metric that actually predicts user impact (RPS, queue depth, custom).
-
Test the scaling - simulate load (with a load testing tool) and verify the scaling works as expected. The team that has not tested scaling has scaling that breaks at the worst time.
-
Add cooldown periods - prevent flapping (scale up, scale down, scale up, scale down). AWS Auto Scaling has cooldown; Kubernetes HPA has stabilization window.
-
Monitor the scaling - alert on scaling events, on failed scaling, on over-scaling (cost).
FAQ
How fast does Kubernetes HPA scale?
The default sync period is 15 seconds. A new pod is ready in 5-10 seconds. So scaling latency is roughly 20-30 seconds from metric change to new pod serving traffic. The team that needs faster uses KEDA with event-driven scaling (millisecond response).
What happens if I set minReplicas=0?
The deployment can scale to zero - no pods running, no cost. The catch: a new request triggers a cold start (the pod must spin up), which adds latency for the first request. The team that uses scale-to-zero has a cost-saving pattern but a latency penalty on cold start.
Can I scale based on a custom metric?
Yes - HPA supports custom metrics via the metrics adapter (Prometheus Adapter, Datadog Adapter, CloudWatch Adapter). The team that has a queue depth metric or a business metric exposes it to the metrics pipeline and uses it for scaling.
What is the difference between HPA and VPA?
HPA scales horizontally (more pods, same resources per pod). VPA scales vertically (same number of pods, more resources per pod). HPA is the right answer for stateless services; VPA is the right answer for stateful services or single-pod deployments.
How do I prevent scaling flapping?
Set a stabilization window. In Kubernetes, --horizontal-pod-autoscaler-downscale-stabilization (default 5 minutes). In AWS Auto Scaling, the cooldown period (default 300 seconds). The team that has flapping has too-aggressive scaling rules.
What is the difference between elasticity and scalability?
Scalability is the ability to handle increased load. Elasticity is the ability to scale up AND down, automatically, in response to demand. The team that has a scalable system can handle a spike; the team that has an elastic system handles it without manual intervention.
How this fits the rest of the stack
For a sense of what the full project costs before it commits, the RunxBuild hosting calculator shows the line items together. The API, the database, the storage, the worker, the bandwidth - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers.
Useful related references: