Skip to main content
As our user base grows, a fixed modulo-based sharding scheme becomes costly to change. Previously we used four shards (shard 0–3) and assigned users by taking 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.
How it works
  1. Treat the full hash space like a circle — the hash ring — so the largest and smallest hash values are adjacent.
  2. Place shard identifiers (or shard tokens) at points on the ring.
  3. 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.
For example, Chris’s hash lands at some point whose first clockwise shard is shard 3, so Chris belongs to shard 3. Leo’s hash lands elsewhere and maps to shard 0.
A dark-themed diagram of a consistent hashing ring with labeled shard nodes (Shard 0, Shard 1, Shard 2, Shard 3 and New Shard). Two user icons (Leo and Chris) with numeric keys are shown mapped by dashed lines to points on the ring.
Adding a shard on the ring
  • 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.
This means a single shard addition/removal causes only a small, local set of keys to move—unlike modulo sharding, where most keys are remapped by altering the divisor.
An infographic comparing sharding strategies: the left side shows the old "hash ÷ number of shards" approach with many keys moved across four shards, while the right side shows a hash ring (consistent hashing) where only one section is moved when a shard changes.
Virtual nodes (vnodes) and load balancing Consistent hashing reduces reshuffling, but it can introduce imbalance: if each physical shard has only one token, some shards may accidentally own much larger contiguous ranges. To mitigate this, assign multiple virtual nodes (vnodes) per physical shard. Each vnode is simply another token on the ring that maps back to the same physical shard. Benefits of vnodes:
  • 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.
The image is a stylized diagram titled "Consistent Hashing" showing a circular hash ring with many colored nodes. A legend on the right maps colors to different shards (Shard 0–3, New Shard, One More).
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.
Quick comparison 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.
Links and further reading

Watch Video