What is autoscaling, and what are horizontal and vertical scaling?
BeginnerConceptInfrastructure & cloud basics
Answer
In one line: Scaling means adjusting capacity to match load; horizontal scaling adds or removes instances, vertical scaling makes one instance bigger or smaller, and autoscaling does this automatically based on metrics.
Vertical scaling (scale up/down)
- Give a machine more CPU or memory:
t3.medium→t3.xlarge. - Pros: simple, no code changes.
- Cons: hardware limit; often needs a restart; a single machine is still a single point of failure.
Horizontal scaling (scale out/in)
- Add more instances behind a load balancer: 3 pods → 10 pods.
- Pros: almost unlimited; better fault tolerance.
- Cons: the app should be stateless (sessions in Redis, files in S3), so any instance can serve any request.
Autoscaling examples:
- AWS Auto Scaling Groups / Azure VM Scale Sets — add VMs when average CPU is above 60%.
- Kubernetes Horizontal Pod Autoscaler (HPA):
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler spec: scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: orders } minReplicas: 3 maxReplicas: 20 metrics: - type: Resource resource: { name: cpu, target: { type: Utilization, averageUtilization: 70 } } - KEDA — scale on events like queue length or Kafka lag, even down to zero.
- Cluster Autoscaler / Karpenter — add Kubernetes nodes when pods can't be scheduled.
- Vertical Pod Autoscaler (VPA) — recommends or sets better CPU/memory requests.
- Serverless (Lambda) scales automatically per request.
Good practices:
- Set sensible min and max values (min for availability, max to cap cost).
- Choose the right metric — CPU for compute-heavy apps, queue depth or requests per second for others.
- Scheduled scaling for predictable peaks (for example 9 a.m. office traffic).
- Make startup fast, or scaling won't keep up with spikes.
Tip for interviews: mention stateless design as the requirement for horizontal scaling, and give an HPA example.
More on Infrastructure & cloud basics
What is the difference between declarative and imperative infrastructure?BeginnerWhat is idempotency, and why is it important in automation?BeginnerWhat is configuration drift, and how do you prevent it?BeginnerWhat is immutable infrastructure?BeginnerWhat does "pets vs cattle" mean?BeginnerWhat is cloud computing, and what are IaaS, PaaS and SaaS?Beginner