# Consistent Hashing — System Design

Source: https://www.geekswithgeeks.com/en/system-design/sd-consistent-hashing

> Mapping keys to nodes so scaling doesn't reshuffle everything.

## The rehashing problem

With plain `hash(key) % N` sharding, adding or removing one node changes N and remaps **almost every key** — a massive, unnecessary data shuffle.

## A ring of lockers

Consistent hashing places both nodes and keys on a **ring** (a hash circle). A key belongs to the first node found clockwise from it. Adding a node only steals keys from its immediate neighbor — everyone else is untouched.

## Virtual nodes

Give each physical node many points on the ring (virtual nodes) so load spreads evenly instead of depending on random placement.

```text
ring = {}
for node in nodes:
  for v in range(150):  # virtual replicas
    ring[hash(node + str(v))] = node

def lookup(key):
  h = hash(key)
  return ring[first_point_clockwise_from(h)]
```

Output:

```
More virtual nodes = smoother load distribution
```

## Where it's used

Consistent hashing powers cache clusters (Memcached clients), CDNs, and distributed databases like Cassandra and DynamoDB — anywhere nodes join or leave and you want minimal data movement.
