नमस्ते दोस्तों!
स्वागत है The Easy Master पर!
आपने system design aur scaling के बारे में सीखा। अब एक और बहुत जरूरी topic – Load Balancing। जब आपके पास multiple servers hain (horizontal scaling), तो traffic ko unme distribute kaise karein? Kaise ensure karein ki koi server overload na ho aur koi idle na rahe?
Load balancer yahi kaam karta hai – ye ek “traffic police” hai jo incoming requests ko healthy servers ke beech distribute karta hai।
Jab maine pehli baar load balancer seekha, toh mujhe lagta tha ki bas round robin hi kaafi hai। Lekin jab maine production system dekha – jaha servers की capacity alag thi, sticky sessions chahiye the, toh samajh aaya ki alag-alag algorithms ke alag use cases hote hain।
इस Load balancing tutorial Hindi में main aapko sikhata hoon:
✅ Round Robin aur Weighted Round Robin – simple distribution, equal ya unequal server capacity
✅ Least Connections – active connections ke hisaab se smart routing
✅ Hashing (IP Hash aur Consistent Hashing) – session persistence aur minimal remapping
✅ Algorithms comparison – kab kaunsa use karein
✅ Real-world configs – NGINX, HAProxy, AWS ALB
✅ Common mistakes aur best practices
Chaliye load balancer ki duniya mein step rakhte hain! 🚦🚀
Table of Contents
1. Load Balancer Kya Hai? – Traffic Police of Servers
Load balancer ek device ya software hai jo incoming network traffic को multiple backend servers के बीच distribute karta hai।
Load balancer kyun use karein?
| Benefit | Explanation |
|---|---|
| High availability | Agar ek server fail ho jaaye, load balancer traffic को healthy servers pe bhejta hai |
| Scalability | Traffic बढ़ने पर aap easily servers add kar sakte ho |
| Performance | Requests evenly distribute hone se koi server overload nahi hota |
| SSL termination | Load balancer HTTPS decrypt kar sakta hai, backend servers par load kam |
| Health checks | Regularly check karta hai ki server alive hai ya nahi |
Load balancer algorithms decide karte hain ki request kaun se server pe jaayegi। Let’s understand the most important ones.
2. Round Robin – Sabse Simple Algorithm
Round robin सबसे simple algorithm है – requests को एक-एक करके sequentially servers में distribute kiya जाता है।
How it works:
- Servers list mein order maintain रखता है
- Har naye request ko next server bhejta है
- Last server के बाद first server par wapas aata है
Example with 3 servers (S1, S2, S3):
- Request 1 → S1
- Request 2 → S2
- Request 3 → S3
- Request 4 → S1
- Request 5 → S2 … and so on
Pros:
- Simple, easy to implement
- Works well if all servers have equal capacity
Cons:
- Sab servers equally capable hone chahiye – nahi toh slower server bottleneck ban sakta है
- Current server load consider nahi karta
- Session persistence nahी है (unless configured)
When to use: Homogeneous server pools, stateless applications, simple setups।
3. Weighted Round Robin – Different Capacities Ke Liye
Weighted round robin – servers ko weight assign karte ho jo unकी capacity reflect karta hai। Higher weight वाले servers ko zyada requests milti hain।
Example:
- Server A: weight 5 (more powerful)
- Server B: weight 3
- Server C: weight 2
Out of 10 requests, A gets 5, B gets 3, C gets 2।
Pros:
- Handles heterogeneous servers (different capacities)
- Flexible – weights adjust kar sakte ho
Cons:
- Static weights – real-time load changes reflect nahi karte
- Still doesn’t consider current active connections
When to use: When servers have different hardware capabilities, gradual migration to new servers।
4. Least Connections – Smart Load Distribution
Least connections traffic को उस server pe bhejता है जिसके पास currently सबसे कम active connections hain।
How it works:
- Load balancer tracks active connections per server
- New request goes to server with minimum active connections
Example:
- Server A: 100 connections
- Server B: 80 connections
- Server C: 120 connections
- New request → Server B (least connections)
Pros:
- Handles requests of varying lengths well
- Dynamic – real-time load reflect karta hai
- Better resource utilization
Cons:
- Requires tracking connection count per server
- Can falter with very long-lived connections
When to use: When request processing time varies significantly, long-lived connections (WebSocket), unequal workloads।
5. Hashing Algorithms – IP Hash aur Consistent Hashing
Hashing algorithms ensure ki same client के requests hamesha same server pe jaayein – useful for session persistence without sticky cookies।
5.1 IP Hash (Source IP Hash)
IP hash client ki IP address (or a portion) को hash karke server select karta hai।
How it works: hash(client_IP) % number_of_servers – result determines server index।
Pros:
- Simple, deterministic – same client always same server
- No overhead of cookies
Cons:
- If server pool size changes, mapping changes drastically – ~90% remapping
- Many clients behind same NAT (office, college) map to same server
When to use: Small-scale, server pool doesn’t change often, NAT not an issue।
5.2 Consistent Hashing
Consistent hashing – server pool change hone par bhi only ~13% keys remap hoti hain (vs 90% in IP hash)।
How it works:
- Servers and keys mapped on a hash ring
- Each request goes to next server clockwise
- Adding or removing server affects only immediate neighbors
Pros:
- Minimal remapping on scaling (~13% vs 90%)
- Ideal for stateful applications, distributed caches
- Predictable routing even when backend count changes
Cons:
- Implementation complexity
- Virtual nodes needed to handle load imbalance
When to use: Distributed caching (Redis Cluster), stateful apps, dynamic scaling environments।
6. Algorithms Comparison – कब कौनसा use करें?
| Algorithm | Basis | Best for | Sticky | Complexity |
|---|---|---|---|---|
| Round Robin | Sequential | Equal servers, stateless | No | Low |
| Weighted RR | Weight factor | Unequal capacities | No | Low |
| Least Connections | Active connections | Variable request length, WebSocket | No | Medium |
| IP Hash | Client IP | Simple sticky sessions | Yes | Low |
| Consistent Hashing | Hash ring | Distributed caches, dynamic scaling | Yes | High |
Decision framework:
text
Is session persistence required?
│
├── No → Use Round Robin (equal servers) or Least Connections (variable load)
│
└── Yes → Is server pool dynamic (auto-scaling)?
│
├── No → Use IP Hash
│
└── Yes → Use Consistent Hashing7. Real-World Configurations – NGINX, HAProxy, AWS ALB
7.1 NGINX Configuration
Round Robin (default):
upstream backend {
server 10.0.0.1:80;
server 10.0.0.2:80;
server 10.0.0.3:80;
}Weighted Round Robin:
upstream backend {
server 10.0.0.1:80 weight=5;
server 10.0.0.2:80 weight=3;
server 10.0.0.3:80 weight=2;
}Least Connections:
upstream backend {
least_conn;
server 10.0.0.1:80;
server 10.0.0.2:80;
}IP Hash:
upstream backend {
ip_hash;
server 10.0.0.1:80;
server 10.0.0.2:80;
}7.2 HAProxy Configuration
haproxy
backend web_backend
# Round Robin (default)
balance roundrobin
# Least Connections
balance leastconn
# Source IP Hash
balance source
server web1 10.0.0.1:80 weight 3
server web2 10.0.0.2:80 weight 27.3 AWS ALB / Cloud Load Balancers
- Round Robin: Default for most cloud LBs
- Weighted Round Robin: AWS ALB supports via target group weights
- Least Connections: Supported by AWS NLB, Huawei ELB
- Consistent Hashing: Supported by Alibaba Cloud ALB (source IP, URL)
8. Common Mistakes (aur Unka Solution!)
| Mistake | Why it’s wrong | Solution |
|---|---|---|
| Using Round Robin for uneven workloads | Some servers overloaded | Use Least Connections |
| Using IP Hash without considering NAT | All office users go to same server | Use Consistent Hashing or cookie-based sticky |
| Not configuring health checks | Unhealthy servers still receive traffic | Configure health checks with proper thresholds |
| Ignoring session persistence needs | Users randomly logged out | Use IP Hash, Consistent Hashing, or sticky cookies |
| Static weights in auto-scaling | Weights don’t adjust to real capacity | Use Least Connections for dynamic environments |
| Load balancer single point of failure | Load balancer goes down, entire app down | Use HA mode (active-passive or cloud managed) |
| Not setting timeouts | Slow backend ties up connections | Set connection and request timeouts |
9. Best Practices – Production-Level Tips
✅ Match algorithm to workload – Round Robin for equal servers, Least Connections for variable workloads, Consistent Hashing for stateful apps।
✅ Always use health checks – Without health checks, load balancer blind है。
✅ Use Layer 7 for web apps – More features (path routing, host routing)।
✅ Use Consistent Hashing for dynamic scaling – Minimal remapping when servers change।
✅ Avoid sticky sessions if possible – Use shared session store (Redis) to keep backend servers stateless।
✅ Set appropriate timeouts – client_timeout, connect_timeout, server_timeout to prevent hanging requests।
✅ Enable access logs – Helps debug traffic distribution, security audits।
✅ Monitor load balancer metrics – Request rate, error rate, target response time, healthy host count。
✅ Test failure scenarios – Kill a server, see if LB marks it unhealthy and traffic shifts。
10. Resources – Cheat Sheet & Practice Prompts
Load Balancing Algorithms Cheat Sheet
| Task | NGINX | HAProxy |
|---|---|---|
| Round Robin | (default) | balance roundrobin |
| Weighted RR | weight=5 | weight 5 |
| Least Connections | least_conn; | balance leastconn |
| IP Hash | ip_hash; | balance source |
| Consistent Hashing | hash $request_uri consistent; | Custom Lua |
Practice Prompts
Beginner:
- Simple Node.js app likho (Express)। 2 instances run karo (different ports)। NGINX install karo and configure round robin between them। Test karo ki requests distribute ho rahi hain।
Intermediate:
- Least Connections configure karo in NGINX। Create one slow endpoint (5s delay) and one fast endpoint। Observe how Least Connections sends more traffic to faster server।
Advanced:
- Consistent hashing implement karo with NGINX (
hash $remote_addr consistent;)。Add/remove backend servers and observe how many client mappings change vs IP hash।
11. FAQ
Q1: Round Robin aur Least Connections mein kya antar hai?
Round Robin sequential distribution karta hai irrespective of current load। Least Connections real-time active connections के हिसाब से server select karta hai।
Q2: IP Hash aur Consistent Hashing mein kya antar hai?
IP Hash simple modulo uses – server change par ~90% keys remap। Consistent hashing ring uses – only ~13% keys remap on scaling।
Q3: Sticky sessions kab use karein aur kab avoid?
Use when backend stores session in-memory and you can’t change to shared store। Avoid because it hurts scalability and availability। Prefer external session store (Redis)。
Q4: Weighted Round Robin kab use karein?
When servers have different hardware capabilities – older servers (lower weight) and newer powerful servers (higher weight)।
Q5: Load balancer ki capacity kaise decide karein?
Monitor CPU, memory, network of load balancer। For NGINX/HAProxy, a medium VM (2-4 vCPU) can handle 50k-100k requests/sec। Cloud LB (ALB) scales automatically।
Q6: Kaunsa algorithm best hai?
No single best algorithm – depends on use case। Stateless apps → Round Robin, Variable workloads → Least Connections, Stateful apps → Consistent Hashing。
12. Conclusion – Ab Aapki Baari!
Bahut badhiya! Aapne aaj seekh liya:
✅ Load balancing tutorial Hindi – complete guide with algorithms
✅ Round Robin – simplest, equal distribution
✅ Weighted Round Robin – different server capacities
✅ Least Connections – smart, real-time load awareness
✅ IP Hash – simple sticky sessions
✅ Consistent Hashing – minimal remapping on scaling
✅ Real-world configs – NGINX, HAProxy, AWS ALB
✅ Common mistakes aur best practices
Load balancer modern scalable systems ka backbone hai। Ab aap confident ho ki kaunsa algorithm kab use karna hai।
Aapki challenge: Local environment mein NGINX setup karo with two backend servers। Round Robin test karo, phir Least Connections enable karo and see difference। Apna config comment mein share karo!
Next topic kya chahiye?
- Consistent Hashing Deep Dive?
- API Gateway vs Load Balancer?
- Service Mesh (Istio) Load Balancing?
Comment mein batao!
The Easy Master ke saath load balancing seekhte raho। Happy distributing! 🚀🚦
Resources
- NGINX Load Balancing Documentation
- HAProxy Algorithms Documentation
- AWS ALB User Guide
- Traefik Load Balancing Guide – 2026
- Consistent Hashing – System Design Primer
Additional Resources
- FastAPI Kya Hai? FastAPI Python Setup Aur Pehla API Hindi 2026
- FastAPI Path Parameters Hindi – शून्य से हीरो तक गाइड 2026
- Pydantic v2 Tutorial Hindi – Data Validation Master 2026
- FastAPI dependency injection Hindi – Code Reuse Ka Magic
- FastAPI Async Await Hindi – Non-Blocking Code 2026
- FastAPI PostgreSQL SQLModel Hindi – Async Guide 2026
- FastAPI JWT Authentication Hindi – Secure API Login
- FastAPI OpenAI Integration Hindi – AI Chatbot API 2026
- FastAPI Multi-Agent AI Hindi – LangGraph Zero to Hero
- FastAPI Deployment Hindi – Railway Zero to Hero 2026
- Docker Introduction in Hindi? Containers vs Virtual Machines
- Docker Images and Containers Hindi – Pehla Container
- Docker Compose Tutorial Hindi – Node.js + MongoDB
- Docker Volumes Networking Hindi – Data Persist कैसे करें
- Kubernetes Architecture Hindi – Pods, Nodes, Cluster
- Minikube Tutorial Hindi – Local Cluster कैसे बनाए
- Kubernetes Deployments Services Hindi – App Expose Karein
- K8s Ingress Tutorial Hindi – Domain se App Access
- Helm Kya Hai? – Kubernetes Charts Se App Deploy
- GraphQL Introduction in Hindi – REST vs GraphQL Comparison
- GraphQL Schema Tutorial Hindi – Types Queries Resolvers
- GraphQL Queries Mutations Hindi – Frontend Integration
- GraphQL Advanced Features Hindi – Fragments Aliases Variables
- Apollo Server GraphQL Node.js TypeScript Hindi – API Kaise Banaye
- Apollo Client React Hindi – GraphQL Queries Use Kaise Karein
- GraphQL Testing Supertest Hindi – Queries Mutations Test
- Integration Testing Node.js – Mock DB aur APIs Hindi
- E2E Testing Playwright – GraphQL Frontend Testing & CI/CD Hindi
- System Design Kya Hai? System Design Introduction in Hindi
- Vertical Horizontal Scaling Hindi – कब क्या Use करें
- Load Balancing Tutorial Hindi: Round Robin and Hashing
- Microservices vs Monolith Hindi – Modular Monolith se Safar
- Message Queues (RabbitMQ, Kafka) – EDA Samjhe Hindi
- Consistent Hashing Hindi – Distributed Caching & Sharding
- System Design Case Study Hindi – TinyURL WhatsApp Instagram
- Shift Left security in Hindi– Security Ko Pehle se कैसे करें
- OWASP Top 10 Hindi – Web Security Risks & Fixes 2026
- Supply Chain Security Hindi – npm PyPI Malicious Packages