Consistent Hashing for Load Balancing
What You'll Learn
You'll discover how consistent hashing minimizes disruption when servers are added or removed from your load balancer pool.
The Problem with Simple Hashing
Recall the IP Hash Algorithm you learned earlier—it maps clients to servers using a hash function (like hash(client_IP) % number_of_servers). But there's a problem: when you add or remove a server, the modulo changes, causing most client-server mappings to break. If server count changes from 5 to 6, nearly all clients get routed to different servers, losing cached data and session state.
How Consistent Hashing Works
Consistent hashing arranges servers on a virtual hash ring (imagine a circle with values 0 to 2³²-1). Both servers and client requests are hashed to positions on this ring using the same hash function.
The mapping rule: A request travels clockwise around the ring until it finds the first server.
The Magic: Minimal Disruption
When you add a new server, it only "captures" requests that would have gone to the next server clockwise—not all requests. When you remove a server, only its requests get redistributed to the next server.
Example: With servers at positions 100, 500, and 900 on the ring, a request hashing to 350 goes to server-500. Add a new server at position 400? Only requests between 350-400 move—everything else stays put.
This means ~1/N of requests are remapped when the server count changes (where N = total servers), instead of nearly 100% with simple hashing.
Key Takeaway: Consistent hashing maps servers and requests to a hash ring, ensuring only a small fraction of mappings change when servers are added or removed, preserving cache locality and session stability.