Skip to content
Security

4. Load Balancing Tutorial Hindi – Round Robin Least Connections Hashing

June 27, 2026 11 min read

नमस्ते दोस्तों!

स्वागत है 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! 🚦🚀

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?

BenefitExplanation
High availabilityAgar ek server fail ho jaaye, load balancer traffic को healthy servers pe bhejta hai
ScalabilityTraffic बढ़ने पर aap easily servers add kar sakte ho
PerformanceRequests evenly distribute hone se koi server overload nahi hota
SSL terminationLoad balancer HTTPS decrypt kar sakta hai, backend servers par load kam
Health checksRegularly 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 करें?

AlgorithmBasisBest forStickyComplexity
Round RobinSequentialEqual servers, statelessNoLow
Weighted RRWeight factorUnequal capacitiesNoLow
Least ConnectionsActive connectionsVariable request length, WebSocketNoMedium
IP HashClient IPSimple sticky sessionsYesLow
Consistent HashingHash ringDistributed caches, dynamic scalingYesHigh

Decision framework:

text

Code
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 Hashing

7. Real-World Configurations – NGINX, HAProxy, AWS ALB

7.1 NGINX Configuration

Round Robin (default):

Nginx
upstream backend {
    server 10.0.0.1:80;
    server 10.0.0.2:80;
    server 10.0.0.3:80;
}

Weighted Round Robin:

Nginx
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:

Nginx
upstream backend {
    least_conn;
    server 10.0.0.1:80;
    server 10.0.0.2:80;
}

IP Hash:

Nginx
upstream backend {
    ip_hash;
    server 10.0.0.1:80;
    server 10.0.0.2:80;
}

7.2 HAProxy Configuration

haproxy

Code
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 2

7.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!)

MistakeWhy it’s wrongSolution
Using Round Robin for uneven workloadsSome servers overloadedUse Least Connections
Using IP Hash without considering NATAll office users go to same serverUse Consistent Hashing or cookie-based sticky
Not configuring health checksUnhealthy servers still receive trafficConfigure health checks with proper thresholds
Ignoring session persistence needsUsers randomly logged outUse IP Hash, Consistent Hashing, or sticky cookies
Static weights in auto-scalingWeights don’t adjust to real capacityUse Least Connections for dynamic environments
Load balancer single point of failureLoad balancer goes down, entire app downUse HA mode (active-passive or cloud managed)
Not setting timeoutsSlow backend ties up connectionsSet 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

TaskNGINXHAProxy
Round Robin(default)balance roundrobin
Weighted RRweight=5weight 5
Least Connectionsleast_conn;balance leastconn
IP Haship_hash;balance source
Consistent Hashinghash $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

Additional Resources

TheEasyMaster

Author at The Easy Master.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *