Consistent Hashing
Distributing keys across servers with minimal remapping when nodes scale up or crash
Difficulty: Intermediate · Read time: 7 min · Access: Step-1 Preview + Knoera Pro
How It Works
Naive modulo sharding (`hash(key) % N`) breaks catastrophically whenever node count `N` changes—nearly 100% of keys remap to different servers at once, causing massive cache misses. Consistent Hashing maps both servers and keys onto a circular `0 .. 2³² − 1` hash ring (using a hash function like MurmurHash or MD5). Each key lives on the first node encountered moving clockwise, so adding or removing 1 node only moves `~1/N` of keys.
Why It Matters
Distributed caches (Redis Cluster, Memcached), NoSQL (Not Only SQL) databases (Apache Cassandra, Amazon DynamoDB), and stateful WebSocket (persistent full-duplex connection) routers rely on consistent hashing with Virtual Nodes (`vnodes`) to scale elastically without triggering a thundering-herd storm of database queries.
Interactive Architecture Scenarios
- Adding a Node with Minimal Key Movement
- Failure Mode: Naive `hash(key) % N` Reshuffling