Consistent Hashing Explained
π Why Do We Even Need Hashing?
In distributed systems, we need to:
Store data across multiple servers
Retrieve it efficiently
A simple approach is:
server = hash(userId) % number_of_servers
β Benefits:
Same input β same server (deterministic)
Easy read/write mapping
β οΈ Problem with Simple Hashing
Letβs say:
userId % 3 β Server A
Now we add one more server:
userId % 4 β Server B
π Same user now maps to a different server
β Issues:
Almost all keys get reassigned
Massive data redistribution
Not scalable
π‘ Enter Consistent Hashing
Instead of % N, we:
Map both servers and keys onto a ring
Hash space typically ranges from
0 β 2^32
Think of it like a clock π
π How Consistent Hashing Works
Hash each server β place it on the ring
Hash each key β place it on the ring
Assign key to:
π First server in clockwise direction
π§ Example
Key β 250
Servers:
S1 β 100
S2 β 200
S3 β 300
π Key (250) will be assigned to S3
β Adding a New Server
Suppose we add S3 between S2 and S4
Before:
- Keys in range (S2 β S4] β stored in S4
After:
- Keys in range (S2 β S3] β move to S3
π Only a small subset of keys move
β Removing a Server
If a server is removed:
- Its keys move to the next clockwise server
π Again, minimal data movement
π₯ Why Consistent Hashing is Powerful
| Feature | Normal Hashing | Consistent Hashing |
|---|---|---|
| Add Server | Rehash almost all data β | Minimal movement β |
| Remove Server | Rehash almost all data β | Minimal movement β |
| Scalability | Poor β | Excellent β |
βοΈ Virtual Nodes (VNodes) β Solving Uneven Load
π¨ Problem Without Virtual Nodes
If each server is placed only once:
S1 ---- S2 - S3 - S4 ---------------------- S1
π Large gaps can form on the ring
β Result:
Some servers handle huge portions of data
Load becomes uneven
π‘ Solution: Virtual Nodes
Instead of placing each server once:
π Each server is placed multiple times on the ring
Example:
S1 β hash("S1-1"), hash("S1-2"), hash("S1-3")
S2 β hash("S2-1"), hash("S2-2"), hash("S2-3")
π How Virtual Nodes Are Placed
Virtual nodes are created using:
Same hash function
Different identifiers
Example:
hash("S1-1")
hash("S1-2")
hash("S1-3")
β Deterministic
β Evenly spread
β Looks random but predictable
π How Distribution Improves
Instead of one large chunk per server:
S1 S2 S3 S1 S4 S2 S3 S1 S4 ...
π Each server now owns multiple small ranges
β Result:
No large gaps
Better load balancing
π₯ Handling Hotspots
What is a Hotspot?
When:
A particular key gets huge traffic
Example: celebrity user, viral content
β Without Virtual Nodes:
- All traffic β one server
π Server overload
β With Virtual Nodes:
Data is distributed across multiple positions
Requests are more evenly spread
π Load gets distributed across cluster
β οΈ Important Clarification
Virtual nodes:
Do NOT completely eliminate hotspots
They:
Reduce uneven distribution
Improve probability of balanced load
For real-world systems, we also use:
Replication
Caching (Redis, CDN)
Load balancing
π Real-World Systems Using This
Cassandra
DynamoDB
Redis Cluster
CDN systems
Load balancers
π§© Final Summary
Consistent hashing:
Distributes data across servers efficiently
Minimizes data movement during scaling
Handles dynamic server addition/removal
Works even better with virtual nodes
βοΈ My Take
Initially, hashing felt like:
userId % N
But that approach breaks at scale.
Consistent hashing solves this elegantly by:
Using a ring-based model
Minimizing data reshuffling
Enabling scalable distributed systems
π― When Should You Use This?
Distributed caches
Database sharding
Load balancing
CDN routing
π Closing Thought
Consistent hashing is not just an optimization β
itβs what makes large-scale distributed systems practical.