Horizontal and Vertical Scaling

Compare scaling up with scaling out and understand their operational tradeoffs.

Lesson content

🎯 Goal By the end of this lesson, you'll understand: What is Scaling? What is Vertical Scaling? What is Horizontal Scaling? Differences between them Advantages & Disadvantages Real-world examples Interview questions What is Scaling? Scaling means increasing the capacity of an application so it can handle more users, requests, or data. Scaling helps improve: Performance Availability Reliability User Experience Simple Definition: Scaling means increasing your application's ability to handle more traffic. Why Do We Need Scaling? As an application grows: More users visit More API requests arrive More database queries occur More memory and CPU are required Scaling helps maintain good performance. Real-Life Example 🏪 Imagine a grocery store. 10 Customers │ ▼ 1 Billing Counter 100 Customers │ ▼ Need More Capacity You can either: Upgrade the existing billing counter (Vertical Scaling) Add more billing counters (Horizontal Scaling) What is Vertical Scaling? Vertical Scaling means upgrading the existing server by adding more resources. Examples: More CPU More RAM Faster SSD Better Network Simple Definition: Vertical Scaling means making one server more powerful . Vertical Scaling Diagram Before +-------------+ | 4 GB RAM | | 2 CPU | +-------------+ │ Upgrade ▼ +-------------+ | 32 GB RAM | | 16 CPU | +-------------+ Advantages of Vertical Scaling ✅ Easy to implement ✅ No application changes required (in many cases) ✅ Simple deployment ✅ No load balancer needed Disadvantages of Vertical Scaling ❌ Hardware has limits ❌ Can become expensive ❌ Single point of failure ❌ Usually requires downtime for major hardware upgrades What is Horizontal Scaling? Horizontal Scaling means adding more servers instead of upgrading one server. Traffic is distributed across multiple servers. Simple Definition: Horizontal Scaling means adding more servers to share the workload. Horizontal Scaling Diagram Load Balancer │ ┌──────────────┼──────────────┐ ▼ ▼ ▼ +---------+ +---------+ +---------+ | Server1 | | Server2 | | Server3 | +---------+ +---------+ +---------+ Advantages of Horizontal Scaling ✅ High Availability ✅ Better Fault Tolerance ✅ Easier to scale as traffic grows ✅ Supports millions of users Disadvantages of Horizontal Scaling ❌ More complex architecture ❌ Requires a load balancer ❌ Data synchronization can be challenging ❌ Distributed debugging is harder Vertical vs Horizontal Scaling Feature Vertical Scaling Horizontal Scaling Servers One Multiple Upgrade CPU/RAM Add Servers Cost Can become expensive Flexible as you grow Downtime Possible Usually lower with proper setup Fault Tolerance Low High Scalability Limited by hardware Very High Complexity Simple More Complex Node.js Example Vertical Scaling Node.js App │ ▼ Upgrade Server 2 CPU ↓ 16 CPU Only one server is upgraded. Horizontal Scaling Client │ ▼ Load Balancer │ ├── Node Server 1 ├── Node Server 2 ├── Node Server 3 Traffic is shared among multiple Node.js servers. Request Flow Vertical Scaling Client │ ▼ One Powerful Server │ ▼ Database Horizontal Scaling Client │ ▼ Load Balancer │ ▼ Multiple Servers │ ▼ Database Real-Life Example 🚗 Highway Toll Plaza Vertical Scaling One Toll Booth ↓ Upgrade Booth Faster Machine More Staff Horizontal Scaling Toll Plaza Booth 1 Booth 2 Booth 3 Booth 4 Cars are distributed across multiple booths. Cloud Example Vertical Scaling Small Cloud VM ↓ Large Cloud VM Example: 2 vCPUs → 8 vCPUs 4 GB RAM → 32 GB RAM Horizontal Scaling VM 1 VM 2 VM 3 VM 4 Multiple virtual machines run the same application behind a load balancer. Which One Should You Use? Vertical Scaling Best for: Small applications Development environments Applications that don't need high availability Horizontal Scaling Best for: Large applications Cloud environments High-traffic websites Microservices Applications requiring high availability Load Balancer A Load Balancer distributes incoming requests across multiple servers. Client Requests │ ▼ +----------------+ | Load Balancer | +----------------+ │ ┌────┼────┐ ▼ ▼ ▼ Server1 Server2 Server3 Benefits: Better performance High availability Even distribution of traffic Interview Question What is Vertical Scaling? Answer Vertical Scaling increases the capacity of a single server by adding more CPU, RAM, storage, or other hardware resources. What is Horizontal Scaling? Answer Horizontal Scaling increases capacity by adding more servers and distributing traffic between them. Which scaling provides better fault tolerance? Answer Horizontal Scaling Which scaling is easier to implement? Answer Vertical Scaling Why is a Load Balancer used? Answer A Load Balancer distributes incoming requests across multiple servers to improve performance, availability, and reliability. Best Practices ✅ Start with Vertical Scaling for small applications. ✅ Use Horizontal Scaling as traffic increases. ✅ Place a Load Balancer in front of multiple servers. ✅ Design applications to be stateless when using Horizontal Scaling. ✅ Store shared session data in a central store (e.g., Redis) if needed. ❌ Don't rely on a single server for high-traffic production systems. ❌ Don't store user sessions only in server memory when requests can reach different servers. Key Points ✅ Scaling improves application performance and capacity. ✅ Vertical Scaling upgrades one server. ✅ Horizontal Scaling adds more servers. ✅ Horizontal Scaling provides better availability and fault tolerance. ✅ Vertical Scaling is simpler but limited by hardware. ✅ Load Balancers are commonly used with Horizontal Scaling. 🧠 Easy Formula Application Slow │ ▼ Need More Capacity │ ├──────────────┐ ▼ ▼ Vertical Horizontal Upgrade Add Servers CPU ↑ Server 1 RAM ↑ Server 2 Storage ↑ Server 3 Vertical = Bigger Server Horizontal = More Servers Load Balancer = Distributes Requests Remember: ⬆️ Vertical Scaling = Upgrade one server ➕ Horizontal Scaling = Add more servers ⚖️ Load Balancer = Shares traffic across servers 🚀 Horizontal Scaling is preferred for large, highly available applications 🖥️ Vertical Scaling is simpler for small applications