Lesson 3 � Intermediate

Load Balancers & Hashing

Load Balancer ek traffic controller hai jo incoming requests ko multiple servers mein distribute karta hai. Bina load balancer ke horizontal scaling possible nahi hai.

? 22 min✓ Intermediate✓ Scalability basics

Load Balancer hota kya hai?

WHAT

Load Balancer ek component hai jo client requests ko multiple backend servers mein distribute karta hai. Ye ensure karta hai ki koi ek server pe zyada load na aaye aur system smoothly kaam kare.

WHEN

Jab tumhare paas ek se zyada servers hain (horizontal scaling) tab load balancer chahiye. Ye traffic ko evenly distribute karta hai � like a traffic police at a junction.

WHERE

Application load balancer (ALB), Network load balancer (NLB), Database load balancer � har jagah. AWS ELB, Nginx, HAProxy � popular load balancers hain.

# Load Balancer Flow

Client Request ✓ Load Balancer ✓ Server 1 (30% traffic)
 ✓ Server 2 (35% traffic)
 ✓ Server 3 (35% traffic)

# Types of Load Balancers:

1. L4 (Transport Layer)
 ✓ IP + Port based routing
 ✓ Fast, less intelligent
 ✓ Use: TCP/UDP traffic

2. L7 (Application Layer)
 ✓ HTTP header, URL, cookie based routing
 ✓ More intelligent, content-aware
 ✓ Use: HTTP traffic, microservices

# Popular Load Balancers:
- Nginx (L7, open source)
- HAProxy (L4+L7, open source)
- AWS ALB/NLB (managed)
- Cloudflare (global LB)

Load Balancing Algorithms

Load balancer ko decide karna padta hai ki next request kis server ko jaaye. Ye algorithms use hote hain:

# 1. Round Robin
✓ Har request next server ko
✓ Server 1 ✓ Server 2 ✓ Server 3 ✓ Server 1...

# 2. Weighted Round Robin
✓ Powerful servers ko zyada weight
✓ Server 1 (8GB RAM, weight 3) ✓ Server 2 (4GB RAM, weight 1)

# 3. Least Connections
✓ Jis server pe sabse kam active connections hain
✓ Server 1: 5 connections, Server 2: 2 connections ✓ Server 2

# 4. IP Hash
✓ Client IP se hash nikalo, uss server pe bhejo
✓ Same client always same server (sticky sessions)

# 5. Least Response Time
✓ Jis server ka response time sabse kam hai
✓ Server 1: 50ms, Server 2: 30ms ✓ Server 2

# Best Practice: Round Robin for stateless apps
# Least Connections for variable request times
Mental Model: Load Balancer jaise restaurant ka receptionist. Agar 3 waiters hain toh receptionist decide karta hai kaunsa customer kaunse waiter ke paas jaayega. Har waiter pe equal load hona chahiye � na koi idle baitha rahe, na koi overworked.

Consistent Hashing

Consistent Hashing ek technique hai jo distributed systems mein data ko evenly distribute karti hai. Jab naya server add ya remove ho toh minimum data reshuffle ho.

# Problem: Simple Hashing
hash(key) % N ✓ Server number

# Agar N change ho (server add/remove) ✓ sab data reshuffle!
# 1000 servers the, 1 naya aaya ? 99.9% data shift!

# Solution: Consistent Hashing
✓ Ring shape mein servers aur keys place karo
✓ Har key nearest server pe jaati hai
✓ Server add/remove ✓ sirf uske paas ka data shift

# Ring visualization:
Server A (0�)  Server B (120�)  Server C (240�)
Key 1 (10�) ✓ Server A
Key 2 (130�) ✓ Server B
Key 3 (250�) ✓ Server C

# Agar Server B remove ho:
Key 2 (130�) ✓ ab Server C (sirf 1 key shift!)

# Real use: Cassandra, DynamoDB, Memcached
# Virtual nodes for better distribution

Health Checks & Failover

Load balancer sirf traffic distribute nahi karta, ye servers ki health bhi check karta hai. Agar koi server down hai toh usko traffic mat bhejo.

# Health Check Process

1. Load balancer periodically pings each server
2. Server responds with status code
3. If server doesn't respond ✓ mark as unhealthy
4. Stop sending traffic to unhealthy server
5. When server recovers ✓ mark as healthy, resume traffic

# Types of Health Checks:
- TCP Check: Port open hai ya nahi
- HTTP Check: /health endpoint 200 return kare
- Custom Check: Database connection, disk space

# Failover:
- Primary load balancer down ✓ Secondary takes over
- Active-Passive: 1 active, 1 standby
- Active-Active: Dono active, round robin

# Real example: AWS ELB
✓ Default: 30 second interval, 5 failed checks = unhealthy
✓ Automatic failover to healthy instances

Real-world example: Netflix

# Netflix Load Balancing Architecture

Global Load Balancer (Route 53)
 ✓ Regional Load Balancers (US, EU, APAC)
 ✓ Zone Load Balancers (AZ-1, AZ-2)
 ✓ Service Load Balancers (per microservice)

# Why multiple levels?
- Global: Route user to nearest region
- Regional: Handle region-level failures
- Zone: Handle AZ-level failures
- Service: Distribute within microservice

# Result:
- 200M+ users worldwide
- 99.99% uptime
- User always connected to nearest server

Exercise

Question: Load balancing mein sabse simple algorithm konsa hai jo har request ko sequence mein servers ko bhejta hai? (2 words)

Question: Consistent Hashing ka main benefit kya hai jab server add/remove ho? (3 words)

Common mistakes

Lesson complete?

Load Balancing samajh aa gayi✓ Ab Caching Strategies seekhte hain � frequently accessed data ko kaise cache karte hain.