Topic 073 / Core
Partitioning and sharding: shard keys, hot partitions, resharding, rebalancing
Chapter 5.5 · Distributing Data
What and why
Replication copies the same data everywhere; partitioning (sharding) splits different data across nodes so no single node holds — or writes — all of it. It solves a write-throughput ceiling that replication alone cannot. Builds on: → 72 (replication).
Diagram
shard key hash % 3
key "cust_41" → hash → shard 2
┌─────────┐ ┌─────────┐ ┌─────────┐
│ shard 0 │ │ shard 1 │ │ shard 2 │ ← hot: one key gets disproportionate traffic
└─────────┘ └─────────┘ └─────────┘
How it works
- Choose a shard key that distributes load evenly and lets most queries hit a single shard.
- Range partitioning (ordered ranges per shard) keeps range scans local but risks hotspotting on sequential keys; hash partitioning spreads load evenly but loses range-scan locality.
- A poorly chosen shard key (a celebrity id, or a monotonic timestamp) creates a hot partition — one shard absorbing disproportionate load while others idle.
- A query that doesn't include the shard key must fan out to every shard — expensive, and easy to miss when designing the schema.
- Resharding and rebalancing: as data grows or hotspots appear, keys must move between shards — operationally expensive and risky unless planned for up front (e.g., consistent hashing → 74, or pre-splitting into many logical shards mapped few-to-one onto physical nodes).
Practical example
(illustrative). Sharding an "order" table by a sequential
order_id with range partitioning puts every new order — the most actively queried rows —
on the newest shard: a textbook hot partition. Switching the shard key to
hash(customer_id) spreads both writes and the customer-scoped reads that dominate the
workload evenly, at the cost of losing cheap "orders between two ids" range scans.
When to use it
- Use when: read replicas and vertical scaling are exhausted and a single writer is genuinely the bottleneck, measured, not assumed.
- Avoid when: sharding is reached for early — it is a one-way door that complicates every query, transaction, and migration from that point on.
- Failure scenarios: cross-shard transactions and joins (→ 138); a shard key chosen for write-path convenience that forces reads to fan out to every shard.
- Simpler alternative: a single, well-indexed database with read replicas — adequate well past the point teams assume they need sharding.
Practice
(decision/numeric, 20 min). For an orders table at 500M orders/year across
10 shards, queried mostly by customer_id, compare three shard keys — customer_id,
order_id, and hash(customer_id) — and identify which risks a hot partition and which
keeps the dominant query single-shard. Done when you can name the specific query pattern
that decides the answer.
Key takeaway
A shard key is chosen for the dominant query pattern and even load, not convenience — and resharding later is expensive. Done when you can: spot a hot partition from a described shard key before it's built. Used next in → 74, 75; distributed transactions → 138.
References
Kleppmann and Riccomini, Designing Data-Intensive Applications — see
references.md.