Interview's MateSystem Design
Interview's Mate

System Design

Architecture & trade-offs

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
System Design topics
Lesson 6/ 31
Fundamentals & Networking

Consistent Hashing

Distribute keys across nodes so adding or removing a server only moves ~1/N of the data.

IntermediateHash Ring · Virtual Nodes3 quiz questions0/2 subtopics done
Practice in AI HLD
In plain wordsDiagramTheorySubtopics (2)Step by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice
Deep-dive subtopics (2) - 0/2 completed
Modulo Hashing vs. Consistent Hashing

See why hash % N causes massive cache misses during scaling and how the ring moves only 1/N keys.

Intermediate
Virtual Nodes & Hot-Spot Mitigation

Eliminate uneven slices and handle celebrity hot keys cleanly.

Intermediate

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

Loading interactive diagram
Loading interactive exercise

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.

Key takeaway: Modulo hashing remaps almost all keys whenever server count changes.

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 4
Ring Topology

1. 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.

K virtual nodes (e.g. 100–200) per physical server prevent skew
Key numbers at this step
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

Modulo Hashing (hash % N)
Best for: Fixed-size arrays where N never changes

Strengths: O(1) arithmetic; trivial to implement.

Watch out: Changing N remaps almost every key, triggering massive cache misses.

Consistent Hashing + Vnodes
Best for: Distributed caches (Memcached/Redis), Dynamo/Cassandra rings

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).

Rendezvous (HRW) Hashing
Best for: Small-to-medium server pools needing minimal metadata

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?
Interview tip: Always pair Consistent Hashing with Virtual Nodes (vnodes) and explain how replicas pick the next R distinct physical hosts.

Knowledge check

Loading quiz

Put it into practice

Design full architectures with AI feedback

Apply Consistent Hashing inside real High-Level Design prompts or review Q&A flashcards.

Open AI HLD Practice →SD Interview Q&A
Previous topic
CDN & Edge Computing
Next topic
Networking & Protocols