100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is Hash-Based Sharding?

Learn how hash-based sharding distributes data evenly across shards and why it makes range queries require scanning every shard.

mediumQ81 of 228 in Database Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Hash-based sharding applies a hash function to the shard key and uses the result (typically modulo the shard count) to assign each row to a shard, which spreads data and write load evenly across servers but sacrifices efficient range scans, since logically adjacent keys land on unrelated shards.

Because a good hash function scatters even sequential or skewed input values uniformly across the output space, hash-based sharding avoids the hot-shard problem that range-based sharding faces with monotonically increasing keys. The trade-off is that range queries, like "all orders between two dates," now have to fan out to every shard and merge results, since the hash destroys any notion of adjacency between nearby key values. Hash-based sharding also complicates resharding: adding or removing a shard changes the modulo result for most keys, which is why production systems typically pair it with consistent hashing or a fixed, over-provisioned number of virtual shards to limit data movement.

  • Even distribution of writes and storage across shards
  • Avoids hot shards from sequential or skewed keys
  • Simple, deterministic routing given a stable shard count
  • Predictable load balancing without manual range tuning

AI Mentor Explanation

A league assigning players to training grounds by running each player's ID through a scrambling formula, rather than by jersey-number order, spreads players evenly across grounds no matter how sequentially IDs were issued. But if a coach wants "every player with IDs 100-200," the scrambled assignment means those players are scattered across every ground, requiring a check-in at all of them. Hash-based sharding trades this same convenience: even distribution in exchange for losing any easy range grouping.

Step-by-Step Explanation

  1. Step 1

    Choose a hash function

    Apply a well-distributed hash function to the shard key value.

  2. Step 2

    Compute the shard index

    Take the hash result modulo the number of shards to determine the target shard.

  3. Step 3

    Route the operation

    Direct reads and writes for that key directly to the computed shard.

  4. Step 4

    Plan for resharding

    Use consistent hashing or virtual shards to limit data movement when the shard count changes.

What Interviewer Expects

  • Clear explanation of hash-then-modulo routing
  • Understanding that hashing solves the hot-shard problem of sequential keys
  • Awareness that range queries require scatter-gather across all shards
  • Knowledge that adding/removing shards is disruptive without consistent hashing

Common Mistakes

  • Claiming hash-based sharding preserves range-query efficiency
  • Ignoring the resharding cost when the shard count changes
  • Confusing hash-based sharding with consistent hashing itself
  • Assuming any hash function guarantees even distribution regardless of input skew

Best Answer (HR Friendly)

โ€œHash-based sharding runs each row's key through a hash function and uses the result to decide which shard it lives on, which spreads data very evenly even if the original keys were sequential. The downside is that range queries, like fetching everything from a certain date range, now have to check every shard instead of just one.โ€

Code Example

Hash-based shard routing
function getShardForKey(userId, shardCount) {
  const hashValue = hash(userId); // uniformly distributed
  return hashValue % shardCount;
}

-- Point lookup: routes deterministically to exactly one shard
SELECT * FROM Users WHERE user_id = 55321;

-- Range query: must fan out to every shard and merge results,
-- because the hash destroys adjacency between nearby user_id values
SELECT * FROM Users WHERE created_at BETWEEN '2026-07-01' AND '2026-07-31';

Follow-up Questions

  • What happens to existing data mappings when you add a new shard?
  • How does consistent hashing reduce data movement during resharding?
  • Why do range queries become expensive under hash-based sharding?
  • How would you support both fast point lookups and range queries in a hash-sharded system?

MCQ Practice

1. Hash-based sharding primarily solves what problem?

Hashing scatters even sequential key values uniformly across shards, preventing any one shard from becoming a hot spot.

2. What is a downside of hash-based sharding compared to range-based sharding?

Hashing destroys key adjacency, so a range query can no longer be resolved by touching a single contiguous shard.

3. Why is resharding disruptive in a plain hash-modulo scheme?

With hash % shardCount routing, adding or removing a shard shifts the modulo result for most keys, forcing large-scale data movement.

Flash Cards

What is hash-based sharding? โ€” Using a hash of the shard key, modulo the shard count, to assign each row to a shard.

Main benefit of hash-based sharding? โ€” Even distribution of load, even for sequential or skewed keys.

Main downside of hash-based sharding? โ€” Range queries require fanning out to every shard, since key adjacency is lost.

How is resharding pain reduced? โ€” By using consistent hashing or a fixed set of virtual shards to limit data movement.

1 / 4

Continue Learning