Load Balancing Algorithms
You'll learn: How load balancers decide which server should handle each incoming request using different routing strategies.
The Distribution Problem
Once you've decided to use load balancing (spreading traffic across multiple servers), you need an algorithm to determine which server gets each request. There's no one-size-fits-all answer—different algorithms optimize for different goals.
Think of it like a restaurant host seating customers across multiple waiters. Should you rotate evenly? Send people to whoever's least busy? Consider the waiter's experience level?
Common Load Balancing Algorithms
Round-Robin
The simplest approach: distribute requests sequentially across servers in rotation. Server 1 gets request 1, Server 2 gets request 2, Server 3 gets request 3, then back to Server 1. Fair and predictable, but ignores actual server load or capacity.
Least Connections
Routes new requests to the server currently handling the fewest active connections. Smart for workloads where some requests take much longer than others—prevents one server from getting overwhelmed while others sit idle.
Weighted Distribution
Assigns servers different "weights" based on their capacity. A powerful server with weight 3 might receive three times the traffic of a weight-1 server. Useful when your backend servers have different hardware specs.
IP Hash
Uses the client's IP address as input to a hash function that deterministically maps them to a specific server. The same client always reaches the same server, which helps with session persistence without requiring session sharing—though it may distribute unevenly if traffic comes from a few IP ranges.
Key Takeaway: Load balancing algorithms range from simple (round-robin) to context-aware (least connections, weighted, IP hash), each suited to different application needs and traffic patterns.