hash(user_id) % 4. Lina has a dedicated shard for her data outside this rule. Now we want to add one more shard to serve normal users—but changing the divisor from 4 to 5 causes extensive data movement.
Why? Because the shard count is baked into the hashing rule. When that number changes, almost every key is likely to remap to a different shard. This process of relocating user data when the shard topology changes is called resharding.
Example: with 20 users and modulo-4 sharding, many user IDs map to the same shard. Switching to modulo-5 will change most assignments; in a simple test about 80% of keys may need to move. That’s a lot of work for a seemingly small topology change.
Why consistent hashing helps
- Make the shard count independent of the basic mapping rule.
- Only keys that fall into the region owned by the added/removed shard must move.
- Add or remove shards with minimal data movement.
- Treat the full hash space like a circle — the hash ring — so the largest and smallest hash values are adjacent.
- Place shard identifiers (or shard tokens) at points on the ring.
- To route a key: compute
hash(key)→ find that point on the ring → move clockwise until you hit the first shard token; that shard owns the key.

- Place the new shard’s token(s) somewhere on the ring (for instance, between shard 1 and shard 2).
- Only the keys whose hash falls in the arc between shard 1 and the new shard will move to the new shard.
- Keys that map to other arcs (for example, Chris’s or Leo’s) remain unchanged if their first clockwise shard didn’t change.

- The shard’s ranges are scattered across the ring rather than concentrated in a single arc, smoothing load.
- When a shard fails or is added, many small ranges are redistributed, spreading the transfer cost across multiple shards.

Virtual nodes are logical positions on the hash ring that belong to a physical shard. They improve balance and reduce the impact of adding/removing physical shards by spreading ownership of hash ranges across the ring.
Choosing the number of virtual nodes is a tuning problem. Too few vnodes -> imbalance; too many -> larger routing tables and more memory overhead. Benchmark and monitor shard load when tuning vnode counts.
Summary
- Modulo-based sharding ties the shard count to the routing rule, causing extensive resharding when the count changes.
- Consistent hashing maps keys onto a ring and requires moving only keys in the region affected by an added or removed shard.
- Use virtual nodes to smooth distribution and reduce hotspots; tune the number of vnodes based on traffic and operational constraints.
- Consistent hashing — Wikipedia
- Amazon Dynamo paper — highly available key-value storage (introduces vnodes concept)
- Martin Fowler — Database Sharding