System Design Scalabilityπ± Beginner
A Load Balancer is a critical piece of infrastructure that acts as a traffic cop, distributing incoming user requests evenly across a cluster of backend servers.
What is a Load Balancer?
When you implement Horizontal Scaling (running 5 identical copies of your Node.js server), you cannot give users 5 different IP addresses. Instead, the user connects to a single Load Balancer. The Load Balancer instantly decides which of the 5 servers has the least amount of work, and forwards the request to it.
Why is it Mandatory?
Without a load balancer, all users might accidentally connect to Server 1, causing it to crash from overload, while Servers 2, 3, 4, and 5 sit completely idle. Furthermore, if Server 1 physically catches fire, the Load Balancer instantly detects the failure (via Health Checks) and stops sending traffic to it.
How Does it Route Traffic?
Load Balancers use various algorithms to decide where to send the next HTTP request:
- Round Robin: The simplest method. It sends Request 1 to Server A, Request 2 to Server B, Request 3 to Server C, then starts over.
- Least Connections: It actively monitors the servers and sends the request to whichever server currently has the fewest active users connected.
- IP Hashing: It mathematically hashes the user's IP address to guarantee that User X is always sent to Server A (crucial for maintaining session state).