A distributed hashing scheme that minimizes key redistribution when nodes are added or removed. The backbone of distributed caches, databases, and load balancers.
Medium High FrequencyImagine a ring (circle) representing the entire hash space [0, 2ยณยฒ). Both nodes and keys are hashed onto this ring. Each key is assigned to the first node encountered clockwise from the key's position.
With simple hash(key) % N, adding or removing a server changes N, causing nearly all keys to be remapped. For a cache cluster, this means a near-100% miss rate โ catastrophic.
When a node is added, only keys between the new node and its predecessor (counter-clockwise) need to move โ roughly K/N keys (where K = total keys, N = total nodes). When a node is removed, only its keys move to the next node clockwise.
With few physical nodes, the ring can be unbalanced (one node owns a huge arc). Virtual nodes solve this: each physical node creates 100โ200 virtual nodes spread around the ring. This gives near-uniform distribution.
With N nodes and a new node added:
Number of virtual nodes: More vnodes = better load distribution but more memory and lookup time. 100โ200 vnodes per physical node is the sweet spot. Cassandra uses 256 by default (configurable).
Hash function choice: Must be fast and uniform. MD5 works but is overkill for non-crypto use. MurmurHash3 or xxHash are better โ faster and well-distributed. Avoid CRC32 (poor distribution).
Ring metadata storage: The ring map (sorted list of hash โ node) must be available to all clients. Options: gossip protocol (Cassandra), centralized config (ZooKeeper), or client library with seed nodes.
Replication with consistent hashing: Store each key on N successive nodes clockwise (replication factor). Skip virtual nodes of the same physical node to ensure replicas are on different machines.
Consistent hashing comes up whenever the interviewer asks about distributed data partitioning or scaling a cluster.
Interview signal: The interviewer wants to see you understand why naive modulo hashing fails and how consistent hashing minimizes disruption during scaling events.
| Metric | Value |
|---|---|
| Keys remapped on node add (naive hash) | ~(N-1)/N โ 90% for N=10 |
| Keys remapped on node add (consistent) | ~K/N โ 10% for N=10 |
| Recommended virtual nodes per physical | 100โ200 |
| Cassandra default vnodes | 256 |
| Ring lookup time (sorted array + binary search) | O(log V) where V = total vnodes |
| Ring memory (10 nodes ร 200 vnodes) | ~2000 entries โ negligible |
| Load std deviation (150 vnodes) | ~5โ10% of mean |
| MurmurHash3 throughput | ~3โ5 GB/s |