Skip to Content

How do you choose between hash and range sharding?

Category: System Design

Answer

Hash sharding (shard = hash(key) % N) spreads writes evenly but kills range queries (“give me data from last week”). Range sharding (shard = floor(hash(key) / chunk_size)) preserves locality for range queries but risks hot shards. Most production systems pick range by default and add a consistent-hash layer for hot-key redistribution.

Concrete examples from the fca project context

Example 1

Hash: writes evenly distributed; point lookups fast; range scans expensive (need scatter-gather).

Example 2

Range: time-series queries are local; one shard becomes hot.

Example 3

Consistent hashing: smooth rebalance when one node dies; used by Cassandra, DynamoDB.

Data flow / flow chart

key -> hash -> shard key (mod N or consistent hash)
  pick hash for write throughput, pick range for time-series locality

Takeaway

Default to range for time-series analytics. Switch to hash when writes are skewed.