← Lessons

quiz vs the machine

Gold1360

Databases

Hash Based Sharding In SQL

Spreading writes evenly by hashing the key, at the cost of ordered scans.

4 min read · core · beat Gold to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. What is the main benefit of hash sharding?

2. Why are range scans expensive under hash sharding?