What is database sharding?

Database sharding splits one logical dataset across several independent database servers, called shards, using a shard key. Each shard holds a subset of the rows, so writes and storage scale beyond one machine.

Also called sharding, sharded database, horizontal shardingUpdated

Database sharding splits one logical dataset across several independent database servers, called shards. Each shard holds a subset of the rows, for example the first million customers on shard A and the next million on shard B, and has its own CPU, memory and disk. Sharding is horizontal scaling: adding servers rather than buying one bigger server.

Sharding vs partitioning vs replication

  • Partitioning splits a table into pieces inside one database server, such as PostgreSQL declarative partitioning by month. Queries and transactions work as normal. It helps with large tables and retention, not with server limits.
  • Sharding puts the pieces on different servers. It is partitioning across machines, sometimes called horizontal partitioning.
  • Replication copies the same data to several servers for availability and read scaling. Shards are usually replicated too. MongoDB, for example, requires every shard to be deployed as a replica set.

How rows find their shard

Every sharded table has a shard key: a column chosen so that related rows live together and load spreads evenly.

  • Hash sharding. Hash the key and assign hash ranges to shards. This spreads load evenly, even for keys that only increase, such as timestamps, but range queries have to visit every shard.
  • Range sharding. Each shard holds a range of key values. Range queries stay on one shard, but an ever-increasing key sends all new writes to the last shard.
  • Directory sharding. A lookup table maps each tenant or customer to a shard. It is flexible and lets you move one large customer to its own shard, at the cost of an extra lookup.

A router sits in front of the shards: mongos in MongoDB, the coordinator node in Citus for PostgreSQL, VTGate in Vitess for MySQL. A query that includes the shard key goes to one shard. A query without it is broadcast to every shard and the results are merged, which is slow.

Example: sharding a B2B invoicing app by tenant

An invoicing SaaS has 4,000 business customers, and its single PostgreSQL primary is at its write limit. The team distributes its tables by tenant_id with Citus:

SELECT create_distributed_table('invoices', 'tenant_id');
SELECT create_distributed_table('invoice_lines', 'tenant_id', colocate_with => 'invoices');

Each tenant's invoices and invoice lines sit on the same node, so a query joining the two with WHERE tenant_id = 812 runs on one node with normal joins and transactions. Reports across all tenants would hit every node, so they move to a data warehouse instead. A tenant that grows far larger than the rest can be moved to a shard of its own.

This works because the app is multi-tenant and almost every query is scoped to one tenant. That is the best case for sharding. Some databases can also pin key ranges to shards in a chosen region, such as MongoDB zones, which helps with data localisation rules.

Before you shard

Sharding is hard to undo and adds permanent work: cross-shard joins and transactions, rebalancing, schema changes on every shard, and backups that must be consistent across shards. Try the simpler options first: indexes and query tuning, a larger instance, read replicas, caching, partitioning and archiving old data. As our PostgreSQL vs MongoDB for fintech comparison notes, most products won't outgrow a well-tuned PostgreSQL primary with read replicas for a long time.

Common mistakes

  • A low-cardinality shard key, such as country or status, so a few shards hold most of the data.
  • A key most queries don't filter on, so most queries hit every shard.
  • Auto-increment IDs per shard, which collide across shards. Use UUIDs or a central ID scheme.

Any idea in mind? Let's make it real.

Tell us what you're building. You'll hear back from an engineer, not a sales team, usually within one working day.

Book a 30-min call