Rendezvous and jump hashing
Learn how rendezvous (highest random weight) hashing and jump consistent hash choose a node without a token ring, measure them against modulo and the ring on the same keys, and match each scheme to the job it fits.
Contentsand lesson path
On this page
- Why it matters
- Rendezvous hashing: every node bids for every key
- Jump consistent hash: no table, numbered buckets
- Measuring all four on the same keys
- Choosing a scheme
- Check your understanding
- Apply it
- Review prompt
Lessons of Consistent Hashing
- Why hash mod N breaks when the cluster changes Concept · 15 min
- The ring, virtual nodes and replica placement Concept · 20 min
- Implementing and measuring a hash ring Lab · 30 min
- Rendezvous and jump hashing Concept · 15 min
- Rings in practice: cache clients and Dynamo-style stores Worked example · 20 min
- Consistent hashing check Knowledge check · 6 questions
What you will learn
- Compare modulo, ring, rendezvous and jump hashing by balance, key movement and lookup cost.
01. Why it matters
The ring is not the only way to keep most keys in place. Two other schemes need no token table at all: rendezvous hashing, published by Thaler and Ravishankar in 1998, and jump consistent hash, published by Lamping and Veach at Google in 2014. They trade lookup work, memory and flexibility in different ways. Pick the wrong one and you pay with slow lookups, large tables on every client, or a cluster that cannot lose an arbitrary server cleanly.
This lesson explains both, then measures them against modulo and the ring with the same keys and the same changes as in the lab.
02. Rendezvous hashing: every node bids for every key
Rendezvous hashing is also called highest random weight (HRW) hashing. To place a key, compute a score for every node by hashing the node's name together with the key, for example h64("node-3|user:42"). The node with the highest score wins. Every client computes the same scores from the same names, so all clients agree on the owner without talking to each other; that agreement is the "rendezvous" in the name.
The scheme keeps both promises from the first lesson without virtual nodes. Each node has the same chance of the top score for any key, so each wins about 1/N of the keys. When a node leaves, only the keys it was winning change owner, and each of them goes to its own second-highest scorer. Those runners-up are spread at random over the remaining nodes, so the departing node's keys spread evenly instead of landing on one neighbour. When a node joins, it takes only the keys for which it now scores highest, about 1/(N + 1) of them.
Sorting the scores gives an ordered list of nodes for each key, which the original paper uses for failover: if the first choice is down, try the second. The same list places replicas: the top R scorers are R distinct nodes, with no skipping needed.
The price is lookup work. Every lookup computes N hashes, one per node, so it costs O(N). For a client choosing among 10 or 50 cache servers that is usually cheap; for a router choosing among thousands of backends on every request it may not be. Tree-shaped variants reduce the work to about log N, at some cost in balance when nodes change. The Python memcached client pymemcache uses rendezvous hashing by default in its HashClient.
03. Jump consistent hash: no table, numbered buckets
Jump hash takes a 64-bit key and a number of buckets n, and returns a bucket number from 0 to n − 1. Picture n growing from 1 upwards. With one bucket, every key is in bucket 0. When a second bucket is added, half the keys must jump to bucket 1 to stay balanced. When a third is added, a third of all keys must jump to bucket 2, and so on: when bucket n is added, each key jumps to it with probability 1/(n + 1), and otherwise stays where it was. A pseudo-random generator seeded with the key makes those decisions, so the same key always makes the same jumps.
Walking through every bucket would be slow, so the algorithm calculates where the key's next jump lands and skips straight to it. The paper shows that the expected number of loop rounds is less than ln n + 1. The whole function is about five lines of C++ and uses no memory beyond a few variables, and in the paper's measurements it split the key space between buckets almost perfectly.
Its limits come from using numbers instead of names. Buckets must be numbered 0 to n − 1 with no gaps, so you can only add or remove the last bucket cheaply. The authors say plainly that this makes it more suitable for data storage than for web caching. In a store, each bucket is a shard kept on several replicas; when a server dies, a replica takes over the shard and the bucket count does not change. Only capacity changes alter n. In a cache tier, any server can disappear, and that does not fit. Keys that are not 64-bit integers, such as strings, must first be hashed to a 64-bit number.
04. Measuring all four on the same keys
compare.py reuses ring.py from the lab, so save it in the same ring-lab folder. It translates the paper's C++ code line by line; % 2**64 reproduces C++'s unsigned 64-bit overflow. For each scheme it places the 100,000 keys on 10 nodes and reports the busiest node against the mean, the keys moved when node-10 joins, and the keys moved when node-3 leaves. Jump hash cannot drop bucket 3 directly, so the script moves node-9 into bucket 3 and then removes the last bucket, the usual workaround. The last two lines count jump hash's loop rounds and show where node-3's keys go under rendezvous hashing. It took about 15 seconds on the machine used to test it, most of that in the rendezvous runs.
"""Mod N, ring, rendezvous and jump hash on the same keys and the same changes."""
from collections import Counter
from ring import HashRing, ModN, h64
KEYS = [f"user:{i}" for i in range(100_000)]
NODES = [f"node-{i}" for i in range(10)]
loops = Counter() # jump hash loop iterations, to measure its lookup work
def jump(key: int, buckets: int) -> int:
"""Jump consistent hash (Lamping and Veach, 2014), translated from their C++."""
b, j = -1, 0
while j < buckets:
loops["jump"] += 1
b = j
key = (key * 2862933555777941757 + 1) % 2**64
j = int((b + 1) * (2**31 / ((key >> 33) + 1)))
return b
class Rendezvous:
"""Score every node with hash(node, key); the highest score wins."""
def __init__(self, nodes: list[str]):
self.nodes = list(nodes)
def owner(self, key: str) -> str:
return max(self.nodes, key=lambda node: h64(f"{node}|{key}"))
class Jump:
"""Bucket numbers 0..n-1 index a list of node names."""
def __init__(self, nodes: list[str]):
self.nodes = list(nodes)
def owner(self, key: str) -> str:
return self.nodes[jump(h64(key), len(self.nodes))]
def jump_without(node: str) -> Jump:
"""Jump can only drop the last bucket, so move the last node into the gap."""
nodes = list(NODES)
nodes[nodes.index(node)] = nodes.pop()
return Jump(nodes)
def moved(a, b) -> str:
return f"{sum(a.owner(k) != b.owner(k) for k in KEYS) / len(KEYS):.1%}"
schemes = {
"mod N": ModN,
"ring v=1": lambda nodes: HashRing(nodes, 1),
"ring v=100": lambda nodes: HashRing(nodes, 100),
"rendezvous": Rendezvous,
"jump": Jump,
}
print(f"{'scheme':<12}{'max/mean':>9}{'moved on add':>14}{'moved on remove':>17}")
for name, make in schemes.items():
base = make(NODES)
load = Counter(base.owner(k) for k in KEYS)
grown = make(NODES + ["node-10"])
if name == "jump":
shrunk = jump_without("node-3")
else:
shrunk = make([n for n in NODES if n != "node-3"])
print(f"{name:<12}{max(load.values()) / (len(KEYS) / 10):>9.2f}"
f"{moved(base, grown):>14}{moved(base, shrunk):>17}")
loops.clear()
for key in KEYS:
jump(h64(key), 10)
print(f"jump hash: {loops['jump'] / len(KEYS):.2f} loop rounds per key for 10 buckets")
full, rest = Rendezvous(NODES), Rendezvous([n for n in NODES if n != "node-3"])
gone = Counter(rest.owner(k) for k in KEYS if full.owner(k) == "node-3")
print(f"rendezvous: node-3's {sum(gone.values()):,} keys went to {len(gone)} nodes,",
f"{min(gone.values()):,} to {max(gone.values()):,} each")python3 compare.pyscheme max/mean moved on add moved on remove
mod N 1.01 90.9% 90.1%
ring v=1 3.72 0.3% 6.2%
ring v=100 1.14 9.3% 11.4%
rendezvous 1.01 9.0% 10.0%
jump 1.01 9.1% 18.9%
jump hash: 2.93 loop rounds per key for 10 buckets
rendezvous: node-3's 10,035 keys went to 9 nodes, 1,069 to 1,145 eachRendezvous and jump hashing balance as well as modulo (1.01) with no virtual nodes, and both move close to the ideal 1/11 when a node joins. Rendezvous moves exactly node-3's 10.0% when node-3 leaves, and those 10,035 keys spread over all nine survivors, between 1,069 and 1,145 each. Jump hash moves 18.9% when a middle node leaves: node-3's keys must move, and most of node-9's keys move too, because its old bucket disappears. That is close to twice the minimum. Jump hash ran its loop 2.93 times per key on average, under the paper's bound of ln 10 + 1 ≈ 3.3.
05. Choosing a scheme
scheme balance keys moved when one node lookup work client state
joins or leaves
mod N even about N/(N+1) 1 hash node list
ring even if V big that node's share 1 hash + log2(NxV) NxV tokens
rendezvous even that node's share N hashes node list
jump even last bucket: its share; 1 hash + ~ln N node count
any other: about 2x loop rounds and orderThe table suggests a starting point, not a rule. A client-side cache tier with a few dozen servers that fail at random suits a ring with many virtual nodes or rendezvous hashing; rendezvous is simpler to get right, and the ring is cheaper when there are many servers. A storage system whose shards are replicated and only ever added at the end suits jump hash, because it needs no memory and balances almost perfectly. Servers of different sizes are easiest on a ring, where a bigger server simply gets more tokens; weighted versions of rendezvous hashing also exist.
Load balancers add one more option. Envoy offers ring_hash and maglev. Maglev, described by Google in 2016, fills a fixed lookup table (65,537 entries by default in Envoy) so that a lookup is a single array read. Envoy's documentation says it builds and looks up much faster than a ring but is less stable: simulations show about twice as many keys moving when hosts are removed. A larger table reduces that.
06. Check your understanding
07. Apply it
Extend your practice report with the two new schemes:
- Add rows for rendezvous and jump hashing to the table you made in the lab, using 1,000,000 keys if your machine allows.
- Time 10,000 lookups for the ring and for rendezvous hashing with 10, 100 and 1,000 nodes (
time.perf_counter), and note where rendezvous starts to cost more than the ring. Timings vary by machine, so report your machine and Python version. - Write one paragraph recommending a scheme for a 40-server memcached tier and one for a store with 256 replicated shards, giving the property that decided each.
08. Review prompt
Without notes, explain how rendezvous hashing chooses a node, why a removed node's keys spread evenly, and what one lookup costs. Then explain jump hash's idea of keys jumping to new buckets, why it needs no memory, and why removing a bucket from the middle costs about twice the minimum.
References
- Lamping and Veach (2014): A Fast, Minimal Memory, Consistent Hash Algorithm
- Thaler and Ravishankar (IEEE/ACM ToN, 1998): Using Name-Based Mappings to Increase Hit Rates
- Rendezvous hashing (overview, Wikipedia)
- Envoy 1.40 documentation: supported load balancers (ring hash, Maglev)
- pymemcache source: HashClient and its default rendezvous hasher
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
6 questions · about 5 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.