Split the data across many databases, so no single one holds everything.
Sharding means splitting one big dataset into smaller pieces, and putting each piece on its own database. Each piece is a shard. No single machine has to hold or serve everything.
user_id) decides which shard owns each row.One database server has limits: disk size, memory, and how many writes per second it can take. You can buy a bigger server (vertical scaling), but that gets very expensive and then stops. The other way is to use many servers and give each one a part of the data. This is horizontal scaling, and for databases it is called sharding.
The router can be a library in your app, a separate proxy, or part of the database itself. The important thing is that every request carries the shard key, so it can go to one shard.
| Strategy | How it works | Good and bad |
|---|---|---|
| Range-based | Each shard owns a block of keys (ids 1 to 1M, 1M to 2M...). | Range queries are fast. New data piles on the last shard, so it gets hot. |
| Hash-based | shard = hash(key) mod N. The hash spreads keys evenly. | Even spread. Range queries hit every shard. Changing N moves almost everything. |
| Consistent hashing | Shards and keys sit on a ring. A key goes to the next shard clockwise. | Adding a shard moves only about 1/N of the keys. Needs more design. |
| Directory-based | A lookup table says which shard has which key (or tenant). | Most flexible. The lookup service must be fast, small and always up. |
| Geo-based | The shard is chosen by the user's region. | Low latency and legal control. Uneven sizes, only a few shards. |
shard = hash(user id) mod N. Spreads users evenly. But when N changes, almost every user gets a new shard.
A simplified model with a fixed, deterministic data set (no random numbers). Users are numbered 1 to 200. Latency is a rough guide: the point is that a query without the shard key must visit every shard, and that adding a shard moves very different amounts of data depending on the method.
| Term | Meaning in easy words |
|---|---|
| Shard | One piece of the data, stored on its own database |
| Shard key | The field that decides which shard owns a row |
| Router / proxy | The part that sends a request to the right shard |
| Hot shard / hot key | A shard or key with much more traffic than the rest |
| Cardinality | How many different values a field can have |
| Resharding | Moving data when the number of shards changes |
| Scatter-gather | Ask every shard, then merge the answers |
| Consistent hashing | A hashing method where adding a node moves little data |
| Directory | A lookup table from key to shard |
| Cross-shard transaction | A change that touches rows on more than one shard |
| Saga | A sequence of local transactions, with undo steps if one fails |
| Choice | We gain | We lose |
|---|---|---|
| Hash sharding | Even spread | Range queries touch all shards |
| Range sharding | Fast range queries | Hot spots on the newest range |
| Directory sharding | Total flexibility | A new critical dependency |
| More shards | More total capacity, smaller failures | More machines, more scatter-gather cost |
| Fewer, bigger shards | Simpler operations | Harder to grow later |
| Shard by tenant | Easy isolation and single-shard queries | One huge tenant can be a hot shard |
hash mod N with no plan. Going from 4 to 5 shards moves about 80 percent of the data.