Sharding partitions a database horizontally across multiple servers, each holding a subset of the data. It's the primary technique for scaling writes beyond what a single machine can handle.
When a single database can't handle your data volume or write throughput, you split the data across multiple database servers — each called a shard. Each shard contains a partition of the total dataset and handles queries only for its data.
Example: Users with IDs 1–1M go to Shard A; IDs 1M–2M go to Shard B; etc.
Split data by a continuous range of the shard key.
Shard A: user_id 0 – 9,999,999
Shard B: user_id 10M – 19,999,999
Shard C: user_id 20M+
Pros: Simple, supports range queries efficiently Cons: Hot shards — if most active users are in one range (e.g., new users have high IDs), one shard gets all the traffic
Apply a hash function to the shard key, then modulo by the number of shards.
shard = hash(user_id) % num_shards
Pros: Even distribution — avoids hot shards
Cons: Range queries become scatter-gather (must query all shards); resharding is painful (changing num_shards requires moving most data)
Maintain a lookup table that maps shard keys to shards.
Pros: Most flexible — supports any mapping, easy to move data between shards Cons: Lookup table is a single point of failure and potential bottleneck (needs caching)
The shard key choice is the most critical sharding decision.
Good shard keys:
user_id, organization_idBad shard keys:
country_code — most traffic may come from US)created_at) — new data always hits one shardWhen you need to add more shards (because existing shards are full or overloaded), data needs to be redistributed. This is called resharding and is painful:
Consistent Hashing solves this — only 1/N of data moves when adding a node instead of ~all of it.
Sharding breaks operations that span multiple shards:
Design your data model to minimize cross-shard operations.
| Aspect | Sharding | Replication | |---|---|---| | Goal | Scale writes and data volume | Scale reads and availability | | Data | Each shard has different data | Each replica has all data | | Complexity | Very high | Medium | | When to use | Data too large for one server | Read-heavy, HA requirements |
They're complementary — you can replicate each shard for high availability.
Sharding adds enormous operational complexity. Exhaust these options first: