Sub topic 12
Rendezvous hashing and jump consistent hash
This chapter assumes you have read the earlier chapters on the hash ring and virtual nodes, and that you can read short Python functions. Both algorithms below solve the same problem as the ring: when the node set changes, only a small share of keys should move. They do it without a ring or a sorted list.
Rendezvous hashing
Rendezvous hashing, also called highest random weight (HRW) hashing, works like this. For a given key, compute a score for every node by hashing the node name together with the key. The key goes to the node with the highest score.
Removing a node only affects keys for which that node had the top score. Every other key still has the same winner. Adding a node takes only the keys that the new node scores highest on, about 1/N of them.
import hashlib
def score(node, key):
return int(hashlib.md5(f"{node}:{key}".encode()).hexdigest(), 16)
def rendezvous_node(nodes, key):
return max(nodes, key=lambda node: score(node, key))
def rendezvous_replicas(nodes, key, count):
ranked = sorted(nodes, key=lambda node: score(node, key), reverse=True)
return ranked[:count]
nodes = ["a", "b", "c", "d"]
keys = [f"key{i}" for i in range(10000)]
before = {k: rendezvous_node(nodes, k) for k in keys}
after = {k: rendezvous_node(nodes + ["e"], k) for k in keys}
moved = [k for k in keys if before[k] != after[k]]
print(len(moved) / len(keys))
print({after[k] for k in moved})The output is a moved fraction near 0.2, and the set of destinations is {'e'}.
Compared with the ring:
- Load is even without virtual nodes, because each key's winner is effectively random across nodes.
- Replication is trivial. Take the top
countnodes, asrendezvous_replicasdoes. - There is no state to keep in sync beyond the node list.
- A lookup costs O(N) hash computations, where the ring costs O(log N). This is fine for tens of nodes and slow for thousands.
Jump consistent hash
Jump consistent hash, from Lamping and Veach at Google, maps a 64-bit integer key to a bucket number in 0..N-1. It uses no memory and no node list. It simulates a key "jumping" forward to higher bucket numbers as the bucket count grows, using a pseudo-random generator seeded by the key.
import hashlib
def jump_hash(key, num_buckets):
b, j = -1, 0
while j < num_buckets:
b = j
key = (key * 2862933555777941757 + 1) & 0xFFFFFFFFFFFFFFFF
j = int((b + 1) * (1 << 31) / ((key >> 33) + 1))
return b
def to_int(key):
return int.from_bytes(hashlib.md5(key.encode()).digest()[:8], "big")
keys = [f"key{i}" for i in range(10000)]
before = {k: jump_hash(to_int(k), 4) for k in keys}
after = {k: jump_hash(to_int(k), 5) for k in keys}
moved = [k for k in keys if before[k] != after[k]]
print(len(moved) / len(keys))
print({after[k] for k in moved})The moved fraction is again about 0.2, and every moved key lands in bucket 4, the new one. The loop runs about ln(N) times, so lookups are fast.
The limitation is that buckets are plain numbers, and you can only add or remove the last one. If node 2 of 5 dies, you cannot drop bucket 2 without renumbering. Jump hash therefore suits sharded storage where you grow and shrink the shard count in order. It does not suit caches where arbitrary servers fail.
Choosing between them
| Property | Hash ring with virtual nodes | Rendezvous | Jump hash |
|---|---|---|---|
| Lookup cost | O(log N) | O(N) | about O(log N) |
| Memory | sorted list of N × vnodes | node list | none |
| Remove an arbitrary node | yes | yes | no |
| Balance | depends on vnode count | even | even |
| Replicas | walk the ring | take top scores | needs extra work |
<br>
Exercise
A cache fleet has 200 servers, and any one of them can fail at any time. Would you pick rendezvous hashing or jump consistent hash, and what does it cost you?
Show answer
Rendezvous hashing. Jump hash can only remove the highest-numbered bucket, so it cannot handle an arbitrary server failing. The cost of rendezvous hashing is O(N) work per lookup, here 200 hash computations. That is acceptable at this size, or you can use a ring with virtual nodes instead.