The short answer
Consistent hashing is a way to assign keys to servers so that adding or removing a server moves only a small share of keys. Instead of computing a server number from a formula that depends on how many servers exist, it places both servers and keys on a circle of hash values. Each key belongs to the first server found going clockwise from the key’s position.
That small change matters when a system grows or shrinks. Caches stay mostly warm, and fewer records need to be copied across the network. For a shorter definition, see what does consistent hashing mean.
Why simple modulo hashing breaks when servers change
A common first approach is to hash the key and take the remainder after dividing by the number of servers: server = hash(key) mod N. This is simple, and it spreads keys fairly evenly as long as N stays the same.
The trouble starts when N changes. Take a small example with keys 1 to 12. With 3 servers, key 3 goes to server 0, key 4 goes to server 1, and so on. When a fourth server is added, the remainder changes for most keys. Keys 1, 2 and 12 happen to keep the same remainder, but the other nine move to a different server. Key 3, for instance, moves from server 0 to server 3.
In a real system with millions of keys, that means most keys move. Most cached values are lost, and much of the data has to be copied between servers.
Placing servers and keys on a ring
Picture a circle where positions run from 0 up to a very large number and then wrap back to 0. Real systems use a much bigger range, but a small one makes the idea easier to follow.
Each server gets a position on the ring by hashing its name or address. Each key is hashed to a position too. The rule is simple:
A key belongs to the first server at or after its position, going clockwise. If no server lies ahead, the key wraps around to the first server on the ring.
Consider a worked example with positions 0 to 99. Servers A, B and C sit at 10, 40 and 70. Five keys hash to positions 5, 25, 45, 55 and 90.
| Key position | First server clockwise |
|---|---|
| 5 | A (10) |
| 25 | B (40) |
| 45 | C (70) |
| 55 | C (70) |
| 90 | wraps around to A (10) |
These positions are illustrative, not real hash values.
What happens when a server is added
Now add server D at position 50. D takes over the keys between its counterclockwise neighbor, B at 40, and itself. Key 45 was on C. It now sits between B and D, so it moves to D. Key 55 lies past D’s position, so it stays on C. The other keys do not move.
In general, a new server takes only the keys on the arc between itself and the server before it. Everything else stays where it was.
What happens when a server is removed
Remove server B. Its keys move to the next server clockwise. Key 25 was on B, so it now goes to D at 50. Keys 5, 45, 55 and 90 keep their owners.
With N servers and one of them changing, about one divided by N of the keys move, on average. That is an approximation, but it is far less than the most-keys result of modulo hashing.
Virtual nodes
With only one position per server, the arcs can be very uneven. One server may get a long arc and carry far more keys than the rest.
The usual fix is to give each server many positions on the ring, called virtual nodes. A physical server might be hashed as “server-A#1”, “server-A#2” and so on, with those points spread around the circle. The arcs then even out. A larger machine can be given more positions to take a bigger share. When a server leaves, its keys spread across several neighbors instead of landing on one.
The trade-off is that the ring has more entries to store and search, so systems pick a count that balances evenness against that overhead.
Amazon’s Dynamo paper describes a similar virtual node approach, and Apache Cassandra gives each node many tokens on the ring.
How a key finds its server
The ring is usually kept as a sorted list of positions, each linked to its server. To find the owner of a key:
key_position = hash(key)
index = first entry in sorted ring with position >= key_position
if no entry is found, index = 0 (wrap around)
return owner of the entry at index
Because the list is sorted, the search can use binary search. The lookup time then grows with the logarithm of the number of ring entries, not with the number of keys. For a refresher on what that notation means, see big o data structures.
Copies on the ring
Many systems store more than one copy of each key for safety. A common approach is to store the key on the server that owns it and on the next few distinct servers clockwise. Dynamo-style systems use this idea, which the paper calls a preference list. If the owner fails, a copy still exists on another server.
Where consistent hashing is used
- Distributed caches. David Karger and colleagues introduced the idea in 1997 to spread web caching load and reduce hot spots.
- Distributed key-value stores and databases, such as Dynamo-style systems and Apache Cassandra.
- Some load balancers and content delivery systems, which use a similar ring or lookup approach to route requests.
Limits and newer variants
Consistent hashing is useful, but it has limits:
- Uneven load with few positions. Without enough virtual nodes, some servers carry more keys than others.
- Hot keys. If one key is very popular, its server still receives all of that key’s traffic. Hashing spreads keys, not the requests for a single key.
- Bounded loads. Mirrokni, Thorup and Zadimoghaddam, from Google, proposed a version that caps how loaded any server can get and passes overflow to the next server. The paper was first posted in 2016 and published at SODA 2018.
- Rendezvous hashing. Also called highest random weight, it skips the ring. Each server gets a score for each key, and the highest score wins. It needs no ring structure, but the basic form checks every server on every lookup.
- Jump consistent hash. Lamping and Veach, working at Google, described a method that uses very little memory. It maps keys to bucket numbers from 0 to N minus 1, so it fits best when servers are numbered and change at the end of the range. It cannot take arbitrary server names.
Which method fits depends on the workload, the number of servers and how often the group changes.
Key takeaways
- Consistent hashing places servers and keys on a ring, and each key goes to the next server clockwise.
- Adding or removing a server moves only the keys on its arc, roughly one over N of the keys on average.
- Virtual nodes even out the load across servers.
- Lookups use a sorted list and binary search.
- Variants such as bounded loads, rendezvous hashing and jump consistent hash address other trade-offs.