- Hash the user ID.
- Divide by 4 (the number of shards).
- Use the remainder as the shard number.
How consistent hashing works
The core idea:- Consider the full range of hash values produced by the hash function.
- Lay these hash values out in order, then bend that line into a circle so the maximum wraps back to the minimum — this is the hash ring.
- Place shard “points” on the ring (each point identifies a shard).
- To route a user: hash the user ID, locate that position on the ring, then move clockwise until you hit the first shard point — that shard owns the user.

The imbalance problem with single positions
If each physical shard is represented only once on the ring, some shards can end up owning very large contiguous sections while others own small ones. That leads to uneven load and storage distribution. If a heavily-loaded shard fails, its contiguous section transfers to a single neighbor — which can overload that neighbor.
Virtual nodes (vnodes): even distribution and resilience
The standard mitigation is to place multiple logical positions for each physical shard on the ring — commonly called virtual nodes or vnodes. For example, shard 0 might appear at 10 different points on the ring, shard 1 at 10 different points, and so on. Important: these are not multiple machines — they are multiple logical positions that map back to the same physical shard. By spreading a shard’s positions around the ring, each physical shard ends up owning many small sections instead of one large contiguous section. This evens out the distribution of keys and reduces hotspots. When a shard fails, its many small sections are redistributed across several other shards instead of being absorbed entirely by one neighbor. When adding a new shard, the new shard receives many small sections taken from many different existing shards, which smooths the transition.
Virtual nodes are a practical and widely used technique to produce an even key distribution and to reduce the impact of node churn. The number of virtual nodes per physical shard is tunable — increasing it typically reduces variance but raises metadata and lookup overhead.
Choosing too few virtual nodes can leave you with high variance; choosing too many increases lookup cost and memory for metadata. Benchmark different vnode counts with your workload and monitor the variance in shard size and request load.
Comparison: modulo sharding vs consistent hashing
When to use consistent hashing
- You expect to add or remove shards frequently (autoscaling, cloud instances).
- You need low data movement and minimal rebalancing during topology changes.
- You require better failure isolation and even load distribution — especially when combined with virtual nodes.
Summary
- Modulo-based sharding ties routing to the number of shards and causes massive reshuffles when that number changes.
- Consistent hashing removes the shard count from the routing decision, so adding or removing a shard only affects keys in a small portion of the ring.
- Virtual nodes per physical shard even out load, reduce hotspots, and make node churn less disruptive.
- Consistent hashing — Wikipedia
- Dynamo paper (Amazon): Dynamo: Amazon’s Highly Available Key-value Store — presents consistent hashing and vnodes in a distributed storage context.