Back to all posts

System Design Explained to a 5-Year-Old · Episode 13

Consistent Hashing Explained: Kids Around a Round Lunch Table

#System Design#Consistent Hashing#Distributed Systems#Caching#Sharding#Scalability#what is consistent hashing#consistent hashing explained simply#consistent hashing for beginners#hash ring#virtual nodes#modulo hashing problem#distributed cache#dynamodb cassandra partitioning#rebalancing data#system design interview#eli5 system design
Consistent Hashing Explained: Kids Around a Round Lunch Table
August 30, 20267 min read

Imagine a teacher handing out 100 lunchboxes to 4 helpers. Her rule is simple: "Look at the number on the lunchbox, divide by 4, and the remainder tells you which helper carries it." Box 9 → 9 ÷ 4 leaves 1 → Helper 1.

It works perfectly… until a fifth helper joins. Now the rule becomes "divide by 5," and suddenly almost every lunchbox belongs to a different helper. All 100 boxes get passed around the room. Chaos!

So the teacher tries something smarter. She seats everyone around a round lunch table. Helpers sit at a few spots around the circle, and every lunchbox is placed at a spot on the table edge too. The rule: "Each lunchbox belongs to the first helper you meet walking clockwise."

When a new helper sits down, they only take over the lunchboxes between them and the helper before them. Everyone else keeps their boxes.

Kids sitting at a round lunch table with lunchboxes placed around the edge, each box walking clockwise to the nearest kid

That round table is Consistent Hashing, the trick that lets Redis clusters, Cassandra, DynamoDB, and CDNs add or remove servers without moving all their data.

Keywords

  • Hash Function: Turns any key (like user:42) into a big, random-looking number.
  • Modulo Hashing: server = hash(key) % N. Simple, but breaks badly when N changes.
  • Hash Ring: Imagining the hash numbers bent into a circle, where the biggest number wraps back to 0.
  • Node: A server (cache, database shard) placed at a point on the ring.
  • Virtual Nodes (VNodes): Placing each real server at many points on the ring to spread load evenly.
  • Rebalancing: Moving data when servers are added or removed.

Character Mapping

  • Hash Ring: The Round Lunch Table.
  • Servers / Nodes: Helper kids sitting at the table.
  • Keys / Data: Lunchboxes placed around the table's edge.
  • Clockwise Lookup: "Walk clockwise until you meet a helper."
  • Virtual Nodes: One helper wearing several name tags and sitting in several seats.

1. The Problem with Simple Modulo Hashing

In Episode 11, we sharded data with hash(key) % N. Let's see what happens with 4 cache servers when we add a 5th:

Keyhash(key)% 4 (old)% 5 (new)Moved?
user:118Server 2Server 3✅ Moved
user:225Server 1Server 0✅ Moved
user:340Server 0Server 0❌ Stayed
user:433Server 1Server 3✅ Moved
user:552Server 0Server 2✅ Moved

Going from N to N+1 servers moves roughly 80% or more of all keys. For a cache, that means a sudden flood of cache misses. Every request falls through to the database at once (a cache stampede), and the database can collapse. That's a terrible way to scale up during a traffic spike!


2. How Consistent Hashing Works

A hash ring from 0 to 2^32 with servers A, B, C placed on it and keys walking clockwise to their owner

Step 1: Build the Ring Picture every possible hash value (0 to about 4.3 billion) laid out around a circle.

Step 2: Place Servers on the Ring Hash each server's name (hash("server-A")) and put it at that spot on the circle.

Step 3: Place Keys on the Ring Hash each key (hash("user:42")) and put it on the circle too.

Step 4: Walk Clockwise A key belongs to the first server you meet walking clockwise from its position.

Adding a Server ➕

Server D lands between A and B. It only takes the keys between A and D, which used to belong to B. Every other key stays where it was.

With N servers, adding one moves only about 1/N of the keys instead of nearly all of them.

Removing a Server ➖

If Server B crashes, its keys simply roll clockwise to the next server, C. Nothing else moves.


3. The Lumpy Table Problem and Virtual Nodes

With only 3 helpers, random seating might put two of them right next to each other. One helper ends up guarding half the table while another guards a tiny sliver. Uneven load!

The fix: Virtual Nodes. Each real server gets placed on the ring 100–200 times under different names (server-A#1, server-A#2, …). With so many seats scattered around, each server ends up owning a fair, even share of the circle.

Bonus powers:

  • Unequal machines: Give a bigger server more virtual nodes so it takes more load.
  • Smoother failures: When a server dies, its many small slices spread across all remaining servers instead of dumping everything on one neighbor.

Without virtual nodes, servers own uneven arcs; with virtual nodes, each server's many small slices are evenly spread


4. Where Consistent Hashing Shows Up

  • Amazon DynamoDB & Apache Cassandra: Partition data across nodes using a hash ring. Replicas are stored on the next few servers clockwise.
  • Distributed Caches (Memcached clients, Redis Cluster's hash slots): Add cache nodes during peak hours without wiping the whole cache.
  • CDNs (Episode 6): Decide which edge server inside a data center caches a given file, so the same file isn't stored on every machine.
  • Load Balancers (Episode 5): "Sticky" routing that sends the same user to the same backend, with minimal disruption when servers change.
  • Discord: Routes each chat server (guild) to a specific backend process using a hash ring.

Modulo Hashing vs Consistent Hashing

🔢 Modulo (hash % N)⭕ Consistent Hashing
Keys moved when adding a serverAlmost allAbout 1/N
Handles server failureMassive reshuffleOnly the failed server's keys move
Load balanceEven (if N stays fixed)Even with virtual nodes
ComplexityOne line of codeNeeds a sorted ring + lookup
Use whenServer count never changesServers come and go (auto-scaling, failures)

🍕 Explain It Like You're 5

Kids are sitting around a merry-go-round, and balloons are tied at different spots on its edge.

Each balloon belongs to the first kid you reach if you walk around the circle clockwise.

When a new kid hops on, they only take the few balloons right before their seat. Everyone else keeps their balloons. Nobody has to swap everything around. That's Consistent Hashing!


🎯 The System Design Interview Summary

Whenever your design has data spread across servers that can be added or removed (distributed cache, sharded database, key-value store), mention consistent hashing.

Key Takeaways for Senior Interviews:

  1. State the problem first: hash % N reshuffles almost everything when N changes, which causes cache stampedes.
  2. Explain the ring: Servers and keys hashed onto a circle, ownership found by walking clockwise.
  3. Always add virtual nodes: Without them, load is uneven. This detail shows real understanding.
  4. Combine with replication: Store each key on the next 2–3 distinct servers clockwise for fault tolerance (the Dynamo/Cassandra approach).
  5. Know the alternatives: Redis Cluster uses 16,384 fixed hash slots, and rendezvous hashing is another option. Both solve the same problem.

Next episode: when the network between your servers breaks, you can't have everything. Time for the CAP Theorem.

Enjoyed this article? Share it: