Partitioning Strategies: Range, Hash, Directory and Consistent Hashing
Overview
Once you decide to shard, you need a rule that says which shard owns each key. The rule you choose decides how evenly data spreads and which queries stay fast.
This lesson covers the four common rules and when each fits.
Detailed Notes
What is a partitioning strategy
A strategy is the rule that maps a shard key to a shard. The four common ones are range, hash, directory and consistent hashing.
The problem it solves
You want data and traffic spread evenly, so no machine is overloaded, while still being able to find data quickly and, ideally, to add machines without moving everything. Different rules trade these goals off.
How it works
Range partitioning. Each shard owns a continuous range of keys: A-H, I-P, Q-Z, or dates by month.
- Good: range queries ("all orders in March") hit one or few shards.
- Bad: uneven load. If everyone writes today's date, the newest shard gets all the traffic.
Hash partitioning. Compute hash(key) mod N (N = number of shards) to pick the shard.
- Good: spreads keys very evenly.
- Bad: neighbouring keys end up on different shards, so range queries must ask every shard. And if N changes from 4 to 5, almost every key maps to a new shard, so almost all data must move.
Directory (lookup) partitioning. A separate lookup table says which shard holds each key (or group of keys).
- Good: very flexible. You can move one customer to another shard by editing the table.
- Bad: the lookup service is extra work and can become a bottleneck or single point of failure.
Consistent hashing. Place both shards and keys on an imaginary circle (a hash ring) using a hash function. A key belongs to the first shard found going clockwise from its position.
- Good: when a shard is added or removed, only the keys next to it move, about 1/N of the data, instead of nearly everything.
- Often combined with virtual nodes: each machine appears at many points on the ring so load spreads more evenly.
- Used by Cassandra, DynamoDB-style stores and many caches.
A simple example
Sorting mail into pigeonholes. By range: letters A-H in hole 1 and so on. By hash: a formula based on the name picks the hole, so mail spreads evenly but alphabetically close names are scattered. With a directory: a clerk keeps a notebook of where each person's mail goes.
System design example
A storage service needs to add servers often. Using hash(key) mod N, going from 10 to 11 servers would move about 90% of the data. With consistent hashing, only about 1/11 moves. An analytics system that mostly queries "last 7 days" prefers range partitioning by date, but adds a second key (like customer id) so today's writes do not all hit one shard.
Summary
- Range keeps nearby keys together: great for range queries, risky for hot spots.
- Hash spreads data evenly but scatters ranges.
- Directory is flexible but adds a lookup component.
- Consistent hashing moves only a small share of data when shards change.
- Pick based on your queries and how often you add machines.
Architecture Diagram
The same key can land in different places depending on the rule.
- 1The shard key is read from the request.
- 2A rule turns the key into a shard number.
- 3The query is sent to that shard.
- 4Changing the rule or the shard count may move data.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

