Skip to main content

Load Balancing and Reverse Proxies

Load Balancing Algorithms

0:00
LearnStep 1/3

Deep Dive into Load Balancing Algorithms

For senior engineers, selecting the right load balancing algorithm is less about knowing the definitions and more about understanding the trade-offs between latency, resource utilization, and failover behavior. This module covers standard and advanced algorithms used in production Layer 4 and Layer 7 load balancers.

1. Round-Robin & Weighted Round-Robin

Round-Robin is the simplest static algorithm, iterating through the list of servers sequentially. It assumes all servers have identical capacity and that requests have uniform processing costs.

Weighted Round-Robin addresses heterogeneous environments by assigning a weight to each server.

bash

2. Least Connections

This dynamic algorithm directs traffic to the server with the fewest active connections. It is ideal for requests with varying processing times (e.g., long-lived WebSocket connections or video encoding jobs).

3. IP Hash (Session Affinity)

IP Hashing uses the client's IP address as a hashing key to determine the upstream server. This ensures that requests from the same client always reach the same server, provided the server list remains constant. This is critical for sticky sessions where local state is maintained.

bash

4. Consistent Hashing

In distributed caching (e.g., Memcached, Redis Cluster), standard modulo hashing (hash(key) % N) is disastrous when scaling. Adding or removing a node changes N, invalidating nearly all cache keys.

Consistent Hashing maps both servers and keys to a unit circle (hash ring). A key is assigned to the next server found moving clockwise on the ring. When a server is added, it only takes a share of keys from its neighbor, minimizing data movement to K/N (where K is total keys, N is nodes).

5. Random with Two Choices

Pure random selection can lead to hot spots. The Power of Two Choices algorithm picks two servers at random and chooses the one with the lower load. This simple variation drastically improves load distribution compared to pure random, approaching the efficiency of Least Connections with significantly less state overhead.