In this lesson · topic 2 of 7
Topic 2
Range, hash, and splitting
6 min read
Range partitioning
Sort the keys. Choose boundaries, and give each partition the keys between two boundaries. To find a key’s partition, search the sorted list of boundaries. This is how Bigtable-style stores lay out tables. Each stretch is a tablet or region.
Keys that are close together stay together. A query for everything between two keys, a range scan, reads from one or two partitions. That is the main reason to choose ranges.
Hash partitioning
Hash the key and use the result to pick the partition. A common version splits the hash space into equal ranges, one for each partition. Neighbouring keys have unrelated hashes, so they scatter. A good hash makes the load almost even regardless of key patterns.
The price is that order is gone. A range scan has to ask every partition that might hold a key in the range, which is usually all of them, then merge the results. Single-key reads and writes are as cheap as ever.
Watch both rules on the same twelve keys.
Where keys land under range and hash partitioning
Step 1 / 3- keys
- 11
- partitions
- 3
Eleven keys are placed
Each partition owns a stretch of the key order, so neighbouring keys sit together.
Key 12 arrives, a newer timestamp
It is larger than every key so far, so it belongs to the last partition. Every future timestamp will go there too.
A scan reads keys 5 to 8
The keys are contiguous, so one partition answers the whole scan.
- Eleven keys are placed. Each partition owns a stretch of the key order, so neighbouring keys sit together. keys: 11; partitions: 3.
- Key 12 arrives, a newer timestamp. It is larger than every key so far, so it belongs to the last partition. Every future timestamp will go there too. key 12 goes to: partition 3.
- A scan reads keys 5 to 8. The keys are contiguous, so one partition answers the whole scan. partitions touched: 1.
Hot spots
A hot partition has two sources. The first is a key pattern that range partitioning concentrates, such as sequential timestamps or auto-incrementing ids, where new writes always land at the top of the range. Hashing fixes that kind. The second is skew in the traffic itself, where a few keys are far more popular than the rest. A request count that follows a Zipf distribution is the standard model. Hashing helps with skew only by spreading the popular keys over different partitions. A single very popular key still maps to a single partition, and no choice of hash changes that.
Splitting a hot range
Range partitions can be divided. Pick the busy partition, choose a key inside it, and cut it into two partitions at that key. The halves can then live on different machines. A good split point is where half the load lies below it, not the middle of the key range. The Bigtable paper describes ranges that split when they grow too large, and the Spanner paper describes data that is resharded automatically as it grows. The lab splits on load, which is the version that helps a hot spot.
Splitting has limits. A partition holding one key cannot be divided. A partition at the open end of a sequential workload can be split, but the new keys keep arriving at the new end, so the load never moves.
Fixed partitions
Hashing the key and taking the remainder after dividing by the number of nodes, the scheme from the first lesson, moves most keys when the node count changes. Systems avoid that by creating many more partitions than machines, assigning partitions to machines, and moving whole partitions when machines come and go. The key to partition rule then never changes. Only the partition to machine map does. A ring with virtual nodes is another answer to the same problem.