What is database sharding and what are its trade-offs?
Database sharding is horizontal partitioning of data across multiple independent database nodes (shards), each owning a subset of the dataset. Unlike vertical scaling (bigger hardware) or read replicas (copies), sharding distributes both reads and writes, enabling scale beyond what a single node can handle.
There are three main sharding strategies. **Range sharding** assigns contiguous key ranges to shards (e.g., user IDs 1–1M on shard 1). It supports efficient range queries but creates hot spots when access patterns are skewed (e.g., recent records are always on the latest shard). **Hash sharding** applies a hash function to the shard key (e.g., `hash(user_id) % N`) to distribute data uniformly. It eliminates hot spots but makes range queries expensive—they must scatter to all shards. **Directory sharding** maintains an explicit mapping table from key to shard. It's the most flexible (arbitrary assignment, easy rebalancing) but adds a lookup hop and makes the directory a potential bottleneck and SPOF.
Choosing the shard key is the most consequential decision. A good shard key distributes data and queries evenly, is immutable (changing it requires migrating the row), and aligns with the primary query pattern. Bad shard keys cause hot shards that bottleneck the entire system while other shards sit idle.
The major trade-offs: **Cross-shard queries** are expensive. A query that filters on a non-shard-key column must scatter-gather across all shards and merge results in application code or a query router. **Cross-shard transactions** require distributed coordination (2PC or Saga). **Joins** across shards are generally not supported at the database layer and must be done in application code. **Rebalancing** when adding shards is complex—consistent hashing minimizes data movement, but it's still operationally heavy.
Sharding adds significant operational complexity: connection pooling per shard, schema migrations must run on all shards, backups across N nodes, and monitoring per shard. Many teams delay sharding by exhausting vertical scaling, read replicas, caching, and query optimization first. Managed solutions like CockroachDB, Vitess, or DynamoDB handle sharding transparently and are worth evaluating before rolling a bespoke sharding layer.
| Aspect | Range Sharding | Hash Sharding | Directory Sharding |
|---|---|---|---|
| Distribution uniformity | Poor (skewed if access is skewed) | Excellent | Configurable |
| Range query support | Efficient (single shard) | Poor (all shards) | Depends on mapping |
| Hot spot risk | High (temporal keys) | Low | Low |
| Rebalancing complexity | High | High (consistent hashing helps) | Low |
| Extra infrastructure | None | None | Lookup service required |
| Best fit | Time-series, ordered data | User data, even distribution | Multi-tenant, custom routing |
Explains range vs. hash sharding with a concrete example, identifies shard key selection as critical, names at least two trade-offs (cross-shard queries, rebalancing).
Covers all three sharding strategies with trade-offs, explains shard key immutability and hot spot problem, discusses cross-shard query scatter-gather, mentions consistent hashing for rebalancing, and recommends evaluating managed solutions before manual sharding.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment