Consistent hashing
A way to spread keys across servers so that adding or removing a server moves only a small share of keys instead of nearly all of them.
The naive way to pick a server for a key is hash(key) mod N. It works until N changes: add one cache server and almost every key maps somewhere new, so the caches go cold at once and the database takes the full load, a thundering herd. Consistent hashing places servers and keys on the same ring of hash values and assigns each key to the next server clockwise. Adding a server takes over only the keys between it and its neighbor.
It comes from a 1997 paper by David Karger and colleagues on web caching. Amazon’s Dynamo paper made it standard for distributed databases, and it is used today in Cassandra, cache client libraries, load balancers and CDNs.
With few servers the ring is uneven, so each server is placed at many points (virtual nodes) to spread the load. Rendezvous hashing is a simpler alternative with the same goal.
Sources
- ACM STOC ’97: Karger et al., Consistent Hashing and Random Trees doi.org
- Amazon (SOSP 2007): Dynamo: Amazon’s Highly Available Key-value Store allthingsdistributed.com
Related terms
- Hash table: A structure that finds a value by its key in about constant time by hashing the key into a slot; fast lookups, no order.
- Vector clock: A list of counters, one per node, attached to each change so a system can tell whether one change happened before another or both happened concurrently.
- Thundering herd: Many clients or processes hitting one resource at the same moment, for example right after an outage ends or a popular cache entry expires.
- Multi-tenancy (multi-tenant SaaS): One application and one deploy serving many client companies, with each client’s data kept apart.
- Edge computing (Workers): Running code in data centers close to the user, on the CDN, instead of in one central region.
