Skip to main content

Why Distributed Systems Use Consistent Hashing

🎯 The Question​

"In a distributed cache with NN servers, why is modulo hashing (hash(key) % N) catastrophic when scaling up or down? How does Consistent Hashing solve key redistribution?"


⚑ 30-Second Elevator Pitch​

With traditional Modulo Hashing (extServer=exthash(extkey)(modN) ext{Server} = ext{hash}( ext{key}) \pmod N):

  • If you have 4 servers and 1 server crashes (or you add a 5th server), NN changes.
  • Almost 100% of existing keys remap to different servers, triggering a catastrophic Cache Miss Storm that cascades and crashes your underlying databases.

Consistent Hashing solves this:

  1. Both servers and keys are hashed onto a circular 360Β° Hash Ring ([0,232βˆ’1][0, 2^{32}-1]).
  2. A key is assigned to the first server encountered moving clockwise.
  3. When a server is added or removed, only KN\frac{K}{N} keys are moved on average; the rest stay on their existing servers.

🧠 Under-the-Hood: The 360Β° Hash Ring​


πŸ”¬ Virtual Nodes (V-Nodes) for Uniform Distribution​

In a naive ring with 3 servers, nodes might land close to each other, creating Hotspot imbalance (one node handling 70% of keys).

Solution: Virtual Nodes:

  • Each physical server is assigned multiple positions on the ring (e.g. ServerA-01, ServerA-02, ... ServerA-100).
  • Virtual nodes interleave across the ring, ensuring an even, uniform distribution of keys and predictable load balancing.

πŸ“Œ Comparison Matrix: Modulo Hashing vs. Consistent Hashing​

PropertyNaive Modulo Hashing (hash % N)Consistent Hashing with V-Nodes
Key Redistribution on Node Change~100% of keys re-hashed (Massive Cache Thrash)Only 1N\frac{1}{N} of keys moved
Horizontal ScalabilityDisastrous (Requires full data reload)Seamless (Zero-downtime scaling)
Hotspot PreventionNoneSolved using Virtual Nodes (V-Nodes)
Industry AdoptionMonolithic single-node systemsDynamoDB, Cassandra, Memcached, CDNs

πŸ’‘ What Interviewers Ask Next (Follow-Up Traps)​

  1. "How does binary search find the assigned server in O(log⁑N)O(\log N) time?"

    • Answer: Server positions are maintained in a sorted array or red-black tree (e.g., std::map or Java TreeMap). When a key hash is computed, upper_bound() or ceilingEntry() performs a binary search to find the nearest clockwise server in O(log⁑S)O(\log S) time.
  2. "How do DynamoDB and Cassandra handle data replication using Consistent Hashing?"

    • Answer: To replicate data with a replication factor R=3R=3, the coordinator node assigns the key to the first node on the ring, and then automatically replicates copies to the next Rβˆ’1R-1 successive distinct physical nodes clockwise along the ring.

Placement & Interview Takeaway

Interview Answer: Consistent Hashing maps both data keys and server nodes to a circular hash ring. It decouples cache distribution from the node count, ensuring that adding or removing a node only redistributes 1N\frac{1}{N} of the keys, preventing catastrophic cache stampedes in distributed systems.


πŸ“Ί Video Explanation​

πŸš€ Level Up Your Engineering Mindset

Never Study in Isolation. Join Binary Dose.

Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.

Discord
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚑
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

πŸ’‘Have handwritten notes, cheat-sheets, or want to publish a technical article?
Learn How to Contribute→