Part 1 · 1 chapters · ~8 min

Consistent and Rendezvous Hashing

Why hash mod N fails when nodes change, consistent hashing rings with virtual nodes, bounded loads, rendezvous (highest random weight) hashing, Google's jump consistent hash, Maglev hashing for load balancers, and where each is used (caches, databases, CDNs, Envoy).

2

Placing keys on changing node sets

code
// rendezvous hashing: score each node, pick the best
import { createHash } from 'node:crypto';
const score = (key: string, node: string) => createHash('md5').update(key + '|' + node).digest().readUInt32BE(0);
const pick = (key: string, nodes: string[]) => nodes.reduce((best, n) => score(key, n) > score(key, best) ? n : best);

// jump consistent hash (Lamping and Veach, 2014)
function jump(key: bigint, buckets: number): number {
  let b = -1n, j = 0n;
  while (j < BigInt(buckets)) { b = j; key = (key * 2862933555777941757n + 1n) & 0xFFFFFFFFFFFFFFFFn; j = BigInt(Math.floor(Number(b + 1n) * (2 ** 31 / Number((key >> 33n) + 1n)))); }
  return Number(b);
}

Maglev hashing (Google's load balancer) fills a lookup table so each backend gets an even share and connections mostly stay put when backends change; Envoy offers ring hash and Maglev for sticky load balancing.

KEYS MOVED WHEN ADDING A 5TH NODE
share of keys that change node, by placement algorithm
hash mod N~80%consistent hashing (vnodes)~20% (1/5)rendezvous hashing~20%jump consistent hash~20%
swipe the figure sideways, or tap expand for full screen
1/4
mod N
hash(key) mod N moves most keys when N changes from 4 to 5 (Distributed Systems P12 showed 16 of 20 in a small example): a cache cluster loses most of its hits at once.
most keys movea cold cache after scaling