Short answer: Consistent hashing maps keys and nodes onto a circle by a hash. A key is stored on the first node at or clockwise from the key’s point. Adding or removing a node moves only the keys that walk past that node, about 1/n of the keys when n nodes are balanced, rather than almost every key as in a hash modulo n. Virtual nodes, many points per real node, keep the load even when a single point per node would clump.
What the problem actually asks
A table that addresses a node by hash(key) mod n changes almost every key’s home when n changes by one. A cache fleet that grows or loses a machine then repopulates from scratch. The ring fixes the neighbors of a key: only a key whose successor node disappeared, or whose new node landed in front of it, moves.
One point per node is a random spacing problem. The largest gap can be several times the average, so one node holds several times its share. Virtual nodes draw many independent points for each real node and average those gaps out. The number of virtual nodes is the load-balance parameter.
What the algorithm guarantees
With one point per node, a newly added node takes the keys in the arc from its predecessor to itself, and no other keys move. The expected fraction is 1/(n+1) for a random point. Virtual nodes reduce the relative load variance. Lookups are a successor search in a sorted ring, O(log n) with a balanced tree, or a hash to a bucket of virtual nodes. Extra memory is the ring of virtual points.
The hash has to be unrelated to the key structure and to the node ids, or an adversary places every key on one arc. Replication places a key on the next r successors. That is a separate availability choice and does not change the ring rule.
The working steps
- Hash the key space onto a circle. For each physical node, insert v virtual points at hashes of the node id salted by 1..v.
- Store the points in a sorted map.
- A key lives on the first point clockwise from hash(key). The physical node that owns that point stores the key.
- To add a node, insert its virtual points. Each new point takes the keys from the point that was its successor, back to itself.
- To remove a node, delete its points. Those keys move to the next surviving point clockwise.
The 1/n movement bound is about membership changes of the node set. It is not a bound on hot keys, which can still collide on one arc if the hash is weak or v is tiny. A modulo hash moves more keys when n changes and is simpler when n is fixed. Rendezvous hashing is an alternative that needs no ring and scores each key against each node.
Approaches that look simpler and fail
| Approach | Why it fails when each node places only one random point and expects equal load |
|---|---|
| Use one point per node and assume the arcs are equal | Random points leave large gaps. One node can own several times the average arc. Virtual nodes are the fix. |
| Rehash with modulo n and call it consistent | Changing n moves almost every key. Consistency means the opposite: most keys keep their node. |
| Forget to move the arc when a point is inserted | The new node is in the ring and the keys that now precede it still sit on the old successor. The handoff is the point of the insertion. |
| Hash node ids with a function that clusters | The ring balance is only as good as the hash. A structured id hashed poorly recreates the large gap. |
A fixed node count can use hash modulo n. Rendezvous hashing picks the node with the largest hash(key, node) and avoids a sorted ring. Consistent hashing is the clockwise-successor ring, balanced with virtual nodes.
A working example
This Python fragment places each node at several virtual points on a ring and stores a key on the clockwise successor. Key hash 10 has successor 12, owned by n1. Inserting a point at 11 takes that key. Key hash 15 stays on n2. It prints the owner of hash 10 before and after, n1 n3.
import bisect
ring = [] # (point, node)
def add_node(node, points):
for point in points:
ring.append((point, node))
ring.sort()
def owner(key_hash):
i = bisect.bisect_left(ring, (key_hash, ""))
if i == len(ring):
i = 0
return ring[i][1]
add_node("n1", [12, 30])
add_node("n2", [20])
before = owner(10)
add_node("n3", [11])
assert before == "n1" and owner(10) == "n3" and owner(15) == "n2"
print(before, owner(10))
Where it is used
Cassandra, Dynamo, and ketama-style memcached rings place a key by consistent hashing so one node’s arrival moves only its share of the keys. An ordinary hash modulo the server count moves almost every key. Rendezvous hashing picks the server by a score instead of a ring.
When you can drop this algorithm
Drop a single point per node when load balance matters. Use many virtual nodes, or use rendezvous hashing. Keep the ring when membership changes should move about 1/n of the keys.
Why this stays published
We publish this so a search for consistent hashing returns the clockwise successor and the virtual nodes that even out the arcs. Consistent hashing stores a key on the clockwise successor node of its hash, so a membership change moves only the keys on the affected arcs.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
