In this lesson · topic 4 of 7
Topic 4
When one key outruns every partition
6 min read
These numbers come from the simulator with a fixed seed, and a test pins each one. Hover a value to see its setup. Load is the busiest partition divided by the mean, so 1.00x is perfectly even.
Hot spots by traffic shape
Eight partitions, 800 ticks of 40 requests each:
| Traffic | Range partitioning | Hash partitioning |
|---|---|---|
| Uniform keys | 1.03xmeasured | 1.06xmeasured |
| Sequential keys | 8.00xmeasured | 1.02xmeasured |
| Zipf keys, skew 1.0 | 6.29xmeasured | 1.54xmeasured |
Under uniform traffic nothing is wrong either way. Under sequential keys, range partitioning sends everything to one partition, which is the largest possible factor for eight, while hashing is flat. Under skew, hashing cuts the imbalance a lot but does not remove it.
Hash cannot fix a hot key
With skew 1.0 the busiest range partition took 79%measured of requests and the busiest hash partition 19%measured, against a fair share of 12.5%measured. The most popular key alone is a large slice of traffic, and it lives in one place whatever the rule. Raise the skew to 1.4 and the hash layout’s imbalance becomes 2.89xmeasured.
The usual remedies live above the partitioning scheme. Cache the hot key in front of the database. Split a hot key into several sub-keys that are written separately and combined on read. Or give a hot key its own partition with more replicas.
What hashing costs: range scans
Uniform keys, eight partitions, scans of 300 keys out of ten thousand:
| Rule | Partitions touched per scan |
|---|---|
| Range | 1.2measured |
| Hash | 8.0measured |
A range scan needs the keys in order. Under range partitioning they are in one or two places. Under hashing they are in every partition, so the scan costs a round of requests to all of them, and its latency is that of the slowest. A longer scan makes range partitioning touch more as well. At 2000 keys a range scan touched 2.5measured partitions on average, while hash was already at eight and could not go higher.
Systems that want both often hash a prefix of the key and keep the rest sorted. Scans within one prefix stay on one partition, and different prefixes spread out.
Splitting, and where it stops
Skewed keys, range partitioning, busiest partition’s share of requests after each batch of splits:
| Splits | Busiest partition’s share |
|---|---|
| none | 79%measured |
| 1 | 40%measured |
| 3 | 21%measured |
| 8 | 10%measured |
Each split halves the load of the hottest partition, so the share falls fast. After eight splits there were 16measured partitions and the share had flattened out. By then the busiest partition is the single most popular key, and a single key cannot be cut. That sets the floor.
Sequential keys are worse. After five splits the busiest partition still took 100%measured of the requests, because every new key is larger than the split point and arrives at the same open end.
What the model leaves out
- Load is counted as requests. Real partitions also differ in bytes stored, request cost, and read versus write mix.
- Splits are instant and free. A real split moves data, and the busy partition is the one least able to spare the effort.
- The key space is fixed at ten thousand keys for the non-sequential cases, and nodes never fail.
- A scan counts as one request and adds no load to the bars.
- Cross-partition operations, such as transactions that write two keys in two places, are not modelled. They are slower and harder, which is another reason to keep related keys together.