Partitioning strategies
medium 25 minRange, hash and list partitioning are three rules for placing rows.
Video lesson · Chapter 1 of 5
The first number across the fence
Theory
Read or listen first. About 2 min.
What it is
A partitioning strategy is the rule that turns a key into a partition. Three rules cover most systems. Range places keys by intervals, hash places them by a computed number, and list places them by a table of values.
A list whose table is stored as data and can change at any time is also called a directory or lookup strategy.
Rules can be stacked. For example, a hash of the tenant chooses the shard, and a range of months chooses the part inside it.
Why it exists
No rule is best for everything. A rule that keeps neighbours together makes range questions cheap, and it also sends neighbouring writes to the same place. A rule that scatters neighbours does the opposite.
The rule is also hard to change later, because every row has been placed by it. So it is worth an hour of thought about the queries before the first row is stored.
A short history
1983
Teradata delivers the first shared-nothing database system, with the data divided over nodes.
1997
Karger and colleagues introduce consistent hashing, a hash rule that moves little when nodes change.
2006
Google's Bigtable paper describes tables split into row ranges called tablets.
2017
PostgreSQL 10 adds declarative partitioning.
2018
PostgreSQL 11 adds partitioning by hash, and a default partition for values that match no other.
Words to know
- Range
- A partition owns all keys between two bounds.
- Hash function
- A fixed calculation that turns a key into a number that looks random.
- Modulo
- The remainder of a division: 42 mod 3 is 0.
- List or directory
- A table that says which value belongs to which partition.
- Point lookup
- Reading one row by its exact key.
- Range query
- Reading all rows between two keys.
- Locality
- Rows that are used together live together.
- Hot spot
- A partition that gets far more work than the others.
How it works
Step through the idea, or press Play.
Take the key. For example user_id, a date or a region
Understand it
Start here
Seeds in three beds
Asha has nine packets of seeds and three garden beds. There are several ways to decide which seed goes where, and each makes one kind of search easy.
The same choice appears with a box of toys: by size, by a number computed from each toy, or by kind, with cars here and dolls there.
The idea
Three rules side by side
Range
By intervals: 0 to 999, 1000 to 1999, 2000 and up.
Hash
By a computed number: hash of the key, modulo N.
List
By a table you write: US is 0, EU is 1.
Where does 42 go, with three shards?
| Rule | Calculation | Shard |
|---|---|---|
| Range | 42 is between 0 and 999 | 0 |
| Hash | 42 mod 3 = 0 | 0 |
| Range, key 1500 | between 1000 and 1999 | 1 |
| Hash, key 1501 | 1501 mod 3 = 1 | 1 |
| List, region EU | the table says 1 | 1 |
Range
Range partitioning
Each partition owns an interval of the key: dates, numbers or letters. Intervals must touch without overlapping. The usual habit is to include the lower bound and exclude the upper.
Orders of 2024 by date
| Shard | From | Up to, not including |
|---|---|---|
| 0 | 2024-01-01 | 2024-05-01 |
| 1 | 2024-05-01 | 2024-09-01 |
| 2 | 2024-09-01 | 2025-01-01 |
Ids 900 to 1100 with a range rule
Range is good at
- Range queries touch few partitions
- Easy to understand and to build
- Natural for time-series data
- Old ranges can be dropped whole
Range is bad at
- A hot spot on the newest range when keys only grow
- Ranges can be uneven in size
- Splits and moves are needed as data grows
A range can be split at any key, and systems such as MongoDB and CockroachDB do that by themselves. That makes growing easier than with modulo, not harder. How splits work is in advanced partitioning.
Hash
Hash partitioning
The rule
user_id mod 3
| Shard | Ids |
|---|---|
| 0 | 0, 3, 6, 9 ... |
| 1 | 1, 4, 7, 10 ... |
| 2 | 2, 5, 8, 11 ... |
Ids 900 to 1100 with a hash rule
Hash is good at
- An even number of keys per shard
- Finding the shard in constant time
- Simple code, nothing to store
- Keys that only grow are spread out
Hash is bad at
- Range queries ask all shards
- Adding shards with modulo moves most keys
- Neighbours lose each other
- One very busy key is still one busy shard
Choosing a hash
- 1
Stable
The same result on every server, in every version of your program, for years.
- 2
Well spread
MurmurHash, xxHash and CRC32 are common choices. A cryptographic hash also works and is slower.
- 3
Not negative
A remainder of a negative number is negative in Java, Go and JavaScript. In Java even Math.abs of the smallest int is negative. Use Math.floorMod.
- 4
Not modulo the machines
Use fixed slots or consistent hashing, so that growth moves a fair share only.
List
List and directory partitioning
A table says where each value lives. If the table is part of the schema, it is a list. If it is kept in a service and can change while you run, it is a directory.
A directory of regions
List is good at
- Full control over placement
- New values are one more line
- Locality by business: a region, a large customer
- Moving one value without touching others
List is bad at
- Somebody must maintain the table
- Uneven if values differ in size
- A directory asked on every request can become a bottleneck
- Unknown values need an answer
A lookup that does not overload the directory
Compare
The three rules in one table
| Range | Hash | List | |
|---|---|---|---|
| Spread of keys | Can be uneven | Even | As you decide |
| Range queries | Few partitions | All partitions | Depends on the values |
| Finding the partition | Binary search, O(log p) | O(1) | O(1) in a map |
| Hot spots | Likely with growing keys | Only from hot keys | Possible |
| Adding capacity | Split a range | Modulo: most keys; slots: a fair share | Move one value |
| Typical use | Time series | User data | Regions, tenants |
Range search is not slow
Decide
A short guide
Which rule?
- 1
Need range queries?
Use range.
- 2
Need an even spread?
Use hash.
- 3
Need a place by geography or contract?
Use a list.
- 4
Need two of these?
Combine them.
Together
Two rules at once
Messages are read as the newest 50 of one channel. Hash the channel to choose the shard, and keep rows sorted by time inside it. The channel's messages are together, and channels are spread out.
Hash outside, range inside
Very long partitions are cut by adding a time bucket to the first part, such as channel and month. Discord described this for its messages, with a bucket of about 10 days.
-- Hash, then range, in PostgreSQLCREATE TABLE events ( tenant_id bigint, created_at date, body text) PARTITION BY HASH (tenant_id);CREATE TABLE events_0 PARTITION OF events FOR VALUES WITH (MODULUS 4, REMAINDER 0) PARTITION BY RANGE (created_at);CREATE TABLE events_0_2026 PARTITION OF events_0 FOR VALUES FROM ('2026-01-01') TO ('2027-01-01');-- ListCREATE TABLE users (id bigint, region text) PARTITION BY LIST (region);CREATE TABLE users_eu PARTITION OF users FOR VALUES IN ('EU');CREATE TABLE users_rest PARTITION OF users DEFAULT;Second questions
Queries on another field
Users are sharded by id, and somebody searches by city. Two kinds of secondary index exist.
| Local index | Global index | |
|---|---|---|
| Stored | On each shard, for its own rows | On its own shards, by city |
| Read by city | Ask every shard | One index shard, then the rows |
| Write | One shard | Two places to update |
| Freshness | With the row | Often a little behind |
Choosing an index
Worked example
A school bus tracker
Assume 20,000 buses that report a position every 10 seconds. The main query is the route of one bus today. The second is the last position of all buses of one school.
The load
- 1
Range by time
All 2,000 writes a second hit today's partition. Rejected for shards.
- 2
Hash by bus
Writes are even, and one bus's day is one sorted read. Chosen.
- 3
Days inside each shard
Local ranges by day, so old days can be dropped.
- 4
Second query
A small table with the last position, keyed by school and bus, sharded by school.
Moving on: what to do when a partition gets too large or too hot is in advanced partitioning, and routing on real clusters is in sharding.
Summary
The whole topic in one minute.
Range keeps order: good for intervals and time, with a risk of a hot end. Hash spreads keys evenly: good for single lookups, bad for intervals. A list gives control: good for regions and large tenants, with a table to look after.
Start from the question you ask most. If it is an interval, look at range. If it is a single key, look at hash. If law or a customer decides the place, use a list. Combine two rules when one is not enough.
- Range keeps neighbours together, hash spreads them, a list puts them where you say.
- Range queries: few partitions with range, all with hash.
- A hash spreads keys, not the traffic of one busy key.
- Use a stable hash, never a negative remainder, and fixed slots instead of modulo machines.
- Hash the owner, then sort by time: the most useful combination.