Skip to main content
AllDevToolsHub
Back to Glossary

Sharding

A database architecture pattern that involves breaking a very large database into smaller, faster, more easily managed parts called data shards.

Detailed Explanation

Sharding is a form of horizontal scaling. Instead of one massive database, you might put users A-M in Shard 1 and N-Z in Shard 2. This distributes the load across multiple servers. It is extremely complex to implement, especially for joins across shards, but it is necessary for platforms operating at the scale of Facebook or Google.

Quick Summary

Sharding splits one logical database across many physical servers, partitioning rows by a key. It unlocks horizontal scale , and it adds enough operational complexity that most teams should put it off as long as possible.

Key Takeaways

Key Takeaways

  • Pick a shard key carefully , hot shards from a bad key are extremely painful to fix later.
  • Cross-shard joins, transactions, and analytics become hard; design queries around the shard key.
  • Resharding (changing the key or number of shards) is one of the most expensive database operations there is.
  • Many "need sharding" cases turn out to be "need a better index" or "need read replicas" once profiled.
  • Modern alternatives , Vitess, Citus, CockroachDB, Spanner , handle sharding semi-automatically.
Use Cases

When to use it

  • Workloads exceeding what a single beefy machine can handle (hundreds of TB, millions of QPS).
  • Multi-tenant SaaS where each tenant fits on a single shard naturally.
  • Geographic partitioning to keep data near users (and within data-residency boundaries).
  • Time-series workloads sharded by time range with old shards archived cold.
Watch out

Common Mistakes

  • Sharding too early , vertical scaling and read replicas often last far longer than expected.
  • Choosing a low-cardinality shard key, creating a few enormous shards instead of many balanced ones.
  • Forgetting that backups, schema migrations, and monitoring multiply with shard count.
  • Building cross-shard joins in application code , easy to start, painful to operate.
FAQ

Sharding, Frequently Asked

How do I know when I need sharding?

When a single primary, with healthy indexes and a couple of read replicas, can no longer keep up with writes. If writes are the bottleneck and you've already optimized , that's when. Read-heavy workloads usually scale via replicas, not shards.

Sharding vs. partitioning?

Partitioning splits one table across storage on a single server (still one database). Sharding splits across multiple servers (separate databases). Partitioning is much simpler operationally and often enough.