Caches and replicas scale reads. Writes are different: every replica still applies every write, so a single primary is a hard ceiling on write throughput and on how much data fits on one machine. Sharding (horizontal partitioning) breaks that ceiling. Each shard is a full, independent database that owns a subset of the rows.
Try everything else first
Sharding is hard to undo and makes every later feature more complicated. Before you shard, check that you have tried indexes and query tuning, a bigger machine, a cache, read replicas, moving old data to cheaper storage, and splitting unrelated tables into separate databases.
Partitioning strategies
| Strategy | How | Strength | Weakness |
|---|---|---|---|
| Range | Shard A holds ids 1–1M, B holds 1M–2M… | Range scans stay on one shard | Hot spots: new ids all land on the last shard |
| Hash | shard = hash(key) mod N | Even spread | Range queries hit every shard; changing N moves most keys |
| Consistent hash | Keys and shards placed on a ring | Adding a shard moves only ~1/N of keys | More complex routing |
| Directory | A lookup table maps key → shard | Full control, easy to move tenants | The directory is another critical service |
Choosing the shard key
The shard key is the most important decision. A good key:
- Spreads load evenly: it has many distinct values and no single value dominates.
- Keeps queries local: the common queries include the key, so they touch one shard. For a social app,
user_idkeeps a user's posts, likes and settings together. - Never changes: moving a row between shards because its key changed is painful.
Bad keys create hot spots. A celebrity's account or a timestamp-based key can send most of the traffic to one shard. Mitigations include splitting a hot key into key#0…key#9 (and merging on read), or giving very large tenants their own shards.
Why mod-N hurts, and consistent hashing
With hash(key) mod N, going from 4 to 5 shards changes the shard for about 80% of keys, because a key only stays put if h mod 4 = h mod 5. That's a huge data migration for one new shard.
Consistent hashing places both shards and keys on a ring of hash values. Each key belongs to the next shard clockwise. Adding a shard only takes keys from its immediate neighbour, about 1/N of the data. Giving each physical shard many virtual nodes on the ring smooths out uneven ranges and spreads a failed node's load across all the others. Dynamo, Cassandra and many caches use this approach.
The costs you take on
- Cross-shard queries become scatter-gather: ask every shard and merge the results. They're slower, and as slow as the slowest shard.
- Cross-shard transactions need two-phase commit (slow, and it blocks if the coordinator fails) or a saga (a sequence of local transactions, each with an undo step).
- Unique constraints and auto-increment ids stop working globally. Use UUIDs, ULIDs or Snowflake-style ids.
- Operations multiply: backups, migrations and monitoring now cover N databases. Each shard still needs its own replicas for availability.
- Re-sharding is a project. Starting with more logical shards than machines (say 256 logical shards on 8 servers) lets you move whole logical shards later rather than splitting them.
In the game: each database you add in Hypergrowth is another shard with 600 writes/s of capacity. About 2,500 writes/s at peak means at least five shards, and six keeps write latency comfortable. The CDN and cache keep reads off the shards, so the budget goes on write capacity. Celebrity Shard lets you choose the shard key and the key-to-shard mapping, then adds a shard mid-run: watch one shard turn red with a range or plain user_id key, and the rebalance overload every shard with hash mod N.
Interview checklist
- Show that writes or data size have outgrown a single primary.
- Name the shard key and explain why it spreads load and keeps queries local.
- Choose the partitioning strategy (usually hash or consistent hash).
- Handle hot keys and cross-shard queries.
- Plan id generation, per-shard replication and future re-sharding.
Level 13: Celebrity Shard
Put this into practice against live traffic. Your progress in the game is saved in your browser.
Play level 13Further reading
- Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly). The standard book on replication, partitioning and consistency.
- Site Reliability Engineering, Google's free book on running reliable systems.
- The Amazon Builders' Library: practical articles on timeouts, retries, caching and load shedding.
- The System Design Primer: a broad, community-maintained overview.