← Data structures

Consistent hash ring

Hashing and probabilistic sketches

A sorted array of hash positions, each tagged with the node that owns it, arranged as a circle so every key hashes to a point and belongs to the next position clockwise. It exists because hash mod N reassigns almost every key when N changes, and a ring reassigns only the keys between the new position and its predecessor.

What it costs

OperationCostNote
Find owner of keyO(log V): 1 hash + binary search over V positionsV = virtual nodes in the ring, e.g. 50 nodes × 150 = 7,500 positions, about 13 comparisons.
Add a nodeO(V_node log V) to insert its positionsMoves about 1/N of all keys, spread evenly across the other nodes.
Remove a nodeO(V_node log V) to delete its positionsIts keys fall to the next position clockwise; with virtual nodes that is many different neighbours.
Rebuild from node listO(V log V)Sort all positions once; clients do this when they receive a new ring version.

Memory: One 32-bit (or 64-bit) position plus a node id per virtual node: 50 nodes × 150 virtual nodes × 12 bytes ≈ 90 KB. Small enough for every client to hold a copy.

Positions on the ring and the clockwise walk

Nodes own many interleaved positions. A key hashes to a point and belongs to the first position clockwise. Adding node-51 inserts positions that take only the slice just before each of them.

How it works

  1. Define the ring as the range of a hash function, usually 0 to 2^32 − 1, treated as a circle so that the position after 2^32 − 1 is 0.
  2. For each physical node, hash several strings such as "node-7#0", "node-7#1" … "node-7#149" to get 150 positions. Each position is a virtual node: a point on the ring tagged "node-7". Store all positions from all nodes in one sorted array.
  3. To find a key’s owner, hash the key to a position, then binary-search the sorted array for the first position greater than or equal to it. The node tagged on that position owns the key. If the key’s position is past the last entry, wrap around to the first entry.
  4. To add a node, hash its 150 positions and insert them into the sorted array. Each inserted position takes over the keys between it and the previous position: a thin slice, taken from whichever node owned that slice. Across 150 slices the new node steals about 1/N of the keys, a little from everyone.
  5. To remove a node, delete its positions. Each slice it owned now belongs to the next position clockwise, which, because positions are interleaved, is a different node for nearly every slice. The failed node’s load spreads across the cluster instead of landing on one neighbour.

Why virtual nodes and not one position per node

  • With one position each, the gaps between positions are random, so one node can own three times the ring of another. 150 positions each averages the gaps out to within a few percent.
  • When a node dies, 150 small slices go to 150 different neighbours rather than one neighbour doubling its load.

Worked example

The distributed cache: 50 nodes, 800 GB of keys

  • Ring of 2^32 positions. 50 nodes × 150 virtual nodes = 7,500 sorted positions, about 90 KB per client copy. Finding an owner is one MurmurHash3 (about 20 ns) plus 13 binary-search steps.
  • Key "home:4471" hashes to position 2,914,000,117. The binary search finds the first position ≥ that: 2,914,201,880, tagged node-23. Node-23 owns the key.
  • Add node-51 to relieve memory pressure: its 150 positions land in 150 random gaps. One lands at 2,914,150,000, between node-23’s predecessor and node-23’s position, so the keys in that gap, including "home:4471", now belong to node-51. Across all 150 positions node-51 takes 1/51 ≈ 2% of keys, about 16 GB, from 40-odd different nodes. With modulo hashing the same change would have moved 98% of keys.
  • Node-17 fails: its 150 positions are removed and each slice falls to the next position clockwise. 2% of keys miss and are refetched, spread across about 45 surviving nodes, so each sees a 0.04% bump rather than one node absorbing a 2% jump.
  • Load spread: with one position per node the largest node owned 3.4× the average; with 150 virtual nodes the spread is ±5%.

When it is the wrong choice

  • The cluster size is fixed and never changes: plain hash mod N is one instruction with no ring to distribute or version.
  • You need range queries over keys (all orders for a day): hashing scatters adjacent keys across nodes. Use range partitioning with a directory of ranges instead.
  • The set of nodes is tiny (two or three) and you want perfectly even load: rendezvous hashing (score every node per key, pick the highest) is simpler and exactly balanced with no ring to maintain.
  • One key is hot: the ring assigns a key to exactly one node and cannot split it. Replicate the hot key under suffixed names or cache it locally.

Interviewer follow-ups

Answer out loud before you open each one.

How many virtual nodes per physical node, and what does the number trade off?

Typically 100 to 200. More positions make load more even (variance falls with the square root of the count) and spread a failed node’s keys more widely, but the sorted array grows and every ring update touches more entries. Past a few hundred the balance gain is negligible.

How does a client learn that the ring changed?

The ring is versioned. A cluster manager publishes the new version to a config service that clients watch, and nodes tag responses with the version they believe is current. A client that sends a request with a stale version gets a redirect or refresh hint and reloads the ring.

What is the difference between consistent hashing and sharding by range?

Consistent hashing places keys by hash, so keys are spread evenly and the ring decides ownership with no directory, but adjacent keys live anywhere. Range sharding keeps adjacent keys together and supports scans, but needs a directory of range-to-node mappings and manual splitting when a range gets hot.

Does consistent hashing help with a hot key?

No. A key hashes to one point and one owner no matter how many virtual nodes exist. Hot keys need a different fix: an in-process cache on each app server, or replicating the key under several suffixed names so reads spread across nodes.

Where it shows up

Building blocks
Add nodes without remapping everything · Partition the data · Fix a hot shard · Make sure only one worker does it · Find which server holds a user · Spread traffic across servers
Problems
Distributed Key-Value Cache (Redis / Memcached-like) · Chat App (WhatsApp / Messenger) · News Feed (Twitter / X timeline)