Partition strategies: range, hash and directory
When one machine is not enough
Section titled “When one machine is not enough”A database on one machine is limited by that machine: its disk, its memory, the writes it can absorb. Copies of the data on other machines, replicas, help with reads and with surviving a failure, but every copy still holds all of the data and every write still has to reach the one machine that accepts it. When the data or the writes outgrow a machine, the data has to be cut into pieces, and each piece placed on its own machine.
This course calls a piece a partition, and a partition that lives on its own machines a shard. Systems use these words differently, so check the vocabulary of the one you use. What matters is the rule that decides where each key lives. Everything else in this module follows from that rule.
Partitioning pays in capacity: ten shards can hold ten times the data and take ten times the writes, if the load is shared evenly. It costs in three places: a request has to find the right shard, a query that spans shards gets harder, and changing the number of shards means moving data. The three strategies of this lesson are three answers to the first of those, and each one sets the price of the other two.
Mental model. A partition strategy is a function from a key to a shard. Range uses the order of the keys, hash uses a number that looks random, and a directory looks the answer up in a table. A good strategy is cheap to evaluate, spreads the load, and changes little when the number of shards changes. No strategy is best at all three.
Three ways to place a key
Section titled “Three ways to place a key”Range. Cut the key space into contiguous ranges and give each range to a shard: keys user:0 to user:2499 here, the next 2,500 there. Keys that are close stay together, so a scan of a stretch of keys, such as all the orders of one week, reads one shard or a few. The cost is that whatever is close in the key space is close in the machines too: a busy stretch of keys stays on one shard. MongoDB’s documentation notes that a shard key whose values only ever increase sends every new insert to the chunk with the highest bound, which is one shard. It offers range-based and hashed sharding side by side, and recommends hashed sharding for a key that changes monotonically.
Hash. Compute a number from the key with a hash function, and use the number to pick the shard. The simplest form is hash mod N: with N shards, a key goes to shard hash mod N. Nothing is stored, any client computes the same answer, and neighbouring keys land on unrelated shards, so a busy stretch of keys is spread out. The costs are the mirror image of range: a scan of neighbouring keys has to ask every shard, and changing N changes the remainder of almost every key. The next lessons deal with that. Redis Cluster shows another form: it hashes a key with CRC16 into one of 16,384 slots and assigns slots to nodes, so the number of nodes can change while the hash does not.
Directory. Keep a table that says which shard holds each key, or each group of keys. Vitess uses this idea for secondary lookups: a lookup vindex is a table that maps a column value to the id that says where the row lives. A directory is the most flexible strategy, because any key can be put anywhere, and one key can be moved without touching the others. The price is the table: it has to be stored, kept in step and consulted before every request, and it can become a bottleneck or a single point of failure of its own.
Try it: the same keys, four layouts
Section titled “Try it: the same keys, four layouts”The lab has 10,000 keys, user:0 to user:9999, spread over four shards. It offers a fourth strategy, Consistent hashing, which is the subject of lesson 3. Start with Range, then switch Strategy to each of the others and read Placement metadata.
Ten thousand keys on four shards. Switch between the strategies, add and remove shards, and watch how many keys each change moves.
What the last change did
Add or remove a shard, change the strategy, the virtual nodes or the traffic, or press Rebalance. This panel says how many keys the change moved.
- Hottest shard
- 25%Shard 1: 1× the fair share of 25%
- Keys moved by the last change
- —No change yet
- Keys per shard
- 2,500 most, 2,500 fewest10,000 keys on 4 shards
- Placement metadata
- 4 entriesOne boundary for each range.
fair share 25%
Keys arriving
A sample of the requests, and the shard that serves each key under the current layout. The chart and the table hold the figures.
- user:7539Shard 4
- user:4900Shard 2
- user:9833Shard 4
- user:5352Shard 3
- user:8588Shard 4
- user:7741Shard 4
- user:4536Shard 2
- user:7331Shard 3
Look up a key
user:1234 is on Shard 1.
| Shard | Keys held | Share of requests | Keys moved in | Keys moved out |
|---|---|---|---|---|
| Shard 1 | 2,500 (25%) | 25% | 0 | 0 |
| Shard 2 | 2,500 (25%) | 25% | 0 | 0 |
| Shard 3 | 2,500 (25%) | 25% | 0 | 0 |
| Shard 4 | 2,500 (25%) | 25% | 0 | 0 |
Shards are numbered from 1 here. In the C# example the same shards are node-0, node-1 and so on. The keys moved in and out are those of the last change.
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- The lab counts 10,000 keys, user:0 to user:9999, the same keys as the C# example. A key moves when the shard that holds it changes, and every key counts the same, whatever it holds.
- The hash is FNV-1a over the text of the key, and a virtual node sits at the hash of its number, a # and the name of its shard. This is the definition the C# example uses, so the same keys move. It is not a cryptographic hash.
- A shard's load is the share of requests that reach its keys, worked out from how popular each key is. It is what a very long stream converges to, not a measurement of a run. The stream shown beside it is a sample.
- Hot keys are neighbouring numbers, starting at user:3400, like a burst of new ids or one customer's records. The rest of the requests choose any key with equal chance.
Simplifications
- No data is copied and nothing takes time: a resize or a rebalance is instant. In a real system, moving keys copies data, competes with the traffic, and can take hours.
- Range and directory follow simple rules: split the busiest range, move the hottest keys first. Real systems choose when to split and move, and limit how much moves at once.
- FNV-1a puts labels that differ only in their last character close together, so with a few virtual nodes this ring is more lopsided than points placed at random would be. With 100 or more, the two agree more closely.
- Cross-shard queries, replicas and the cost of looking up a directory are not modelled: the lab counts where keys live and what changing that costs.
Parameter values are illustrative. They are not benchmarks of any real product.
The metadata says what each strategy has to remember: Range keeps 4 entries, one boundary for each range, Hash mod N keeps none because the hash decides, Consistent hashing keeps 400 ring entries, and Directory keeps 10,000 entries, one for each key.
Now press Add a shard and read Keys moved by the last change, then Remove a shard from four. Starting from four shards each time:
| Strategy | Keys moved adding a fifth shard | Keys moved removing the fourth |
|---|---|---|
| Range | 1,250 (12.5%) | 2,500 (25%) |
| Hash mod N | 8,006 (80.1%) | 7,559 (75.6%) |
| Consistent hashing | 1,988 (19.9%) | 2,858 (28.6%) |
| Directory | 0 | 2,500 (25%) |
Read the table as a trade. Hash mod N is the cheapest to run and the most expensive to change: four keys in five move. Range and a directory move only what they choose to. The range layout splits its busiest range in half and gives the new shard the upper half, which is 1,250 keys. The directory adds an empty shard and moves nothing, until you decide what to put on it. Removing a shard moves at least what it held, 2,500 keys for a quarter of the data, and range and a directory move exactly that. Hash mod N moves three times as much.
Each strategy has a cost that the table hides. A directory that moved nothing also helped nothing: the new shard is empty, and it takes keys only when someone moves them. Lesson 4 returns to that.
Challenge
Which partition strategy?
Range, hash and directory partitioning each decide where a key lives in a different way. Put each statement under the strategy it describes or needs.
Sorting needs JavaScript.
Categories
- Range
- Keys cut into contiguous ranges, one range for each shard.
- Hash
- The shard is computed from a hash of the key, with no table.
- Directory
- A table says which shard holds each key.
Check your answers at least once before looking at the answers.
What happens if the keys are numbered in the order they were created?
Section titled “What happens if the keys are numbered in the order they were created?”Set Strategy to Range and think about an id that grows with time, such as an order number. The newest keys are always at the top of the key space, so they belong to the last range. Every new order lands on one shard, while the other three hold old data that is rarely written. Adding shards changes nothing for that traffic, because the newest range only ever lives on one. This is the monotonic-key problem that MongoDB’s documentation describes, and the hash strategies avoid it by scattering neighbours.
The reverse also happens. A hash scatters the orders of one week across all the shards, so a report on that week has to ask all of them. The strategy that protects writes can make scans expensive, and the one that makes scans cheap can concentrate writes. Which to choose depends on which of the two your traffic needs more, and lesson 2 is about that choice.
- Partitioning splits data over machines to gain capacity. It costs routing, harder queries across shards, and moving data when the shards change.
- A strategy is a function from a key to a shard. Range keeps neighbouring keys together, hash scatters them, and a directory looks the answer up in a table.
- Range makes scans cheap and can leave a busy stretch of keys on one shard. Hash spreads a busy stretch and makes scans ask every shard. A directory moves any key anywhere, at the price of a table to store and consult.
- In the lab, going from four shards to five moves 1,250 keys with range, 8,006 with hash mod N, 1,988 with consistent hashing and none with a directory.
- Each strategy remembers something different: 4 boundaries, nothing, 400 ring entries, or 10,000 table entries.
Next: the shard key, and how to choose the field the placement is computed from.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.