Quick revision
The short answer
With shard = hash(key) % N, changing N to N + 1 changes the result of the modulo for almost every key, so roughly N/(N+1) of all data has to move, for example about 89 percent when going from 8 to 9 shards. Consistent hashing moves only about 1/N of the keys when a node is added. Pre-splitting into many fixed logical buckets (for example 4,096) mapped to shards means growth moves whole buckets without rehashing, and directory sharding lets us move single tenants or ranges. The lesson is to plan for resharding from day one.
What to remember
In this answer, we saw that routing with hash(key) % N ties every key's shard to N, so adding one shard to eight moves about eight ninths of the data. Consistent hashing moves only about 1/N of the keys, fixed logical buckets mapped to shards let us move whole buckets without rehashing any key, and a directory lets us move exactly the tenants or ranges we choose. Because retrofitting is so much harder, we design for resharding from day one, with buckets and a mapping table.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With shard = hash(key) % N, changing N to N + 1 changes the result of the modulo for almost every key, so roughly N/(N+1) of all data has to move, for example about 89 percent when going from 8 to 9 shards. Consistent hashing moves only about 1/N of the keys when a node is added. Pre-splitting into many fixed logical buckets (for example 4,096) mapped to shards means growth moves whole buckets without rehashing, and directory sharding lets us move single tenants or ranges. The lesson is to plan for resharding from day one.
Why this question is asked
hash(key) % N is usually the first sharding scheme a team writes, because it is one line of code and spreads load evenly. The trouble appears the first time the cluster must grow. Interviewers ask this to check that we understand why modulo routing is fragile and know the techniques that real systems use instead. In this answer, we will measure how much data moves with modulo routing, see why, and then look at consistent hashing, fixed logical buckets and directories, which all reduce the movement in different ways.
1A Quick Recap of Modulo Routing
With modulo routing, we hash the key to a large number and take the remainder after dividing by the number of shards. With N = 8, the remainder is 0 to 7, which is the shard number. As long as N stays fixed, this is perfect: even spread, no lookup table, and every service computes the same answer.
2Why Adding a Shard Moves Almost Everything
The remainder depends on N. When N changes from 8 to 9, a key keeps its shard only if hash mod 8 equals hash mod 9, which is true for only about one key in nine. Every other key now belongs to a different shard. So adding one shard to eight does not move one ninth of the data to the new shard, which is what we would want; it moves about eight ninths of the data between almost all pairs of shards.
| Routing scheme | Share of data that moves | Why |
|---|---|---|
| hash % N | About 89 percent (8 of 9) | The remainder changes for most keys |
| Consistent hashing | About 11 percent (1 of 9) | Only keys in the new node's arcs move |
| 4,096 buckets mapped to shards | About 11 percent, as whole buckets | Move 455 buckets; key -> bucket never changes |
| Directory per tenant | Only the tenants we choose | Placement is an explicit choice |
In a real system that means copying most of the data while the application is running, keeping copies in sync during the move, and switching routing for every key at once, since the new formula applies to all keys at the same moment. Doubling N, from 8 to 16, is a little kinder: each key either stays or moves to exactly one partner shard, but still half of all data moves. That is why modulo routing is expensive to grow.
3Technique 1: Consistent Hashing
Consistent hashing places both nodes and keys on a ring of hash values, for example 0 to 2^64 - 1. A key belongs to the first node clockwise from its position. When we add a node, it takes over only the keys between itself and the previous node on the ring, so only about 1/N of the keys move, and they all move to the new node. The rest stay where they are.
With one point per node, the arcs are uneven and some nodes get much more than their share, so in practice each node is placed at many points, called virtual nodes, such as 100 to 200 per node. That evens out the load and spreads the keys a new node takes over across all existing nodes. Consistent hashing is common in caches and in databases such as Cassandra.
4Technique 2: Many Fixed Logical Buckets
The second technique separates two mappings. First, every key maps to one of a large, fixed number of logical buckets, such as bucket = hash(key) % 4096. This number never changes. Second, a small table maps buckets to physical shards: buckets 0 to 511 on shard 0, 512 to 1023 on shard 1, and so on for 8 shards. Adding a shard means choosing some buckets, roughly 4096/9, about 455, copying their data to the new shard and updating their rows in the mapping table. No key is ever rehashed, because a key's bucket never changes.
Notice that the unit of movement is a whole bucket, which makes the move easy to reason about: we can copy bucket 1337, verify it, switch it and clean it up, then do the next one. The number of buckets must be chosen much larger than the number of shards we will ever have, so 4,096 or 16,384 is typical. Vitess, Citus, Redis Cluster (with 16,384 hash slots) and many in-house systems use this pattern.
5Technique 3: Directory-Based Sharding
A directory maps each key, or each range, explicitly to a shard: tenant 17 is on shard 3. To add a shard, we decide which tenants to move, perhaps the fastest-growing ones, copy each one, and update its directory row. The amount of data moved is exactly what we choose, and we can move one tenant at a time. The cost is the directory itself, which must be highly available and cached. Many systems combine this with buckets, where the directory maps buckets rather than keys, so it stays small.
6Plan for Resharding From Day One
Retrofitting resharding is much harder than designing for it, because by then every service has hash % N baked in, data is large, and the move must happen under load. Designing for it from the start costs very little: hash keys into thousands of logical buckets even if there is only one shard today, keep the bucket-to-shard mapping in a table rather than in code, and route through one shared library. Then the first growth step is a routine bucket move, not a migration project.
7Follow-up questions
Why not just double N each time, from 8 to 16?
Doubling means each key either stays or moves to one specific partner, so the move is simpler to organise, but half of all data still moves at once, and every growth step doubles hardware whether we need it or not. Buckets let us add one shard at a time and move only what is needed.
How many buckets should we choose?
Enough that each shard holds many buckets even at the largest cluster size we can imagine, so we can balance finely: if we might reach 256 shards, 4,096 buckets give 16 per shard, and 16,384 give 64. Too many buckets only make the mapping table larger, which is still tiny.
Does consistent hashing need a lookup table?
Not a stored one, since every client can build the ring from the list of nodes. But all clients must agree on the node list, so in practice it still comes from shared configuration or a coordination service.
8Conclusion
In this answer, we saw that routing with hash(key) % N ties every key's shard to N, so adding one shard to eight moves about eight ninths of the data. Consistent hashing moves only about 1/N of the keys, fixed logical buckets mapped to shards let us move whole buckets without rehashing any key, and a directory lets us move exactly the tenants or ranges we choose. Because retrofitting is so much harder, we design for resharding from day one, with buckets and a mapping table.
Open with
The problem is that the modulo depends on N. When I change N from 8 to 9, a key keeps its shard only if hash mod 8 equals hash mod 9, which is about one key in nine, so roughly 89 percent of the data has to move, between almost every pair of shards, while the system is live.
Then, if they want more
Even doubling to 16 moves half the data. There are three standard ways to reduce that.
Consistent hashing puts nodes and keys on a hash ring, and each key belongs to the next node clockwise, so a new node only takes the keys in its arcs, about 1/N of the total, and with virtual nodes those come evenly from all existing nodes.
The approach I like most for databases is pre-splitting into many fixed logical buckets, say 4,096: a key maps to a bucket by hash mod 4,096, which never changes, and a small table maps buckets to shards.
Adding a shard means moving a few hundred whole buckets and updating the table, with no rehashing, and each bucket can be copied, verified and switched on its own. Vitess, Citus and Redis Cluster's hash slots work like this. Directory-based sharding goes further and lets us move individual tenants or ranges.
The real lesson is to plan for resharding from the start, because retrofitting it into a running system is much harder.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why is adding a shard to a system that routes with shard = hash(key) % N expensive, and...": https://scrivio-interview-library.netlify.app/databases/partitioning-and-sharding/467596b929/