Definition · Range partitioningUnder range partitioning the key space is cut into contiguous intervals and each interval is a shard. The shard of a key is found by locating the interval that contains it, so ordering is preserved: a scan over an interval of keys touches only the shards whose ranges overlap it. Hash partitioning destroys this property deliberately; range partitioning is chosen when it is needed.
The workload that breaks it is the most common workload there is. Events keyed by arrival time, rows keyed by an auto-increment id, documents keyed by a monotonically increasing version — in all of them the key of the next write is larger than the key of the last one, so the next write goes to whichever range holds the top of the key space.
8 ranges, 2 400 writes, monotonic key
window 1 300 · · · · · · · hottest 100.0%
window 2 · 300 · · · · · · hottest 100.0%
window 3 · · 300 · · · · · hottest 100.0%
…
window 8 · · · · · · · 300 hottest 100.0%
over the whole run every range 300 of 2 400 = 12.50%
ranges taking any writes right now: 1 of 8
There is no N in the first column of that table. Doubling the shard count gives sixteen ranges of which one is taking every write, and halving it gives four ranges of which one is taking every write. Splitting the hot range is the same: the write pointer moves into one of the halves immediately and that half is then taking everything. This is the first of the three skews on this course that resharding does not touch.
Why nobody notices until it is in production
The lifetime histogram is flat. Every range ends the run with 12.50% of the writes, which is exactly an equal share, so a dashboard that plots writes-per-shard over a long window reports a perfectly balanced cluster. The measurement that shows the problem is writes-per-shard over the last few minutes, and it reports one shard at 100% and seven idle.
Example · The same fact from the read sideThe flip side is the reason to have done this at all. Under the monotonic key, “read everything from one window” touches 1 range of 8. Under a hashed key the writes spread — the hottest range takes 16.00% of a window against an equal 12.50%, which is 1.28 times a fair share rather than 8.00 times it — and the same read now touches 8 of 8. The write hotspot and the scan cost are the same property seen from two directions.
The middle setting, and what it is a dial on
Put a hashed bucket in front of the timestamp and the current writes spread over as many ranges as there are buckets. At four buckets the lab measures the hottest range at 30.33% of the current writes instead of 100%, and a one-window scan reads 4 ranges instead of 1 or 8. Increasing the bucket count moves both numbers in the directions you would expect, and the bucket count is the only thing being chosen.
Notice that even hashing does not deliver the equal share. 16.00% against 12.50% is the imbalance of “Hash Partitioning and Imbalance” showing up in a different measurement, on 300 writes a window rather than 1 200 keys. Hashing moves the problem from a factor of eight to a factor of 1.28; it does not move it to one.
One more caution about the word “hot”. A hot range is a region of the key space that is busy, and splitting the range or hashing the key addresses it. A hot key is a single key that is busy, and neither of those helps at all, because a single key lands in one place under every partitioning function there is. That is the next lesson.