Script Valley
Interview Prep: System Design Rounds
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

  1. Imagine a ring from 0 to 2ยณยฒ
  2. Hash each server to a position on the ring
  3. For each key, hash it to a position and walk clockwise to find the first server
  4. When a server is added, it only takes keys from its clockwise neighbor
  5. 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.

Up next

How DNS and CDN work in large-scale systems

Sign in to track progress

Consistent hashing explained for system design interviews โ€” Scalability and Load Balancing โ€” Interview Prep: System Design Rounds โ€” Script Valley โ€” Script Valley