How does a key find its node on a consistent hash ring?
Sketch your own answer first, then compare it with the one below.
A strong answer
Both nodes and keys are hashed with the same function onto a circle of hash values. To find a key's node, start at the key's position and walk clockwise until you reach the first node point. That node owns the key.
In code the points are kept in a sorted array. Lookup is a binary search for the first point at or after the key's hash, wrapping to the first element if there is none.
Because the rule depends only on positions on the ring, any client holding the same list of nodes computes the same owner, with no central lookup.
When a node joins, it takes over only the arc just before each of its points. Keys in those arcs move to it and no other keys move. When a node leaves, its keys go to the next node clockwise.
This is the idea in the original consistent hashing paper, and the Chord lookup protocol reused it.
Sources
- David Karger, Eric Lehman, Tom Leighton, Rina Panigrahy, Matthew Levine, Daniel Lewin (1997). Consistent Hashing and Random Trees: Distributed Caching Protocols for Relieving Hot Spots on the World Wide Web.
- Ion Stoica, Robert Morris, David Karger, M. Frans Kaashoek, Hari Balakrishnan (2001). Chord: A Scalable Peer-to-peer Lookup Service for Internet Applications.