Why Distributed Systems Use Consistent Hashing
π― The Questionβ
"In a distributed cache with 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 ():
- If you have 4 servers and 1 server crashes (or you add a 5th server), 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:
- Both servers and keys are hashed onto a circular 360Β° Hash Ring ().
- A key is assigned to the first server encountered moving clockwise.
- When a server is added or removed, only 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β
| Property | Naive Modulo Hashing (hash % N) | Consistent Hashing with V-Nodes |
|---|---|---|
| Key Redistribution on Node Change | ~100% of keys re-hashed (Massive Cache Thrash) | Only of keys moved |
| Horizontal Scalability | Disastrous (Requires full data reload) | Seamless (Zero-downtime scaling) |
| Hotspot Prevention | None | Solved using Virtual Nodes (V-Nodes) |
| Industry Adoption | Monolithic single-node systems | DynamoDB, Cassandra, Memcached, CDNs |
π‘ What Interviewers Ask Next (Follow-Up Traps)β
-
"How does binary search find the assigned server in time?"
- Answer: Server positions are maintained in a sorted array or red-black tree (e.g.,
std::mapor JavaTreeMap). When a key hash is computed,upper_bound()orceilingEntry()performs a binary search to find the nearest clockwise server in time.
- Answer: Server positions are maintained in a sorted array or red-black tree (e.g.,
-
"How do DynamoDB and Cassandra handle data replication using Consistent Hashing?"
- Answer: To replicate data with a replication factor , the coordinator node assigns the key to the first node on the ring, and then automatically replicates copies to the next successive distinct physical nodes clockwise along the ring.
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 of the keys, preventing catastrophic cache stampedes in distributed systems.
πΊ Video Explanationβ
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 Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.