Quick revision
The short answer
Range sharding splits keys into ranges (ids or dates); it is simple and supports range queries on the key, but new data tends to land on one shard and create a hot spot. Hash sharding spreads keys evenly, but range queries must visit every shard and adding shards moves data unless we use consistent hashing or many virtual buckets. Directory sharding keeps a lookup table from key (such as tenant) to shard, which allows flexible placement and moving single tenants, but the directory becomes a critical dependency that must be cached and highly available.
What to remember
In this answer, we compared the three sharding strategies. Range sharding is simple and keeps range queries cheap but tends to create a hot spot on the newest shard. Hash sharding spreads load evenly but sends range queries to every shard and moves data on growth unless we use consistent hashing or many buckets. Directory sharding gives flexible placement and single-tenant moves but makes the directory a critical, cached, highly available dependency. In every case queries must be routed by the application or a proxy, and sharding should come only after vertical scaling, replicas and partitioning.
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.
Range sharding splits keys into ranges (ids or dates); it is simple and supports range queries on the key, but new data tends to land on one shard and create a hot spot. Hash sharding spreads keys evenly, but range queries must visit every shard and adding shards moves data unless we use consistent hashing or many virtual buckets. Directory sharding keeps a lookup table from key (such as tenant) to shard, which allows flexible placement and moving single tenants, but the directory becomes a critical dependency that must be cached and highly available.
Why this question is asked
Sharding means splitting one logical database across several physical databases, each holding part of the rows. Once we decide to shard, the most important design choice is how a row is assigned to a shard, because that choice decides which queries stay cheap, where load concentrates and how painful growth will be. Interviewers ask this comparison to see whether we understand the trade-off behind each strategy rather than just their names. In this answer, we will look at each of the three strategies with an example, compare them side by side, see how queries get routed, and finish with why sharding should not be the first tool we reach for.
1A Quick Recap: Shards and the Shard Key
A shard is one database server, or one database, holding a subset of rows. The shard key is the column whose value decides which shard a row lives on, such as user_id or tenant_id. Every query that includes the shard key can go to exactly one shard. Every query that does not must ask all of them. So the strategy is really a function from shard key to shard, and the three strategies are three kinds of function.
2Range Sharding
With range sharding, each shard owns a continuous range of key values. Shard 1 holds user ids 1 to 10 million, shard 2 holds 10 million to 20 million, and so on. Or, by date, one shard per year. Routing is a simple comparison, and a range query on the key, such as 'orders between id 5,000,000 and 5,001,000' or 'events in March', goes to one shard or a few neighbours.
The risk is the hot spot. Keys that grow over time, such as auto-increment ids or timestamps, send every new row to the last range. The newest shard takes all the inserts, and often most of the reads too because recent data is popular, while older shards sit mostly idle. Ranges can also become unbalanced as some ranges grow faster than others, so they need splitting over time.
3Hash Sharding
With hash sharding, we compute a hash of the key and use it to pick the shard, for example shard = hash(user_id) mod 8. A good hash function spreads keys evenly, so user 1,000,001 and user 1,000,002 land on unrelated shards and the write load is shared fairly.
The cost is that order is destroyed. A range query on the key, such as 'users created this week' by id range, has no idea where those users live, so it must ask every shard. And growth is painful with plain modulo: changing from 8 to 9 shards changes the result of hash mod N for most keys, so most data would move. The standard answers are consistent hashing, where adding a node moves only about 1/N of keys, or pre-splitting into many fixed logical buckets, say 4,096, mapped to physical shards, so growth moves whole buckets.
4Directory Sharding
With directory sharding, also called lookup sharding, a table records where each key lives: tenant 17 is on shard 3, tenant 18 on shard 1, the very large tenant 900 alone on shard 12. Routing means looking up the key in the directory. This gives the most flexibility: we can place keys to balance load, give a big customer a dedicated shard, keep EU tenants on EU shards, and move a single tenant by copying its data and updating one directory row.
The risk is the directory itself. Every request needs it, so if it is slow, everything is slow, and if it is unavailable, nothing can be routed. It must be highly available, usually a small replicated database, and services must cache it, since the mapping changes rarely. The cache brings its own problem: during a tenant move, a service with a stale entry might write to the old shard, so moves need a short write freeze or a version check.
5Comparing the Three
Seen side by side, each strategy is good at one thing and pays for it somewhere else. In all cases, someone must route each query to the right shard: either the application, through a routing library or a Spring routing DataSource, or a proxy layer such as Vitess for MySQL or Citus for PostgreSQL that hides the shards behind one endpoint.
| Strategy | Advantage | Risk |
|---|---|---|
| Range (id or date ranges) | Simple; range queries on the key hit few shards | New data lands on the last shard: hot spot |
| Hash (hash of key) | Even spread of reads and writes | Range queries hit all shards; resharding moves data |
| Directory (lookup table) | Flexible placement; move one tenant at a time | Directory must be cached and highly available |
6Shard Last, Not First
Sharding makes everything harder: cross-shard queries, transactions across shards, schema migrations on every shard, backups, monitoring and on-call. So before sharding we exhaust the simpler options. Vertical scaling, a bigger server, goes surprisingly far today: a single PostgreSQL or MySQL server with 64 cores, 512 GB of memory and fast NVMe disks handles a lot of traffic. Read replicas take read load off the primary. Partitioning within one database solves data lifecycle and maintenance. Caching takes the hottest reads away. Only when writes or data size exceed what one primary can handle, with margin for growth, does sharding pay for its complexity.
7Follow-up questions
Where would you keep the directory and how would services cache it?
I would keep it in a small, highly available relational database, separate from the shards, for example a managed PostgreSQL with a standby, holding rows like (tenant_id, shard_id, version). Services load it into a local cache such as Caffeine at startup and refresh it on a short interval or on a change event published when a tenant moves. Because the mapping changes rarely, the hit rate is nearly 100 percent and the directory sees very little load. To be safe during moves, the shard can store which tenants it owns and reject writes for tenants it no longer owns, so a stale cache entry produces a retryable error instead of a wrong write.
Can we combine strategies?
Yes, and it is common. Many systems hash keys into a fixed number of logical buckets and then use a directory to map buckets to physical shards. That keeps the even spread of hashing and the flexible placement of a directory, while the directory stays small because it maps buckets, not individual keys.
Which would you choose for a multi-tenant SaaS?
Usually directory sharding by tenant_id, because tenants differ hugely in size, the largest ones may need dedicated shards, and moving one tenant is a routine operation. Almost all queries already include the tenant, so they route to one shard.
8Conclusion
In this answer, we compared the three sharding strategies. Range sharding is simple and keeps range queries cheap but tends to create a hot spot on the newest shard. Hash sharding spreads load evenly but sends range queries to every shard and moves data on growth unless we use consistent hashing or many buckets. Directory sharding gives flexible placement and single-tenant moves but makes the directory a critical, cached, highly available dependency. In every case queries must be routed by the application or a proxy, and sharding should come only after vertical scaling, replicas and partitioning.
Open with
Range sharding assigns continuous ranges of the key to shards, like id ranges or one shard per year. It is simple to route and range queries on the key touch only one or a few shards.
Then, if they want more
The risk is hot spots: with growing keys like ids or timestamps, all new inserts land on the last shard while the others sit idle. Hash sharding applies a hash to the key and picks a shard from it.
That spreads reads and writes evenly, which is its big advantage, but range queries on the key now have to go to every shard, and with plain hash mod N, adding a shard moves most of the data, so we use consistent hashing or a fixed set of many logical buckets mapped to shards.
Directory sharding keeps a lookup table from key to shard, typically tenant to shard. It is the most flexible: we can put a huge tenant on its own shard, keep EU tenants in the EU, and move one tenant by copying it and changing one row.
The risk is that the directory is on the path of every request, so it has to be highly available and cached in each service, with care during moves. In all three, either the application or a proxy like Vitess or Citus has to route each query.
And I would add that sharding is a last step: I would first scale vertically, add read replicas, partition and cache.
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 "Compare range-based, hash-based and directory (lookup) sharding. Give one advantage and...": https://scrivio-interview-library.netlify.app/databases/partitioning-and-sharding/8da5875a5c/