Try
↑ ↓ move↵ openesc close
In this lesson · topic 2 of 7
  1. 1. Too much data, too many writes
  2. 2. Range, hash, and splitting
  3. 3. Split a hot range and compare layouts
  4. 4. When one key outruns every partition
  5. 5. Adding a node, and how requests find their partition
  6. 6. Looking up by the column you did not partition on
  7. 7. Check the partitioning rules

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
Partition 11234Partition 25678Partition 391011Range: keys 1-4, 5-8, 9-12 in orderhighlighted = newest key or scanned keys
keys
11
partitions
3
  1. Eleven keys are placed

    Each partition owns a stretch of the key order, so neighbouring keys sit together.

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.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms