vishal patel
UnderstoodAdvancedUpdated 2026-09-23

Sharding & Partitioning

Split data across nodes so storage and throughput scale horizontally — choosing shard keys, range vs hash, hot spots and rebalancing.

scalingshard-keyconsistent-hashinghot-spots

Strategies

diagram
StrategyGood forRisk
Range (createdAt, alphabetical)Range scansHot spot on the newest range
HashEven distributionRange queries become scatter-gather
Directory / lookup (tenant → shard)Moving big tenants individuallyThe lookup service is critical infrastructure
Consistent hashingAdding/removing nodes moves only ~1/N of keysNeeds virtual nodes for balance

Choosing a shard key

A good key has high cardinality, even write distribution, and is present in most queries (to avoid scatter-gather).

For multi-tenant SaaS, { tenantId, <something high-cardinality> } is the classic compound key. Queries stay tenant-local and big tenants still spread across chunks.

Pain points

  • Hot keys (a celebrity user, one giant tenant): split the key, add a random suffix, or give that tenant a dedicated shard.
  • Cross-shard transactions and joins are expensive. Design aggregates to live on one shard.
  • Resharding is operationally heavy, so pick the key carefully. (MongoDB 5+ supports live resharding.)

Cheatsheet

The whole topic on one page. Click to open full screen.

Sources & further learning

Videos, courses, docs and books I recommend for this topic.

Related topics