Skip to content

Partitioning and sharding

Eventually, some databases outgrow a single table or a single server. Queries slow down as tables reach billions of rows, maintenance windows stretch, and the largest available server is still not enough for the write load. Partitioning splits a large table into smaller pieces inside one database. Sharding splits the data across several independent databases.

Both work. Both are hard to undo. And sharding in particular changes the application, not just the database, which is why it’s an architecture decision rather than a tuning exercise.

A sharded database1. Every request carries the shard key, here the tenant, and the router looks up which shard holds it. 2. The request runs entirely on one shard, so each shard stays small and fast. 3. Reports across all tenants run on an analytics store fed by change data capture, not by querying every shard. Applicationpasses the shard key Shard routerkey to shard Shard mapwhich keys live where Shard 1tenants A to H Shard 2tenants I to P Shard 3tenants Q to Z Analytics storefed by CDC123
A sharded database A router sends each request to the one shard that holds its key; questions that span shards go to a separate analytics store.
  1. Every request carries the shard key, here the tenant, and the router looks up which shard holds it.
  2. The request runs entirely on one shard, so each shard stays small and fast.
  3. Reports across all tenants run on an analytics store fed by change data capture, not by querying every shard.
Option What it does Use when Cost
Fix queries and indexes Removes waste Always first Low
Scale up A bigger server Short term, or when growth is modest Money; there’s a ceiling
Caching and read replicas Takes reads off the primary Read-heavy workloads Staleness to manage
Partitioning (one database) Splits a big table by range, list or hash Huge tables, time-based data, easy archiving Queries should include the partition key
Functional split Moves different tables or services to different databases Clear domain boundaries No joins across the split
Sharding Splits the same tables across many databases by key Write load or data size beyond one server Application changes, cross-shard queries, rebalancing
Distributed SQL A database that shards itself You need scale with SQL and strong consistency New technology; latency; licensing

The shard key decides everything. A good one:

  • Appears in almost every query, so each request goes to one shard.
  • Spreads load evenly, with no single key much hotter than the others.
  • Keeps related data together: a tenant’s orders, invoices and users on the same shard so they can still be joined and updated in one transaction.
  • Doesn’t change. Moving a row between shards is expensive.

Common choices are tenant or customer ID in multi-tenant systems, and user ID in consumer apps. Timestamps are usually bad shard keys, because all new writes land on one shard. They make good partition keys inside one database, though, because old partitions can be archived or dropped cheaply.

  • Range, hash or directory. Range sharding (A to H) is simple but can be uneven. Hash sharding spreads load evenly but makes ranges hard. A directory (a lookup table of key to shard) is flexible, and lets you move a big tenant onto its own shard.
  • Many logical shards, fewer servers. Create far more shards than servers (say, 1,024 on 8 servers) so you can rebalance by moving whole shards instead of splitting them.
  • Cross-shard work goes elsewhere. Reports and searches across all shards run on a warehouse, lakehouse or search index fed by change data capture.
  • Avoid cross-shard transactions. If you need them often, the shard key is probably wrong.
  • Automate operations. Schema changes, backups and monitoring now run across every shard; do them with tooling, not by hand.
  • Near-linear scaling of writes and storage by adding shards.
  • Smaller blast radius: a problem on one shard affects only its tenants.
  • Data residency: shards can live in different regions for regulatory reasons.
  • Application complexity: every query needs the key; joins and transactions across shards are hard.
  • Operational load: many databases to patch, back up, monitor and change.
  • Rebalancing hot shards is difficult and risky without good tooling.

When write volume or data size truly exceeds what a well-tuned single primary can handle, and a natural key (such as tenant) exists.

Most systems never need sharding. Exhaust indexing, partitioning, caching, read replicas and functional splits first. Sharding too early adds permanent complexity for a problem you may never have.

  • A shard key that’s missing from common queries, forcing scatter-gather across every shard.
  • One giant tenant that overwhelms its shard.
  • Auto-increment IDs that collide across shards; use globally unique IDs.
  • Reports run against every shard at once, slowing all customers.

Written and edited by Abhinav Taduka, enterprise software architect.