AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content
SD notes

System Design · Note 7 of 22

Database Sharding

Intermediate · 7 min read · updated 29 October 2026

Quick answer

What is database sharding?

Sharding splits one large dataset into smaller pieces called shards and stores each on a different database server. A shard key in every row decides which shard holds it, by range or by hash. Sharding scales writes and storage beyond one machine, at the cost of cross-shard queries, resharding work and lost single-database transactions.

On this page
  1. Why shard
  2. Range vs hash sharding
  3. Choosing a shard key
  4. Resharding
  5. Queries across shards
  6. Interview questions
  7. Common questions

Sharding (horizontal partitioning) splits a table's rows across several database servers, so each holds and serves only part of the data.

Why shard

  • One server has a ceiling on storage, writes per second and memory.
  • Replication scales reads, not writes: every replica applies every write.
  • Sharding splits the data, so writes and storage grow with the number of shards.
  • Try first: indexes, caching, read replicas, a bigger machine. Sharding is the last step.

Range vs hash sharding

  • Range: each shard owns a span of keys (A–H, I–P, Q–Z).
  • Range queries stay on one shard; but load follows the key distribution.
  • Hash: shard = hash(key) mod N.
  • Even spread; neighbouring keys scatter, so range queries ask every shard.
  • Directory: a lookup table maps keys to shards — flexible, but the table is one more thing to keep available.
Range vs hash sharding: where the same keys land

Each row has a shard key. Range sharding gives each shard a span of keys; hash sharding hashes the key and takes the result mod the shard count.

0:01 / 0:29Step 1 of 9
Transcript · 9 steps

Choosing a shard key

  • High cardinality: many distinct values to spread.
  • Even traffic, not just even data.
  • In most queries, so they reach one shard.
  • A bad key creates a hot shard: sequential ids, timestamps, one celebrity user.
A hot shard: sequential keys under range sharding all hit one shard

New orders get increasing ids, starting at 2001. Ids up to 2000 are taken, so under range sharding each new id falls in the last range; hash sharding hashes the id first.

0:01 / 0:15Step 1 of 5
Transcript · 5 steps

Resharding

  • Shards fill up or get hot, so data must be split and moved.
  • With hash mod N, going from N to N + 1 changes the shard of most keys.
  • Fixes:
  • Consistent hashing: only about 1/(N+1) of keys move.
  • Many fixed partitions (say 1,024) mapped to servers: move whole partitions, never re-hash.
  • Moving data live needs double writes or a copy-then-switch cutover.
Resharding with mod N: adding one shard moves most keys

Twelve keys placed by hash(key) mod 3. A key's shard is fixed by its hash, so any server can work out where it lives.

0:01 / 0:13Step 1 of 5
Transcript · 5 steps

Queries across shards

  • With the shard key: routed to one shard — cheap at any scale.
  • Without it: scatter–gather to every shard, merged by the router; latency = the slowest shard.
  • Cross-shard joins move to the application; denormalise data that is read together.
  • Cross-shard transactions need two-phase commit or sagas — avoid by keeping related rows on one shard (ACID).
Scatter–gather: a query without the shard key asks every shard

A query that names the shard key can be routed: the router computes FNV-1a("7") mod 3 = 0.

0:01 / 0:19Step 1 of 7
Transcript · 7 steps
RangeHash
Range queriesOne shardEvery shard
Load spreadFollows key distributionEven
Hot spotsSequential or skewed keysOne very hot key only
Adding a shardSplit a rangeMost keys move (mod N)

Interview questions

  • Sharding vs replication? Sharding splits data to scale writes; replication copies it to scale reads and survive failures.
  • How would you shard a users table? By hashed user id: high cardinality, even load, and most queries name the user.
  • Why not shard by created date? Every new row lands on the latest shard — a hot shard.
  • What breaks after sharding? Queries without the key, cross-shard joins and transactions, and resharding.

Next: CAP Theorem · Before this: Database Replication

Common questions

What is the difference between sharding and replication?

Replication copies the same data to several machines, which adds read capacity and availability. Sharding splits different data across machines, which adds write capacity and storage. Real systems usually do both: each shard is itself a leader with followers.

What is the difference between range and hash sharding?

Range sharding gives each shard a contiguous span of keys, so range queries stay on one shard but sequential or popular keys can overload one shard. Hash sharding places a key by its hash, which spreads load evenly but scatters neighbouring keys, so range queries must ask every shard.

How do you choose a shard key?

Pick a key with many distinct values that spreads both data and traffic evenly and that most queries include, so they can be routed to one shard. A user id often works for user-centric apps; a timestamp or a country code usually does not.

What is a hot shard?

