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

What is Range-Based Sharding?

Understand range-based sharding, how contiguous key ranges map to shards, and why it risks hot shards under skewed traffic.

mediumQ80 of 228 in Database Est. time: 5 minsLast updated:
Open Code Lab
80 / 228

Expected Interview Answer

Range-based sharding assigns rows to shards based on contiguous ranges of the shard key's value, so each shard owns a specific, ordered slice such as user IDs 1-100000, making range queries fast but risking uneven load when data or traffic clusters in one range.

A router or metadata service maintains the mapping of key ranges to shard servers, so a query for a specific key or a range of keys can be routed directly to the shard (or small set of shards) that owns that range, without touching the others. This makes range scans, like fetching all orders from a given month, efficient because the results are typically co-located on one shard. The downside is that if the shard key correlates with insertion order or popularity, like an auto-incrementing ID or a trending date range, one shard absorbs a disproportionate share of new writes or queries while others sit comparatively idle.

  • Efficient range queries since data stays contiguous
  • Simple to reason about which shard owns which values
  • Easy to add new ranges as data grows
  • Good fit for time-series or naturally ordered data

AI Mentor Explanation

A cricket board filing player records by jersey-number range, numbers 1-500 in cabinet A and 501-1000 in cabinet B, makes it fast to pull every player in a specific range, since they sit in one contiguous drawer. But if new signings are always issued the next sequential number, cabinet B fills up rapidly while cabinet A stays untouched. Range-based sharding works the same way: it keeps a range's rows together for fast scans, but new, sequential IDs pile onto the newest range's shard.

Step-by-Step Explanation

  1. Step 1

    Define range boundaries

    Split the shard key's domain into contiguous ranges, each assigned to one shard.

  2. Step 2

    Maintain a range map

    A router or metadata service tracks which range maps to which shard server.

  3. Step 3

    Route by range lookup

    Incoming queries look up the shard key in the range map to find the owning shard.

  4. Step 4

    Split hot ranges as needed

    When a range grows too large or too busy, split it into smaller ranges across more shards.

What Interviewer Expects

  • Clear explanation of contiguous range-to-shard mapping
  • Understanding of the range-query performance benefit
  • Awareness of hot-shard risk when ranges correlate with activity
  • Knowledge of range splitting as a mitigation

Common Mistakes

  • Not mentioning the hot-shard risk with sequential or time-based keys
  • Confusing range-based sharding with hash-based sharding
  • Ignoring the need for a range map or routing layer
  • Assuming ranges never need to be split as data grows

Best Answer (HR Friendly)

โ€œRange-based sharding puts contiguous chunks of data, like user IDs 1 through 100000, on the same shard, which makes range queries very fast. The catch is that if new data always falls into the same range, like the latest IDs or the current date, that one shard gets far more traffic than the others.โ€

Code Example

Conceptual range-to-shard mapping
-- Range map maintained by a routing layer
-- user_id 1      - 100000  -> shard_1
-- user_id 100001 - 200000  -> shard_2
-- user_id 200001 - 300000  -> shard_3

function getShardForUser(userId) {
  if (userId <= 100000) return 'shard_1';
  if (userId <= 200000) return 'shard_2';
  return 'shard_3';
}

SELECT * FROM Users WHERE user_id BETWEEN 100001 AND 100050;
-- entire range query resolves to shard_2 only

Follow-up Questions

  • How do you split a range that has grown too large?
  • When would range-based sharding be a poor fit compared to hash-based sharding?
  • How does a routing layer stay updated as ranges are split?
  • How do you handle a query that spans two adjacent ranges?

MCQ Practice

1. What is the main advantage of range-based sharding?

Because rows in a range live on the same shard, queries over that range only need to touch one server.

2. What is the primary risk of range-based sharding with a sequential key?

New sequential values fall into the newest range, so that range's shard receives a disproportionate share of writes.

3. What component tracks which key range belongs to which shard?

A router or metadata service maintains the range-to-shard mapping so queries can be directed correctly.

Flash Cards

What is range-based sharding? โ€” Assigning contiguous ranges of a shard key's values to specific shards.

Main benefit of range-based sharding? โ€” Efficient range queries, since matching rows stay on one shard.

Main risk of range-based sharding? โ€” Hot shards when new or popular data concentrates in one range.

How do you fix an overloaded range? โ€” Split it into smaller sub-ranges distributed across additional shards.

1 / 4

Continue Learning