Spreading By Hash
Hash sharding maps each primary key through a hash function, then assigns the row to a shard based on the hash. Because a good hash scatters keys uniformly, writes spread evenly even when keys arrive in order.
Strengths And Costs
- No write hot spot: sequential ids land on different shards.
- Load is balanced across nodes without manual tuning.
- The cost is that ordered range scans no longer map to a few shards.
Since neighboring keys hash to unrelated shards, a query for a key range must fan out to all shards and merge results.
Choosing The Strategy
Some systems let you pick per table or per index. Use hash for keys that increase monotonically and are queried by exact match, such as user ids. Use range when ordered scans dominate, such as time series reads.
Key idea
Hash sharding scatters keys to balance writes and kill hot spots, but it sacrifices cheap ordered range scans that range sharding preserves.