Hash sharding is the default choice for many workloads: uniform distribution, simple routing. But rebalancing when the shard count changes is painful. Here's the full picture.

user_1024user_2049user_5137hash(id) mod NN=4 shardsShard 0Shard 1Shard 2Shard 3Rebalance painadding shard = move most keys
Hash sharding: uniform distribution, painful rebalancing
Advertisement

The mechanic

shard = hash(key) mod N. Simple, fast, deterministic. Same key always same shard.

The mechanic

shard = hash(key) mod N. Simple, fast, deterministic. Same key always same shard.

Advertisement

Distribution

Good hash function → uniform distribution. No hotspots from natural key clustering (unlike range sharding).

Rebalancing problem

Going from N to N+1 shards changes hash(key) mod N for most keys. Massive data movement.

Consistent hashing fix

Instead of mod N, map keys onto a ring. Adding a shard moves only 1/N of keys. Foundation of Dynamo, Cassandra.

When to use

Uniform access patterns. Keys with high cardinality. When you don't do range queries.

Hash for uniform distribution; consistent hashing for graceful rebalancing.