Scalability and Load BalancingLesson 2.3
Consistent hashing explained for system design interviews
hash ring, virtual nodes, node addition and removal, cache sharding, load distribution, hotspot prevention
The Problem with Simple Hashing
When you shard data across N servers using hash(key) % N, adding or removing a server remaps nearly all keys. This causes a thundering herd on your databases.
Consistent hashing limits remapping to only the affected portion of keys.
How Consistent Hashing Works
- Imagine a ring from 0 to 2ยณยฒ
- Hash each server to a position on the ring
- For each key, hash it to a position and walk clockwise to find the first server
- When a server is added, it only takes keys from its clockwise neighbor
- When a server is removed, its keys move to the next clockwise server
// Simplified consistent hash lookup
function getServer(key, ring) {
const keyHash = hash(key);
for (const [position, server] of ring) {
if (keyHash <= position) return server;
}
return ring.first().server; // wrap around
}Virtual Nodes
Each physical server maps to multiple ring positions (virtual nodes). This distributes load more evenly and avoids hot spots when servers have unequal capacity. Production systems typically use 100โ200 virtual nodes per physical server.
