Distributed Systems Reference/Partitioning & Sharding

Partition Keys, Skew & Resharding

Range vs hash partitioning, hot partitions, secondary indexes across shards, and how systems rebalance without downtime.

3/5Overview: 25m

Why partition

Replication copies the same data; partitioning splits different data across nodes so each shard fits on disk and CPU. Most large systems use both: partitions, each replicated.

Partitioning strategies

StrategyHowRisk
Key-rangeKeys A–M on shard 1, N–Z on shard 2Hot spots if keys cluster (e.g. recent timestamps)
Hash of keyshard = hash(key) mod NEven spread, but range queries expensive
Consistent hashingRing with virtual nodesMinimal remapping when nodes added/removed

See Networking roadmap for consistent hashing in load balancing — same idea applied to data placement.

Hot partitions

Skewed access patterns create hot shards — one node serves disproportionate traffic. Mitigations:

  • Salted keysuser_id#random_bucket spreads writes, complicates reads
  • Split hot key — sub-shards for celebrity accounts
  • Separate read path — cache in front of hot partition
  • Async aggregation — don't make one row the write bottleneck

Secondary indexes across shards

Global secondary index either:

  • Scatter-gather — query all shards, merge (slow, expensive)
  • Co-located index — index partitioned by same key (limited lookup patterns)
  • Dedicated index service — separate system (Elasticsearch, etc.)

Know the trade-off; system design covers product choices.

Resharding

When you add capacity, move partitions between nodes. Goals: minimal downtime, limited double-writes, routing table updates. DDIA §6.4 covers fixed vs dynamic partitioning.

Interview one-liner

"Pick partition key from the query pattern you can't afford to scatter-gather — usually tenant_id or user_id — and watch for hot keys before you need a fire drill."

Further Reading

Hands-On Tasks (Optional)

Low-setup exercises — browser visualizers, paper drills, or optional Docker. No autograding; the goal is interview fluency.

  • Design around a hot partition key

    A social feed uses `celebrity_user_id` as the partition key and one celebrity has 100M followers. Propose two techniques to spread write load (salting, key splitting, caching layer) in bullet points.

    15m