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.
Diagram
Section titled “Diagram”- Every request carries the shard key, here the tenant, and the router looks up which shard holds it.
- The request runs entirely on one shard, so each shard stays small and fast.
- Reports across all tenants run on an analytics store fed by change data capture, not by querying every shard.
Your options, in the order to try them
Section titled “Your options, in the order to try them”| 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 |
Choosing a shard key
Section titled “Choosing a shard key”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.
How it works
Section titled “How it works”- 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 to use it
Section titled “When to use it”When write volume or data size truly exceeds what a well-tuned single primary can handle, and a natural key (such as tenant) exists.
When to avoid it
Section titled “When to avoid it”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.
Common pitfalls
Section titled “Common pitfalls”- 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.
Related pages
Section titled “Related pages”- Replication and high availability
- Zero-downtime schema changes
- Scenario: the database is the bottleneck
- CQRS and event sourcing
- Data warehouse
Further reading
Section titled “Further reading”- PostgreSQL, Table partitioning (opens in a new tab)
- Vitess, Sharding (opens in a new tab)
- Microsoft, Sharding pattern (opens in a new tab)
- Martin Kleppmann, Designing Data-Intensive Applications, chapter 6: Partitioning (O’Reilly)
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.