CONCEPT SHEET · Concepts

Consistent Hashing

One-liner: Map keys and nodes onto a ring so adding or removing a node remaps only ~1/N of keys instead of nearly all of them.

ArticleConcepts© Arthur Bernier Jr551 words1 code block

Say this#

How it works#

TEXT
        0   D         A      key k hashes to 73 -> walk clockwise -> node B     \     /        remove B -> k goes to C; only B's range moves  C ---- + ---- B     /     \       ...
  1. Hash each node ID (plus vnode index) to points on the ring.
  2. Hash the key; walk clockwise to the first node point; that node owns the key.
  3. Add node: it takes over a slice from its clockwise successor; ~K/N keys move.
  4. Remove node: its slice falls to the successor.
  5. Virtual nodes: 100-200 points per physical node so load variance is a few percent and heterogeneous nodes can get proportional weight.

Why not mod N: going from N to N+1 remaps roughly N/(N+1) of all keys (near 100%), causing cache misses or data movement everywhere.

Trade-offs#

ChoiceProCon
Consistent hashing with vnodesMinimal remap, balanced load, weighted nodesRing metadata; lookups O(log N) via sorted array / binary search
Mod NTrivial, O(1)Near-total remap on resize
Rendezvous hashingNo ring state, naturally balancedO(N) per lookup unless optimized
Few vnodesLess metadataLoad imbalance, slow rebalancing
Many vnodesSmooth loadLarger routing table; more replication streams

Numbers#

  • Adding one node to N moves ≈ 1/(N+1) of keys; mod-N moves ≈ N/(N+1).
  • Without vnodes, load imbalance between nodes can be 2x+; with ~100-200 vnodes the standard deviation drops to roughly 5-10%.
  • Cassandra default: 256 vnodes per node historically (now commonly 16 with the newer allocation algorithm); DynamoDB and Riak partition the ring into fixed partitions (Riak default 64).

In the interview#

See also