Sharding
Split one big data set across several databases by a key, such as customer or region, so data and load can grow beyond what one server can handle.
Description
When one database can't hold all the data or handle all the writes, you split the rows across several databases, the shards. A shard key, such as customer ID or tenant, decides which shard each row lives on, so all of one customer's data sits together.
A routing layer, a lookup table or a hash of the key, sends each request to the right shard. Each shard is smaller, faster and can be scaled or moved independently. The price is that queries spanning many shards become hard, and choosing the key is a decision that's expensive to change.
System design
Sharding system design diagram
- App servers (service).
- Shard router (gateway): hash(customerId) → shard.
- Shard 1 (database): customers A–H.
- Shard 2 (database): customers I–P.
- Shard 3 (database): customers Q–Z.
- Step 1: App servers → Shard router: orders of customer "kim".
- Step 2: Shard router → Shard 2: only shard 2.
- Step 3: Shard router → Shard 1: report: fan out.
- Step 4: Shard router → Shard 3: report: fan out.
- The app asks for one customer's orders.
- The router hashes the customer ID and goes straight to the one shard that holds that customer. This is the fast, common case.
- A query across all customers, such as a monthly report, has to ask every shard…
- …and merge the results. That's why reports usually run on a separate analytics copy instead.
- AWS
- DynamoDB partitions automatically; Aurora Limitless Database; Citus/Vitess on RDS or EC2.
- Azure
- Cosmos DB partition keys; Azure Database for PostgreSQL elastic clusters (Citus); Elastic Database tools for SQL.
- Google Cloud
- Spanner and Bigtable split data automatically; AlloyDB/Cloud SQL with application-level sharding.
When to use it
Use it when
- Data or write volume has outgrown the largest practical single database.
- Most queries are naturally scoped to one key, such as a tenant, customer or region.
- You need data residency, keeping European customers' data in Europe.
- A noisy tenant should only affect its own shard.
Avoid it when
- A bigger instance, read replicas or better indexes would solve it. Sharding is a last resort, not a first step.
- Many queries need to join or aggregate across all data. Every query becomes a fan-out.
- Your database offers transparent partitioning that already scales enough (DynamoDB, Spanner, Cosmos DB do it for you).
Pitfalls
- A bad shard key that creates hot shards, such as date (all new writes on one shard) or a few huge tenants. Pick a key with many values and even load.
- Cross-shard transactions and joins. Design so most operations stay within one shard.
- Rebalancing pain when shards fill up. Plan for it: many logical shards per physical server, or consistent hashing.
- Global uniqueness: auto-increment IDs collide across shards. Use UUIDs or ID services.
Alternatives
Read replicas
Choose it when reads are the bottleneck, not writes or data size.
Vertical partitioning
Choose it when different tables or services can simply live in different databases.
Managed distributed database
Choose it when you'd rather let DynamoDB, Spanner, Cosmos DB or CockroachDB partition for you.
Cache-Aside
Choose it when the load is mostly repeated reads of hot data.