Sharding and Partitioning

Split data across machines when one node is not enough, choose shard keys wisely, and handle hot spots, rebalancing and cross-shard queries.

Intermediate · about 4 hours · difficulty 4/5

Learn first: Replication, Indexes and Storage Engines

When data or write traffic outgrows one machine, you partition (shard) it: each node holds a subset of the data. Replication copies data; sharding splits it. Real systems do both.

Strategies

  • Range-based: users A to F on shard 1, G to M on shard 2, or orders by date. Range scans are easy, but ranges can get hot (everyone writes "today").
  • Hash-based: shard = hash(key) mod N. Spreads load evenly, kills range queries, and naive mod N reshuffles almost everything when N changes.
  • Consistent hashing: only a small fraction of keys move when nodes join or leave (covered in its own topic).
  • Directory-based: a lookup service maps keys to shards. Flexible, but the directory is one more critical component.
  • Geo-based: Indian users' data in the Mumbai cluster, EU users' data in Frankfurt, useful for data residency rules.

Choosing a shard key

A good shard key has high cardinality, spreads writes evenly, and keeps data that is queried together on the same shard. For a chat app, conversation_id keeps one chat's messages together. For an e-commerce order system, user_id makes "my orders" a single-shard query but "all orders for seller X" a scatter-gather.

Hard parts

  • Hot keys: a celebrity account or a viral product overloads one shard. Mitigate with key salting, splitting hot keys or caching.
  • Cross-shard queries and joins: scatter-gather to every shard is slow; denormalise or maintain a secondary index.
  • Cross-shard transactions: avoid where possible, or use sagas.
  • Rebalancing: use many small virtual shards mapped to fewer physical nodes so you can move shards, not rows.

Tools like Vitess (MySQL), Citus (Postgres), and built-in partitioning in Cassandra, MongoDB and DynamoDB handle much of this.

Pitfalls

  • Sharding too early. Vertical scaling, read replicas and caching often buy years.
  • Using a monotonically increasing ID or timestamp as a hash-free range key and sending all writes to the last shard.

You are ready to move on when you can pick a shard key for Instagram posts, explain the queries it makes easy and hard, and describe how you would handle a celebrity hot spot.

Key concepts

  • Vertical vs horizontal scaling
  • Range-based partitioning
  • Hash-based partitioning
  • Directory-based sharding
  • Choosing a shard key
  • Hot keys and salting
  • Rebalancing with virtual shards
  • Vitess and Citus

Mini project

Write a small router library that sends reads and writes to one of four SQLite or Postgres databases by hashing user_id. Then simulate adding a fifth shard with naive mod N and measure what fraction of keys must move.

Check yourself

Why is created_at a poor shard key for an orders table, and what would you choose instead?

Free resources