1.What consistent hashing is for, and the ring concept
Consistent hashing is a way to map both data (keys) and servers (nodes) onto the same circular hash space, called a ring, so that each key belongs to whichever node comes next on the ring in clockwise order. It solves a specific problem with plain "hash(key) % N" routing: with modulo hashing, adding or removing a single server changes the result of the modulo for almost every key, so almost every key gets remapped to a different server at once. On a ring, only the keys that fall between the changed node and its neighbor move — everyone else keeps pointing at the same node they always did. That is why it shows up anywhere a distributed cache, a partitioned datastore, or a load balancer needs to route the same key to the same node consistently, even as nodes come and go.
Key features
- Both nodes (servers) and keys are hashed into the same numeric space and placed as points on a circle (the "ring").
- A key belongs to the first node found walking clockwise from the key's position on the ring.
- Used for three related problems: distributed caching (which cache node holds a key), data partitioning (which shard owns a row), and request routing/load balancing (which backend server a request goes to).
- The alternative, "hash(key) % N", ties every key's owner to the exact current node count N — changing N remaps nearly all keys at once, which is the specific failure consistent hashing avoids.
- Amazon's Dynamo, Apache Cassandra and Akamai's CDN are real systems documented as using consistent hashing for this reason.
Modulo hashing vs. ring-based consistent hashing
- hash(key) % N
- Routing depends on the current node count N
- Adding/removing a node remaps nearly all keys
- Every lookup needs to know N upfront
- Consistent hashing (ring)
- Routing depends only on ring position
- Only keys near the changed node remap
- Lookup only needs the ring, not N
Command
ring = sorted positions of hash(node_id) for every node
key's owner = first node position >= hash(key), walking
clockwise (wrap around to the smallest
position if none is found before 360°/2^32)Ring-based lookup (hash the key, walk clockwise to the next node) means only the keys near a changed node move when that node joins or leaves — unlike hash(key) % N, where changing N remaps nearly everything.