π¦ What is Load Balancing?
Think of a Load Balancer as a digital traffic cop sitting in front of your servers. When thousands of users visit your website at the same time, the load balancer distributes incoming web traffic across multiple backend servers so that no single server gets overwhelmed, crashes, or slows down.
Core Benefits of Load Balancing
Zero Downtime (High Availability): If Server A crashes, the load balancer automatically redirects traffic to Server B without the user noticing.
Scalability: You can easily add 5 more servers during peak sales hours and remove them when traffic drops.
Faster Response Times: Traffic is routed to the healthiest, least busy, or geographically closest server.
π The 5 Core Routing Algorithms Compared
| Algorithm | How it Works (Analogy) | Best Used For |
| 1. Round Robin | Takes turns in order: Server 1 $\rightarrow$ Server 2 $\rightarrow$ Server 3 $\rightarrow$ Server 1. | Servers with identical hardware handling short, equal requests. |
| 2. Least Connections | Sends traffic to the server with the fewest active connections. | Long-lived connections (e.g., chat apps, streaming, gaming). |
| 3. IP Hash | Uses the user's IP address to map them to the exact same server every time. | Applications requiring persistent user sessions (e.g., shopping carts). |
| 4. Least Response Time | Sends traffic to the server with the fastest response time and fewest connections. | Systems where server performance fluctuates or is latency-sensitive. |
| 5. Weighted (Bonus) | Assigns a higher capacity ratio (e.g., 3:1) to stronger servers. | Mixed infrastructure where some servers have more RAM/CPU than others. |
π ️ Deep Dive: When, Where & Why to Use Each
1. Round Robin
How it works: Imagine a dealer passing out cards around a table one by one in a circle. It cycles through servers sequentially regardless of how busy they are.
Benefits: Extremely simple to configure, virtually zero CPU overhead on the load balancer.
When to use: When all backend servers have identical CPU/RAM specs and the incoming requests take roughly the same amount of time to process (e.g., serving static HTML/CSS files).
When NOT to use: If some requests take 10 seconds (like rendering a PDF) and others take 10 milliseconds.
2. Least Connections
How it works: The load balancer keeps track of how many active open connections each server currently holds, and sends the next user to whoever has the shortest line.
Benefits: Prevents server overload when request processing times vary wildly.
When to use: Applications with long-lived connections or variable task lengths—such as live chat apps (WebSockets), file uploads, video streaming, or heavy database query rendering.
3. IP Hash
How it works: The load balancer takes the user’s IP address (e.g.,
192.168.1.50), runs it through a hash function, and converts it into a number that permanently points to Server A.Benefits: Guarantees "session stickiness" (the user always lands on the same server) without relying on browser cookies.
When to use: Legacy applications where user session state is stored in server memory (like an active shopping cart or login session) instead of a shared database like Redis.
4. Least Response Time (or Least Latency)
How it works: The load balancer constantly monitors the health and TTFB (Time To First Byte) latency of each server. It combines the fewest active connections with the fastest ping time.
Benefits: Maximizes end-user speed and automatically routes around servers that are lagging or undergoing background tasks (like memory garbage collection).
When to use: Ultra low-latency systems like high-frequency trading platforms, real-time gaming backends, or globally distributed microservices where network delays vary.
π― Summary Quick-Decision Guide
Do your servers have equal specs?
│
┌─────────────────┴─────────────────┐
YES NO
│ │
Are requests equal in length? Use WEIGHTED
┌───────┴───────┐ ROUND ROBIN
YES NO
│ │
ROUND ROBIN LEAST CONNECTIONS
│
Need persistent session per user?
┌───────┴───────┐
YES NO
│ │
IP HASH LEAST RESPONSE TIME