advanced

Sharding

Split data by key or range while planning routing, rebalancing, hot shards, cross-shard queries, and migrations.

Sharding splits data across nodes by a shard key — user ID hash, tenant ID, or geographic region. It raises write throughput and storage capacity but complicates cross-shard queries, transactions, rebalancing, and schema migrations. Choose keys that avoid hot shards and match access patterns.

Plan a routing layer: application logic, proxy, or embedded client. Rebalancing requires consistent hashing or dual-write migration strategies. Celebrity users may need dedicated shards or cache layers.

On interviews: pick a shard key for a social app, explain a cross-shard query workaround, and describe rebalancing without downtime.

Common pitfalls: monotonic keys causing hot spots; shard key that changes over time; joins across shards in the hot path.

The trade-off is flexibility versus complexity—know when the simpler path is enough.

Checklist:

  • Select shard key from access patterns.
  • Design routing and discovery layer.
  • Plan rebalancing and migration.
  • Mitigate hot shard and celebrity skew.