Consistent Hashing
Distribute keys across nodes so adding or removing a server only moves ~1/N of the data.
In plain words
Consistent hashing decides which server stores which piece of data. When you add or remove a server, only a small part of the data has to move instead of almost all of it.
Think of it like…
People seated around a circular clock table: if one guest leaves, only the dishes right next to them pass to the neighbor instead of everyone swapping seats.
Where you’ve seen it
A cache cluster grows from 10 to 11 servers. With consistent hashing only about 1/11 of the keys move, so the cache stays mostly warm and the database isn’t flooded.
Words you’ll hear
- Hash
- A function that turns a key (like a user ID) into a number.
- Hash ring
- Servers and keys placed on a circle; each key belongs to the next server clockwise.
- Virtual nodes
- Each real server appears many times on the ring so load spreads evenly.
- Rebalancing
- Moving data between servers when the cluster changes size.
Visual diagram
Theory & deep dive
The problem with simple `hash(key) % N`
Suppose you have 4 Redis cache servers (0, 1, 2, 3) and assign each key using hash(key) % 4. Everything works nicely until Server 3 crashes or you add Server 4 (% 5).
Because the divisor changed from 4 to 5, almost every key maps to a different server! Your cache hit rate plunges toward 0%, and all traffic slams your database at once.
How the Hash Ring & Virtual Nodes solve it
Consistent hashing imagines a circle from 0 to 2³² − 1. You hash each server’s ID to place it on the circle, and hash each data key onto the same circle. Walk clockwise from a key to find its owner server.
When a server joins or leaves, only the keys in that small arc of the circle move - roughly 1 / N of all keys. Adding 100–200 virtual nodes per physical machine sprinkles each server around the circle so every machine carries an equal share of traffic, even if one node is larger than another.
How it works, step by step
Step 1 of 41. Place servers on a circular hash ring
Hash each server ID (or multiple virtual nodes per server) onto [0, 2³² − 1] arranged in a circle.
- Ring Space
- 0 .. 2³² − 1
- Vnodes/Server
- 100–200
When to use it & trade-offs
What it is
Consistent hashing maps both servers and keys onto the same circular hash space (0 to 2³²−1). A key is owned by the first server clockwise from its hash.
Why it matters: Naive hash(key) % N breaks nearly 100% of keys whenever N changes (node crash or scale-out), causing catastrophic cache stampedes.
When to reach for it
- Distributed cache clusters (Redis, Memcached) during scale-out or node failure
- Leaderless NoSQL databases (Cassandra, DynamoDB) for token ranges and replicas
- Sticky L4/L7 routing to stateful workers (e.g., rate limiters, connection pools)
When to avoid or be careful
- Using consistent hashing without virtual nodes when N is small (creates huge load imbalance)
- Expecting consistent hashing alone to fix a single ultra-hot celebrity key
Partitioning Key Placement Strategies
Strengths: O(1) arithmetic; trivial to implement.
Watch out: Changing N remaps almost every key, triggering massive cache misses.
Strengths: Only ~1/N keys move on topology change; easy replication to next R clockwise hosts.
Watch out: Requires maintaining ring membership (gossip or ZooKeeper/etcd).
Strengths: Perfect balance without virtual nodes; computes weight per node directly.
Watch out: O(N) hash evaluations per lookup (slower if N is thousands of nodes).
Interview signals & numbers to know
When this topic comes up
- distributed cache
- node failure
- minimal rehashing
- sharding ring
- dynamo
Benchmarks & rules of thumb
- Virtual nodes per host
- 100–200 vnodes
- Standard deviation of load
- <5–10% with 150 vnodes
- Keys moved on adding 1 to N
- ~1 / (N + 1)
- Lookup complexity
- O(log(N · V))
Common mistakes & follow-ups
Common interview mistakes
- Forgetting virtual nodes - with 3 servers on a raw ring, one server can easily get 60% of the arc
- Walking clockwise for replicas without skipping vnodes that belong to the same physical server
- Assuming consistent hashing fixes hot keys (a single hot key still hits one primary node unless salted)
Likely interviewer follow-ups
- Why do virtual nodes solve both load skew and heterogeneous hardware capacity?
- When replicating to the next 3 nodes clockwise, what bug happens if you do not skip duplicate physical hosts?
- How does Consistent Hashing compare to Maglev or Rendezvous hashing?
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply Consistent Hashing inside real High-Level Design prompts or review Q&A flashcards.