How Database Sharding Works

Advanced
8 min read· Backend & Databases

Sharding splits one large dataset across multiple database servers, each holding a subset of the rows. When a single machine can no longer handle the data volume or write throughput, sharding scales the database horizontally. A shard key decides which server owns each row. The payoff is near-unlimited scale; the cost is complexity — cross-shard queries, transactions, and rebalancing all become harder, so sharding is a last resort after simpler options.

Think of sharding as splitting a phone book by surname

One giant phone book is impossible to carry, so you split it into volumes: A–F, G–M, N–Z. Each volume (shard) lives in a different room (server). To find someone you jump straight to the right volume — fast. But listing everyone alphabetically now means visiting every room and merging the results (a cross-shard query), and if one volume gets too fat you must re-split the letters (rebalancing).

Step by Step

1 / 5

Key Concepts

Shard Key

The column whose value decides which shard stores a row. A good shard key distributes data and load evenly and appears in most queries so they hit a single shard.

Range vs Hash Sharding

Range sharding keeps ordered ranges together (good for range scans, risks hotspots). Hash sharding scatters keys evenly (great distribution, but range queries must hit all shards).

Hotspot

A single shard receiving disproportionate traffic — often from a poor shard key (like sharding by date so all new writes hit one shard). Even distribution is the goal.

Sharding vs Partitioning

Partitioning splits a table within one database server; sharding splits data across multiple servers. Sharding scales beyond one machine but adds cross-node complexity.

Key Facts

  • Sharding is a last resort — first try read replicas, caching, better indexes, and vertical scaling, which are far simpler.
  • Cross-shard transactions usually require patterns like the saga instead of a single ACID transaction, because a distributed commit is slow and fragile.
  • The wrong shard key is painful to change later, since it means re-sharding the entire dataset — choose it carefully up front.

Real-World Applications

Multi-tenant SaaS at scale

Sharding by tenant_id keeps each customer data together on one shard, so per-tenant queries stay fast and one huge tenant can even get a dedicated shard.

A global social platform

Sharding by user_id with hash distribution spreads billions of users evenly across servers, so each user timeline lives on one shard and no single machine is a bottleneck.

Frequently Asked Questions

What is a shard key and how do I choose one?

The shard key is the value that decides which shard stores each row. Choose one that spreads data and traffic evenly (to avoid hotspots) and appears in most of your queries (so they hit a single shard). Common choices are user_id or tenant_id; avoid keys like timestamps that funnel all new writes to one shard.

What is the difference between sharding and partitioning?

Partitioning divides a table into pieces within a single database server. Sharding distributes data across multiple servers. Sharding scales beyond one machine capacity but introduces cross-shard queries, transactions, and rebalancing complexity that partitioning does not.

Why are cross-shard queries slow?

A query that does not filter by the shard key cannot target one server, so it must fan out to every shard, run there, and merge the results. Joins and transactions spanning shards are especially costly, which is why you design the shard key around your common access patterns.

When should I shard my database?

Only after simpler options are exhausted — read replicas, caching, query and index tuning, and vertical scaling. Shard when a single server genuinely cannot hold the data or sustain the write throughput, and you accept the added operational complexity.

Related Topics