The full answer
Sharding splits one logical dataset across several independent database servers, each holding a subset of the rows, so storage, reads and writes scale beyond one machine. The main approaches differ in how a row's shard is chosen: by key range, by hash, by a lookup directory, or by geography.
Partitioning is not sharding. Partitioning splits a table into pieces inside one database server; sharding spreads them across servers. Partitioning is often the first step, and sharding comes when one server can no longer cope.
1. Range-based sharding Rows go to shards by ranges of a key: customers 1–1,000,000 on shard 1, the next million on shard 2; or by date.
- Pros: simple; range queries stay on one shard; easy to add a shard for new ranges.
- Cons: hot spots. New customers or today's data all land on the newest shard while older ones sit idle.
2. Hash-based sharding
A hash of the shard key decides the shard: shard = hash(customer_id) % number_of_shards.
def shard_for(customer_id: int, shards: int) -> int:
return hash_function(customer_id) % shards # e.g. a stable hash such as xxHash or MurmurHash
- Pros: spreads data and load evenly.
- Cons: range queries hit every shard. With plain modulo, changing the number of shards moves almost every row; consistent hashing or a fixed large number of virtual shards mapped onto servers moves only a fraction.
3. Directory-based sharding A lookup service maps each key (or tenant) to a shard.
- Pros: total flexibility: move a large tenant to its own shard, rebalance gradually.
- Cons: the directory is an extra lookup on every request and must be highly available and cached.
4. Geographic sharding Data lives in the region of its users: EU customers in an EU database.
- Pros: lower latency for users, and helps meet data residency rules.
- Cons: global reports and users who move between regions need extra work.
Choosing a shard key
The most important decision. A good key has many distinct values, spreads load evenly, and keeps data that is queried together on the same shard. In a multi-tenant SaaS application, tenant_id is the classic choice, because almost every query is for one tenant.
Benefits of sharding
- Writes and storage scale horizontally, beyond the largest single server.
- Each shard is smaller, so indexes fit in memory and maintenance is faster.
- A failure affects only the shard's share of users (each shard still needs its own replicas).
Drawbacks
- Cross-shard queries and joins must be run on every shard and combined, which is slower and more complex.
- Transactions across shards need distributed transactions or careful design to avoid them.
- Rebalancing when shards grow unevenly is operationally hard.
- Operations: backups, schema changes and monitoring multiply by the number of shards.
- Unique IDs must be generated without a single sequence (UUIDs or ID services).
Before sharding, try a bigger server, read replicas, caching, partitioning and archiving old data. When sharding is necessary, tools such as Citus for PostgreSQL and Vitess for MySQL, or distributed SQL databases, handle routing and rebalancing so the application doesn't have to.
Last updated 16 October 2026
Sources