Lesson 2 � Beginner

Scalability: Vertical vs Horizontal

Scalability ka matlab hai system ko growing demand ke saath handle karna. Vertical ya horizontal � dono approaches hain, dono ke trade-offs hain. Chalo samajhte hain.

? 20 min✓ Beginner✓ System Design basics

Scalability hoti kya hai?

WHAT

Scalability ek system ki capability hai ki wo badhti load ko handle kar sake bina performance drop ke. Agar tumhara system 100 users handle karta tha aur ab 10,000 users aa gaye, toh system smoothly kaam kare � ye scalability hai.

WHEN

Jab business grow ho raha ho, users badh rahe ho, data badh raha ho. Instagram pe photos upload ho rahe hain, Netflix pe videos stream ho rahe hain � ye sab scalability ke wajah se possible hai.

WHERE

Har production system mein. Twitter pe 500M tweets daily aate hain, Uber pe millions of rides track hote hain � ye sab scalable systems hain jo millions of requests handle karte hain.

Vertical Scaling (Scale Up)

Vertical scaling matlab hai ek hi server ko powerful banana � zyada CPU, zyada RAM, zyada storage. Jaise phone ka RAM 4GB se 8GB kar do.

# Vertical Scaling Example

Before: 4 GB RAM, 2 CPU cores ? 1000 users handle
After: 16 GB RAM, 8 CPU cores ? 5000 users handle

# Pros:
✓ Simple � ek server hai, manage karna easy
✓ No code changes � application same rahega
✓ Consistency guaranteed � single database

# Cons:
✓ Hardware limit � maximum RAM/CPU hota hai
✓ Single point of failure � server down to sab down
✓ Expensive � high-end servers costly hain
✓ Downtime upgrade mein � server band karna padega

# Use case: Startups, MVP, low traffic applications
# Real example: MySQL server upgraded to bigger machine
Mental Model: Vertical scaling jaise ek auto-rickshaw mein zyada passengers bithaana. Auto big nahi hoga, sirf zyada bheed ho jayegi. Ek limit ke baad auto phat jayega � vertical scaling mein bhi ek limit hoti hai.

Horizontal Scaling (Scale Out)

Horizontal scaling matlab hai multiple servers lagana � har server pe thoda thoda load. Jaise auto-rickshaw ki jagah 10 cars lagado.

# Horizontal Scaling Example

Before: 1 server ? 1000 users
After: 10 servers ? 10,000 users (1000 per server)

# Pros:
✓ No hardware limit � unlimited servers add kar sakte ho
✓ Fault tolerant � 1 server down, baaki kaam karte rahein
✓ Cost effective � cheap servers use kar sakte ho
✓ Zero downtime � naya server add karo bina band kiye

# Cons:
✓ Complex � load balancing chahiye
✓ Data consistency mushkil � distributed data sync karo
✓ Code changes � stateless design chahiye
✓ Session management � sticky sessions ya shared session store

# Use case: Large scale applications, cloud-native
# Real example: Netflix, YouTube, Instagram � sab horizontal scale

Vertical vs Horizontal � Kab kya use karo?

# Comparison Table

| Feature | Vertical (Scale Up) | Horizontal (Scale Out) |
|------------------|------------------------|--------------------------|
| Complexity | Low | High |
| Cost | Expensive per server | Cheap per server |
| Limit | Hardware limit | Unlimited |
| Downtime | Yes (during upgrade) | No |
| Fault Tolerance | Single point failure | High availability |
| Data Consistency | Easy | Complex (CAP theorem) |
| Use Case | Startups, MVP | Large scale production |

# Decision Framework:
- Users < 10,000 ✓ Vertical
- Users > 100,000 ✓ Horizontal
- Budget tight ✓ Vertical initially
- 24/7 uptime ✓ Horizontal (redundancy)

Database Scaling

Sabse mushkil hota hai database scale karna. Application servers toh easily add kar loge, but database✓ Ek hi database hai!

# Database Scaling Strategies

1. Vertical Scaling
 ✓ Bigger server, more RAM, faster disk
 ✓ Limit: ~4TB RAM max available

2. Read Replicas
 ? 1 Primary DB (writes) + N Replica DBs (reads)
 ✓ Most apps read-heavy hote hain (95% read, 5% write)
 ✓ MySQL, PostgreSQL support karte hain

3. Sharding
 ✓ Data ko chunks mein divide karo
 ✓ User ID 1-1M ✓ Shard 1, 1M-2M ✓ Shard 2
 ✓ Horizontal partitioning of data

4. Caching Layer
 ✓ Frequently accessed data cache mein rakho
 ✓ Redis/Memcached for hot data
 ✓ Database load 80% tak kam ho sakta hai

# Real example: Facebook
? 3 billion users ka data ek DB mein nahi aayega
✓ Sharding across thousands of MySQL servers
✓ Read replicas for different regions
Key Insight: Database scaling sabse critical hai kyunki state (data) hota hai database mein. Application servers stateless hote hain � easily add/remove kar sakte ho. Database mein data consistency maintain karna challenging hai.

Real-world example: Instagram

# Instagram Architecture (Simplified)

2010: 1 server (vertical scaling)
 ? 1 box: Web server + DB + Cache
 ? 10,000 users

2012: 100K users ✓ Multiple servers (horizontal)
 ✓ Load balancer + 5 web servers
 ✓ Read replicas for database

2020: 1 billion users ✓ Distributed system
 ✓ Thousands of servers
 ✓ Sharded databases
 ✓ CDN for photos/videos
 ✓ Microservices architecture

# Key Lesson:
Start vertical, migrate to horizontal when needed.
Don't over-engineer from day 1.

Exercise

Question: Vertical scaling ki sabse badi limitation kya hai? (4 words ya kam mein)

Question: Horizontal scaling mein kya chahiye traffic distribute karne ke liye? (2-3 words)

Common mistakes

Lesson complete?

Scalability samajh aa gayi✓ Ab Load Balancing seekhte hain � traffic ko kaise distribute karte hain multiple servers mein.