A shard that receives far more traffic than the others, from sequential keys under range sharding, a skewed key distribution or one very popular key such as a celebrity account. Fixes include hashing the key, splitting the hot range, or adding a random suffix to a hot key and reading all its parts.

Why is resharding hard with hash mod N?

The shard is hash(key) mod N, so changing N changes the answer for most keys, not just the ones the new shard should take. Moving that much data at once is slow and risky. Consistent hashing, or a fixed number of virtual partitions mapped to servers, limits how much moves.

What are the downsides of sharding?

Queries without the shard key must ask every shard, joins and transactions across shards need application logic or distributed protocols, resharding is operationally heavy, and every shard needs its own replicas and backups. It is worth it only once one machine truly cannot keep up.

Database Sharding: Range vs Hash, Hot Shards, Resharding

Database sharding for system design interviews: range vs hash sharding, choosing a shard key, hot shards, resharding and scatter-gather queries, animated.

  • Subject: System Design
  • Level: Intermediate
  • Reading time: 7 min
  • Updated: 2026-10-29

What is database sharding?

Sharding splits one large dataset into smaller pieces called shards and stores each on a different database server. A shard key in every row decides which shard holds it, by range or by hash. Sharding scales writes and storage beyond one machine, at the cost of cross-shard queries, resharding work and lost single-database transactions.

Sharding (horizontal partitioning) splits a table's rows across several database servers, so each holds and serves only part of the data.

Why shard

  • One server has a ceiling on storage, writes per second and memory.
  • Replication scales reads, not writes: every replica applies every write.
  • Sharding splits the data, so writes and storage grow with the number of shards.
  • Try first: indexes, caching, read replicas, a bigger machine. Sharding is the last step.

Range vs hash sharding

  • Range: each shard owns a span of keys (A–H, I–P, Q–Z).
  • Range queries stay on one shard; but load follows the key distribution.
  • Hash: shard = hash(key) mod N.
  • Even spread; neighbouring keys scatter, so range queries ask every shard.
  • Directory: a lookup table maps keys to shards — flexible, but the table is one more thing to keep available.
Range: by first letterShard A–HShard I–PShard Q–Zalicechenfarahkiranmayapriyaravizoerows per shard: 3 / 3 / 2Hash: FNV-1a(key) mod 3Shard 0Shard 1Shard 2alicechenfarahkiranmayapriyaravizoerows per shard: 3 / 3 / 2
Range vs hash sharding: where the same keys land.
  1. Each row has a shard key. Range sharding gives each shard a span of keys; hash sharding hashes the key and takes the result mod the shard count.
  2. "alice" starts with a, so range sharding stores it on A–H; FNV-1a("alice") mod 3 = 2, so hash sharding stores it on shard 2.
  3. "chen" starts with c, so range sharding stores it on A–H; FNV-1a("chen") mod 3 = 0, so hash sharding stores it on shard 0.
  4. "farah" starts with f, so range sharding stores it on A–H; FNV-1a("farah") mod 3 = 0, so hash sharding stores it on shard 0.
  5. "kiran" starts with k, so range sharding stores it on I–P; FNV-1a("kiran") mod 3 = 1, so hash sharding stores it on shard 1.
  6. "maya" starts with m, so range sharding stores it on I–P; FNV-1a("maya") mod 3 = 2, so hash sharding stores it on shard 2.
  7. "priya" starts with p, so range sharding stores it on I–P; FNV-1a("priya") mod 3 = 0, so hash sharding stores it on shard 0.
  8. "ravi" starts with r, so range sharding stores it on Q–Z; FNV-1a("ravi") mod 3 = 1, so hash sharding stores it on shard 1.
  9. "zoe" starts with z, so range sharding stores it on Q–Z; FNV-1a("zoe") mod 3 = 1, so hash sharding stores it on shard 1. Range keeps neighbours together (3/3/2), so "names a to d" reads one shard; hash scatters them (3/3/2).

Choosing a shard key

  • High cardinality: many distinct values to spread.
  • Even traffic, not just even data.
  • In most queries, so they reach one shard.
  • A bad key creates a hot shard: sequential ids, timestamps, one celebrity user.
Range by order id00121–10001001–20002001+Hash of order id, mod 3444shard 0shard 1shard 2
A hot shard: sequential keys under range sharding all hit one shard.
  1. New orders get increasing ids, starting at 2001. Ids up to 2000 are taken, so under range sharding each new id falls in the last range; hash sharding hashes the id first.
  2. Orders 2001–2003 arrive. Range sends all 3 to the 2001+ shard; hashing sends them to shards 1, 1, 2.
  3. Orders 2004–2006 arrive. Range sends all 3 to the 2001+ shard; hashing sends them to shards 1, 2, 2.
  4. Orders 2007–2009 arrive. Range sends all 3 to the 2001+ shard; hashing sends them to shards 0, 2, 0.
  5. After 12 orders, range sharding has put 12 of them on one shard: a hot shard, while the other two get no new writes. Hashing spread them 4/4/4.

