each key → the next server clockwise
Consistent hashing vs Modulo hashing
server = hash % 3
Twelve keys in a cache, over three servers: add a server, lose one, and see how many keys move. Every key that moves is a cache miss.
Consistent hashing or Modulo hashing?
Modulo is fine while the number of servers never changes. When servers come and go, use consistent hashing — and give each server many points on the ring (virtual nodes) so the load stays even.
| Difference | Consistent hashing | Modulo hashing |
|---|---|---|
| A key’s server | The next server clockwise on a ring | hash % N: the remainder picks it |
| Add a server | Only the keys before it move: about 1 in N | Almost every key moves |
| A server dies | Only its keys move, to the next server | Almost every key moves again |
| The cache after a change | A few misses | Mostly misses: the database takes the load |
| Even spread | Uneven with few servers; virtual nodes even it out | Even, as long as N never changes |
| Used in | DynamoDB, Cassandra, Memcached clients, load balancers | A fixed set of shards that never changes |