Sharding

Splitting one dataset across separate machines, and the queries that stop being possible once you do.

Sharding

What each access pattern costs

query.sql SQLite
Result

Worth knowing

Partitioning splits a table across files on one server. Sharding splits it across servers, each with its own copy of the engine.
A shard key decides which server holds a row. Every query that names it goes to one node; every query that does not fans out.
Joins across shards, global unique constraints and cross-shard transactions all become hard or impossible.
Shard last. Indexes, read replicas, caching and partitioning are all cheaper and reversible; sharding is neither.

Overview

A shard is one part of a larger dataset stored on a separate node or cluster member. A sharding rule maps each record to a shard, commonly by range or hash of a shard key. An application or routing layer uses that key to locate the record.

In this guide
  1. What a shard is
  2. The shard key shapes the system
  3. Sharding versus partitioning
  4. A tenant-key example
  5. Choosing a key when queries differ
  6. Plan resharding before launch
  7. Further reading

What a shard is

Sharding can add capacity when one server no longer meets storage or throughput needs. It also adds coordination, operations, and failure modes. Scaling a single database, archiving old data, or adding indexes may be simpler; measure the bottleneck before sharding.

The shard key shapes the system

A good shard key distributes writes and storage while appearing in common queries. A key with only a few values or one extremely popular value can create a hot shard. Queries that omit the key may need to contact many shards, and cross-shard transactions or joins can be more expensive.

Changing the shard key or adding nodes may require data movement and rebalancing. Design how routing metadata changes, how resharding is observed, and what happens when a shard is unavailable.

Sharding versus partitioning

Partitioning divides a logical table into pieces managed within a database system. Sharding distributes data across separate nodes and therefore requires routing and coordination. Some products combine both ideas. See partitioning.

A tenant-key example

Sharding by tenant ID can keep each tenant's requests on one shard, making tenant-scoped reads and transactions local. But a few unusually large tenants can create hot shards, and an administrative query across all tenants must fan out. Hashing tenant IDs may balance count but not workload size. Range-based placement can make range scans easier but may concentrate new writes. Track per-shard storage, request rate, and latency, and plan for a tenant that outgrows one shard.

Choosing a key when queries differ

There is rarely one key that makes every access local. A tenant-scoped request may fit a tenant key, while a global time-range report can touch all shards. Record the fraction of requests that include the proposed key and estimate the fan-out for the rest. Also check whether a single tenant can exceed one shard's capacity. If both tenant isolation and global reporting matter, the architecture may need a separate analytical copy rather than forcing one shard key to serve incompatible workloads.

Plan resharding before launch

A shard map must support moving data when a node is added, removed, or overloaded. Define how routing changes without losing writes, how readers handle a moving range, and how progress is monitored. Consistent hashing or range splitting can help, but do not remove the need for a migration protocol and rollback plan. Backups and restores should be tested at both shard and whole-dataset levels. Operational complexity is part of the cost of sharding, even when the key distribution looks ideal on paper.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. What is the difference between partitioning and sharding?

  2. What happens to a query that does not name the shard key?

  3. Why do virtual buckets make rebalancing easier?

Cheat sheet

Sharding

Shard a table by a key, route a query to the right partition, and see the four capabilities you lose when data lives on separate machines.

DATABASE · vizlearn.in/database/sharding_in_databases.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.