Vertical vs Horizontal Scaling
Scalability is the ability of a system to handle increased load. There are two fundamental approaches.
Vertical Scaling (Scale Up)
Before: After:
┌──────────┐ ┌──────────────────┐
│ 4 CPU │ → │ 16 CPU │
│ 8 GB RAM │ │ 64 GB RAM │
│ 100 GB │ │ 1 TB SSD │
└──────────┘ └──────────────────┘
Server A Bigger Server A
Pros:
- Simple to implement
- No code changes needed
- Strong consistency (single node)
- Lower operational complexity
Cons:
- Hardware limits (max CPU/RAM)
- Single point of failure
- Expensive at high end
- Downtime for upgrades
Horizontal Scaling (Scale Out)
Before: After:
┌──────────┐ ┌──────────┐ ┌──────────┐
│ 4 CPU │ → │ 4 CPU │ │ 4 CPU │
│ 8 GB RAM │ │ 8 GB RAM │ │ 8 GB RAM │
│ 100 GB │ │ 100 GB │ │ 100 GB │
└──────────┘ └──────────┘ └──────────┘
Server A Server A Server B
(same) (new)
Pros:
- Nearly unlimited scaling
- Fault tolerance (no single point of failure)
- Cost-effective (commodity hardware)
- Zero-downtime scaling
Cons:
- More complex architecture
- Requires stateless services or external state
- Data consistency challenges
- Network overhead between nodes
Comparison
| Aspect | Vertical | Horizontal |
|---|---|---|
| Complexity | Low | High |
| Fault Tolerance | Single point of failure | Multiple nodes |
| Maximum Scale | Hardware limited | Nearly unlimited |
| Cost | Expensive at scale | Commodity hardware |
| Downtime | Required for upgrades | Zero-downtime |
| Consistency | Strong (single node) | Eventual (distributed) |
When to Use Each
Vertical Scaling:
- Databases (easier than sharding)
- Small to medium workloads
- When simplicity is priority
- Legacy applications
Horizontal Scaling:
- Web servers behind load balancer
- Microservices architectures
- Systems requiring high availability
- Rapidly growing systems
Load Balancing
A load balancer distributes incoming requests across multiple servers.
Basic Load Balancing
┌─────────────┐
│ Client │
└──────┬──────┘
│
┌──────▼──────┐
│Load Balancer│
└──────┬──────
┌───────────┼───────────┐
│ │ │
┌──────▼──┐ ┌─────▼───┐ ┌────▼─────┐
│Server 1 │ │Server 2 │ │Server 3 │
└─────────┘ └─────────┘ └──────────┘
Load Balancing Algorithms
| Algorithm | How it Works | Best For |
|---|---|---|
| Round Robin | Sequential distribution | Equal-capacity servers |
| Weighted Round Robin | Proportional to weight | Mixed-capacity servers |
| Least Connections | Fewest active connections | Long-lived connections |
| IP Hash | Same client → same server | Session affinity needed |
| Random | Random selection | Simple, no state |
Round Robin Example
Request 1 → Server A
Request 2 → Server B
Request 3 → Server C
Request 4 → Server A (back to start)
Request 5 → Server B
...
Least Connections Example
Server A: 5 active connections
Server B: 2 active connections ← Next request goes here
Server C: 8 active connections
New Request → Server B (fewest connections)
Health Checks
Load Balancer periodically checks:
GET /health → 200 OK → Server is healthy ✓
GET /health → timeout → Server is down ✗
If server fails health check:
- Remove from rotation
- Stop sending traffic
- Retry health check later
- Re-add when healthy
Layer 4 vs Layer 7 Load Balancing
| Aspect | Layer 4 | Layer 7 |
|---|---|---|
| Info Used | IP + Port | Full HTTP request |
| Speed | Faster | Slower |
| Features | Basic routing | Content-based routing |
| SSL | Passthrough | Termination |
| Example | AWS NLB | AWS ALB, Nginx |
Auto-Scaling
Auto-scaling automatically adjusts the number of servers based on demand.
How Auto-Scaling Works
Metric (CPU, requests/sec)
│
▼
┌─────────────┐
│ Scaling │
│ Policy │
└──────┬──────
│
├── Above threshold → Scale Out (add servers)
└── Below threshold → Scale In (remove servers)
Scaling Policies
| Policy | Trigger | Action |
|---|---|---|
| Target Tracking | Metric hits target | Maintain target |
| Step Scaling | Metric crosses thresholds | Add/remove instances in steps |
| Scheduled | Time-based | Scale at specific times |
| Predictive | ML-based prediction | Pre-scale for expected load |
Auto-Scaling Configuration
# Example: AWS Auto Scaling Group
AutoScalingGroup:
MinSize: 2
MaxSize: 10
DesiredCapacity: 4
ScalingPolicy:
TargetValue: 70.0 # CPU utilization
MetricType: AverageCPUUtilization
ScaleOut:
Increase: 2 instances
Cooldown: 300 seconds
ScaleIn:
Decrease: 1 instance
Cooldown: 600 seconds
Scaling Metrics
Common Metrics for Scaling Decisions:
1. CPU Utilization
- Scale out when > 70%
- Scale in when < 30%
2. Request Count per Target
- Scale out when > 1000 RPS per instance
3. Memory Utilization
- Scale out when > 80%
4. Queue Depth
- Scale out when > 100 messages per worker
5. Custom Metrics
- Business-specific: orders/min, search queries/sec
Auto-Scaling Best Practices
- Set minimum capacity: Always have at least 2 instances for availability
- Set maximum capacity: Prevent runaway scaling costs
- Use cooldown periods: Avoid thrashing (rapid scale in/out)
- Monitor scaling events: Alert on unusual scaling patterns
- Test scaling: Regularly verify scaling works as expected
Scaling Challenge: Cold Start
Problem: New instance takes time to start
Solution: Keep warm instances
- Minimum capacity > 0
- Pre-warming instances
- Using reserved capacity for baseline
``
Practice Problems
Design a scalable Scalability system. Cover high-level architecture, data model, and API design.
Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliabilityHow would you scale Scalability to handle 10x the current load? Identify bottlenecks and solutions.
Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decompositionAnalyze potential failure modes for Scalability and design mitigation strategies.
Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradationQuiz
1. What is the main advantage of horizontal scaling over vertical scaling?
2. Which load balancing algorithm routes the same client to the same server?
3. What happens during auto-scaling scale-out?
4. Why are cooldown periods important in auto-scaling?
Flashcards
Question
What is the difference between vertical and horizontal scaling?
Click to reveal answer
Answer
Vertical scaling (scale up) uses bigger machines. Horizontal scaling (scale out) uses more machines. Horizontal is preferred for large-scale systems due to fault tolerance and unlimited scaling potential.
Question
What is a load balancer?
Click to reveal answer
Answer
A load balancer distributes incoming requests across multiple servers to ensure no single server is overwhelmed. It performs health checks and routes traffic to healthy instances.
Question
What are the main load balancing algorithms?
Click to reveal answer
Answer
Round Robin (sequential), Weighted Round Robin (proportional), Least Connections (fewest active), IP Hash (sticky sessions), Random (random selection).
Question
What triggers auto-scaling?
Click to reveal answer
Answer
Common triggers: CPU utilization > 70%, request count per target, memory utilization > 80%, queue depth, or scheduled time-based scaling.
Question
What is the cold start problem in auto-scaling?
Click to reveal answer
Answer
New instances take time to start and become ready. Solutions: keep minimum capacity > 0, pre-warm instances, use reserved capacity for baseline load.
Revision Notes
Key Takeaways
- 1.Horizontal scaling is preferred for large-scale systems
- 2.Load balancers distribute traffic and perform health checks
- 3.Auto-scaling adjusts capacity based on demand
- 4.Always set min/max capacity to control costs and availability
- 5.Use cooldown periods to prevent scaling thrashing
Interview Tips
- •Always discuss scaling strategy early in system design interviews
- •Consider both current and future scale requirements
- •Discuss tradeoffs between vertical and horizontal scaling
- •Mention auto-scaling for cloud-based architectures
Cheat Sheet
Scalability - Cheat Sheet
Vertical Scaling (Scale Up):
- Bigger machines
- Simple but limited
- Single point of failure
Horizontal Scaling (Scale Out):
- More machines
- Complex but unlimited
- Fault tolerant
Load Balancing Algorithms:
| Algorithm | Best For |
|---|---|
| Round Robin | Equal servers |
| Weighted RR | Mixed capacity |
| Least Connections | Long connections |
| IP Hash | Session affinity |
Auto-Scaling:
- Target Tracking: Maintain metric target
- Step Scaling: Add/remove in steps
- Scheduled: Time-based
- Predictive: ML-based
Key Metrics:
- CPU > 70% → Scale out
- CPU < 30% → Scale in
- Use cooldown periods