Part 12 · 1 chapters · ~11 min
Membership, Gossip and Hashing
How large clusters work without a centre: gossip that spreads state in logarithmic rounds, SWIM failure detection with indirect pings and suspicion, why hash mod N moves almost every key, consistent hashing with virtual nodes, and the rendezvous, jump and range alternatives.
21
Who is alive, and where each key lives
code
// consistent hashing with virtual nodes
import { createHash } from 'node:crypto';
const h = (s: string) => createHash('md5').update(s).digest().readUInt32BE(0);
class Ring {
private points: { at: number; node: string }[] = [];
add(node: string, vnodes = 128) {
for (let v = 0; v < vnodes; v++) this.points.push({ at: h(`${node}#${v}`), node });
this.points.sort((a, b) => a.at - b.at);
}
remove(node: string) { this.points = this.points.filter(p => p.node !== node); }
get(key: string): string {
const k = h(key); let lo = 0, hi = this.points.length;
while (lo < hi) { const mid = (lo + hi) >> 1; if (this.points[mid].at < k) lo = mid + 1; else hi = mid; }
return this.points[lo % this.points.length].node; // next point clockwise, wrapping
}
}
// adding a fourth node to three moves about a quarter of keys; mod N would move about three quarters| where you meet it | what it does |
|---|---|
| CDNs and load balancers | consistent hashing keeps a user or URL on the same edge cache, so hits survive scaling |
| Redis Cluster | 16,384 hash slots assigned to nodes, plus gossip for membership |
| Cassandra, DynamoDB-style stores | a token ring with virtual nodes and replication to the next nodes clockwise |
| Kubernetes and service meshes | health checks and endpoints from a control plane, a centralised alternative to gossip |
MEMBERSHIP, GOSSIP AND CONSISTENT HASHING
how a cluster knows who is in it, spreads news without a centre, and places keys so that adding a node moves little
swipe the figure sideways, or tap expand for full screen
1/6
gossip
Gossip: every second, each node picks a few random peers and exchanges what it knows (members, versions, heartbeats). News spreads like an epidemic: with fanout f, it reaches all N nodes in roughly log N rounds, and no node is special. Used in Cassandra, Consul and Redis Cluster.