Resharding

  • Shards fill up or get hot, so data must be split and moved.
  • With hash mod N, going from N to N + 1 changes the shard of most keys.
  • Fixes:
  • Consistent hashing: only about 1/(N+1) of keys move.
  • Many fixed partitions (say 1,024) mapped to servers: move whole partitions, never re-hash.
  • Moving data live needs double writes or a copy-then-switch cutover.
Shard 03 keysShard 14 keysShard 22 keysShard 33 keysuser1user2user3user4user5user6user7user8user9user10user11user12
Resharding with mod N: adding one shard moves most keys.
  1. Twelve keys placed by hash(key) mod 3. A key's shard is fixed by its hash, so any server can work out where it lives.
  2. A fourth shard is added, so the rule becomes hash mod 4. For 10 of the 12 keys (marked) the answer changes.
  3. The marked keys must be copied to their new shards. Most of them move between old shards, not into the new one.
  4. All 10 keys have moved. During the move, reads must know whether a key has been copied yet.
  5. 10 of 12 keys moved, yet the new shard only needed 3. Consistent hashing moves close to that minimum.

Queries across shards

  • With the shard key: routed to one shard — cheap at any scale.
  • Without it: scatter–gather to every shard, merged by the router; latency = the slowest shard.
  • Cross-shard joins move to the application; denormalise data that is read together.
  • Cross-shard transactions need two-phase commit or sagas — avoid by keeping related rows on one shard (ACID).
RouterShard 04 rowsShard 16 rowsShard 25 rowsORDER BY score DESC LIMIT 3817059967448853322968581
Scatter–gather: a query without the shard key asks every shard.
  1. A query that names the shard key can be routed: the router computes FNV-1a("7") mod 3 = 0.
  2. Only shard 0 is asked, and it answers with user 7's row. A point lookup costs the same however many shards there are.
  3. Now "the top 3 scores overall". The query has no shard key, so the router cannot tell which shards hold the answer.
  4. It scatters the query to all 3 shards. Every shard does work for every such query, so adding shards does not make it cheaper.
  5. Each shard computes its own top 3: 81, 70, 59 | 96, 74, 48 | 85, 33, 22.
  6. The local answers travel back. The router must wait for the slowest shard, so one slow shard slows every scatter query.
  7. The router merges the 9 candidates into the true top 3: 96, 85, 81. Choose a shard key that most queries include.
RangeHash
Range queriesOne shardEvery shard
Load spreadFollows key distributionEven
Hot spotsSequential or skewed keysOne very hot key only
Adding a shardSplit a rangeMost keys move (mod N)

Interview questions

  • Sharding vs replication? Sharding splits data to scale writes; replication copies it to scale reads and survive failures.
  • How would you shard a users table? By hashed user id: high cardinality, even load, and most queries name the user.
  • Why not shard by created date? Every new row lands on the latest shard — a hot shard.
  • What breaks after sharding? Queries without the key, cross-shard joins and transactions, and resharding.

Next: CAP Theorem · Before this: Database Replication

Common questions

What is the difference between sharding and replication?

Replication copies the same data to several machines, which adds read capacity and availability. Sharding splits different data across machines, which adds write capacity and storage. Real systems usually do both: each shard is itself a leader with followers.

What is the difference between range and hash sharding?

Range sharding gives each shard a contiguous span of keys, so range queries stay on one shard but sequential or popular keys can overload one shard. Hash sharding places a key by its hash, which spreads load evenly but scatters neighbouring keys, so range queries must ask every shard.

How do you choose a shard key?

Pick a key with many distinct values that spreads both data and traffic evenly and that most queries include, so they can be routed to one shard. A user id often works for user-centric apps; a timestamp or a country code usually does not.

What is a hot shard?

A shard that receives far more traffic than the others, from sequential keys under range sharding, a skewed key distribution or one very popular key such as a celebrity account. Fixes include hashing the key, splitting the hot range, or adding a random suffix to a hot key and reading all its parts.

Why is resharding hard with hash mod N?

The shard is hash(key) mod N, so changing N changes the answer for most keys, not just the ones the new shard should take. Moving that much data at once is slow and risky. Consistent hashing, or a fixed number of virtual partitions mapped to servers, limits how much moves.

What are the downsides of sharding?

Queries without the shard key must ask every shard, joins and transactions across shards need application logic or distributed protocols, resharding is operationally heavy, and every shard needs its own replicas and backups. It is worth it only once one machine truly cannot keep up.

← Database Replication · CAP Theorem →