- Compute
user_id % NwhereNis the number of shards. - The remainder determines the shard number.
N = 4):
- Alan (user ID 10):
10 % 4 = 2→ shard 2 - John (user ID 13):
13 % 4 = 1→ shard 1
- Alan (user ID 10):
10 % 5 = 0→ now shard 0 (previously shard 2) - John (user ID 13):
13 % 5 = 3→ now shard 3 (previously shard 1)

- Place both shards (physical nodes) and keys (user IDs) on the same circular hash space (imagine a clock).
- Hash a key to a point on the circle. Walk clockwise until you find the first shard — that shard is responsible for the key.
- When a shard is added or removed, only the keys that map between the previous clockwise neighbor and the new shard need to move.

Consistent hashing drastically reduces data movement during topology changes: adding one node moves only about
1/(N+1) of the keys (those that fall into the new node’s arc). For better uniformity, assign multiple virtual nodes per physical shard to reduce variance in load distribution.
Further reading and references
- Consistent hashing — Wikipedia
- A thorough blog explanation of consistent hashing and virtual nodes (search for up-to-date posts on consistent hashing and distributed hash tables)