Course map

Topic 074 / Core

Consistent hashing

Chapter 5.5 · Distributing Data

What and why

Plain hash(key) % N sharding reshuffles nearly every key when N changes — a membership change becomes a near-total data movement event. Consistent hashing places keys and nodes on the same hash ring so that a membership change moves only the keys near the changed node. Builds on: → 73 (partitioning and sharding).

Diagram

Consistent hashing ring Four nodes, A, B, C, and D, sit at fixed points around a hash ring marked with 0 degrees at top, 90 degrees at right, 180 degrees at bottom, and 270 degrees at left. Key k hashes to a point on the ring; walking clockwise from k, the first node reached is A, so A owns k. 0° 90° 180° 270° D A C B k owns k key k → hash(k) → walk clockwise → first node found owns k
Virtual nodes: each physical node is placed at many points on the ring, smoothing out the load imbalance a small number of real nodes would otherwise have by chance. Membership change: adding or removing a node moves only the keys between it and its clockwise neighbor — e.g. with 4 nodes, removing one moves only that node's roughly one-quarter share, unlike plain hash(key) % N, which reshuffles nearly everything.
Text version of this diagram
            0°
       D ·       · A
   270°             90°
       C ·       · B
           180°
key k → hash(k) → walk clockwise → first node found owns k

How it works

  1. Hash both nodes and keys onto the same fixed circular space (the "ring").
  2. A key is owned by the first node encountered walking clockwise from the key's position.
  3. Adding or removing a node moves only the keys between it and its clockwise neighbor — not a full remap.
  4. Virtual nodes: each physical node is placed at many points on the ring, smoothing out the load imbalance a small number of real nodes would otherwise have by chance.
  5. This is the standard mechanism behind distributed caches and leaderless, Dynamo-style stores assigning keys to nodes.

Practical example

(illustrative). With 4 nodes on a ring, removing one moves only that node's roughly one-quarter share of keys to its clockwise neighbor — the other three nodes' keys are untouched, unlike plain modulo hashing where changing N from 4 to 3 reshuffles nearly everything.

When to use it

  • Use when: nodes are added or removed from a distributed store or cache and minimizing key movement on membership change matters.
  • Avoid when: the cluster size is fixed and essentially never changes — plain hash(key) % N is simpler and the one-time remap on a rare resize is acceptable.
  • Failure scenarios: too few virtual nodes per physical node leaves the ring badly imbalanced by chance, even though the algorithm is "consistent."
  • Simpler alternative: static hash(key) % N when N is fixed by design.

Practice

(mechanism — trace by hand, 20 min). Draw a ring numbered 0–359. Place 4 nodes and 12 keys at chosen positions. Assign each key to the next clockwise node. Remove one node and recompute. Count how many keys moved, and compare that count to how many would have moved under plain hash(key) % N. Done when both counts are computed and the gap between them is explained in one sentence.

Key takeaway

Consistent hashing bounds key movement on membership change to roughly 1/N of the keyspace, not nearly all of it. Done when you can: hand-trace a ring lookup and a node removal and count the keys that moved. Used next in → 76; quorums → 130; anti-entropy → 131; Capstone.

References

DeCandia et al., "Dynamo: Amazon's Highly Available Key-value Store" — see references.md. Kleppmann and Riccomini, Designing Data-Intensive Applications — see references.md.

Source directory

Search the guide