Learn
Lesson · Partitioning and Load Balancing · Skew

Hot Keys and Salting

One key carrying a fraction f of the traffic leaves the hottest shard at f·λ + (1 − f)λ/N. The first term has no N in it, so resharding cannot reach below f·λ however far it is pushed — a thousand shards barely moved it in the lab. What does work is splitting the key into s pieces, which turns f into f/s, and the price is paid on every read of that key.

hottest = f·λ + (1 − f)λ/N the first term is not a function of N floor = f·λ what no shard count reaches under f = 12%, λ = 100 000/s: floor = 12 000/s N = 16 → 17 500/s N = 1 024 → 12 086/s salted into s: hot term becomes (f/s)·λ s = 8 → 7 000/s reads overall × (1 − f + f·s) = 1.84 at f = 12%, s = 8
A floor, and the only thing that moves it

Core ideas

What this lesson establishes

One expression with two terms, and everything on this page follows from which of the two terms contains N.

1

The hot term does not contain N

Split the load in two: the hot key's f·λ and everybody else's (1 − f)λ. The second is spread over the fleet and becomes (1 − f)λ/N on each shard. The first goes wherever that one key hashes and stays there, whole. So the hottest shard carries f·λ + (1 − f)λ/N, and taking N to infinity drives the second term to zero and leaves the first exactly where it was.

2

Salting splits the key, not the traffic

Write the key as key#0 through key#(s−1), choosing the suffix at random on each write. Those are s distinct keys, so they hash to s places and the hot term becomes (f/s)·λ on each of them. At f = 12% and s = 8 the hottest shard falls from 17 500 /s to 7 000 /s, which is close to the mean of 6 250 /s.

3

The price is on every read of that key

A read of a salted key has to visit all s pieces and combine them, so reads of that key are multiplied by s. Over the whole read stream the multiplier is 1 − f + f·s, because only the f share fans out: at f = 12% and s = 8 that is 1.84 times the reads overall. Salting is a write-side fix bought with read-side work, and it is only worth it when reads of that key are rare or cheap.

The material

A load with a floor in it

Where the two terms come from, what happens to each as N grows, what salting changes, and what it costs on the read path.

Definition · Hot key

A hot key is a single key that receives a disproportionate share f of the traffic to a dataset. Because every partitioning function maps one key to one shard, the whole of f·λ arrives at one node no matter how the function is chosen or how many shards there are. The remaining (1 − f)λ is spread by whatever the function does.

hottest shard's load, with one hot key hottest(N) = f·λ + (1 − f)·λ/N ↑ ↑ no N goes to 0 as N grows λ = 100 000 /s, f = 12% N mean hottest imbalance above the floor 1 100 000 100 000 1.00× 88 000 16 6 250 17 500 2.80× 5 500 32 3 125 14 750 4.72× 2 750 128 781 12 688 16.24× 688 1 024 98 12 086 123.76× 86 floor = f·λ = 12 000 /s

Read the last column down. Sixty-four times the shards took the hottest shard from 17 500 /s to 12 086 /s, a reduction of 31%, and the remaining 12 086 is 12 000 of hot key plus 86 of everything else. Every further shard is buying a share of the 86. The imbalance column is going the other way — 2.80 times the mean at 16 shards and 123.76 times at 1 024 — because the mean is collapsing while the hottest shard is not.

This is the second of the three skews on this course that resharding does not fix, and it is the starkest. A hot range at least moves when you split it; a hot key does not move at all, because there is nothing in it to split. The asymptote is the shape “Graphs and Asymptotes” on the Algebra path describes: a curve falling toward a horizontal line it never meets, and the line is at f·λ.

Salting: making one key into several

Definition · Salting

Salting a key replaces it with s derived keys — key#0, key#1, …, key#(s−1) — choosing a suffix uniformly at random on each write. Writes to the original key are then spread over s distinct keys, which hash independently. A read must fetch all s pieces and combine them, so the operation the salt makes cheap is the write and the operation it makes expensive is the read.

The arithmetic is immediate: the hot term becomes (f/s)·λ and the rest is unchanged, so the hottest shard holds (f/s)·λ + (1 − f)λ/N. At f = 12%, λ = 100 000 /s, N = 16 and s = 8 that is 1 500 + 5 500 = 7 000 /s, against a mean of 6 250 /s. Where sixty-four times the shards bought a 31% reduction, eight salt pieces bought 60%.

Example · What the salt costs, stated two ways

Every read of the salted key now issues s = 8 requests instead of one, so reads of that key are 8 times as expensive. Across the whole read stream the multiplier is smaller, because only the hot key fans out: 1 − f + f·s = 0.88 + 0.96 = 1.84, so the fleet does 1.84 times the reads overall. Both numbers are true and they answer different questions — the first sizes the latency of a read of that key, the second sizes the fleet. Quoting only the second makes salting sound cheaper than it is for the request that actually touches the hot key.

Choose s from the target, not from habit. The lab reports that at these numbers a salt of 4 already brings the hottest shard within 1.5 times the mean, at a read multiplier of 1.36 rather than 1.84. Going to s = 16 gets the hottest shard to 6 250 /s — the mean exactly — for a read multiplier of 2.80. The last increments of balance are the expensive ones.

When not to salt

Salting is one of three answers and it is the one with a running cost. If the hot key is read far more than it is written, a cache in front of it removes the load without touching the partitioning at all — “Hit Rate and Backend Load” prices that. If the key is hot because of one client, a rate limit is cheaper than either. Salt when the traffic is genuinely write-heavy on a key that genuinely has to be one key, and then write down the read multiplier you accepted.

One more property worth stating plainly: f is measured, not assumed. It is the hot key's share of the traffic, and getting it from a sampled request log is usually easy. Everything on this page is a function of f, λ, N and s, so a designer who knows those four numbers can answer “will more shards help?” before anyone builds anything — and the answer, when f·λ is already above one node's capacity, is no.

Interactive

Hot keys and salting

f·λ + (1−f)λ/N, and the first term is not a function of N

A hot key does not care about Nf·λ has no N in it, so the curve flattens instead of falling
the hottest shardthe mean shardafter salting

Method

Pricing a hot key

Measure f first. Every other number on this page is a function of it, and an assumed f makes the whole calculation decorative.

1

Measure f from a request sample

f is the hot key's share of requests, and a sampled log gives it directly. Do this before anything else: the difference between f = 2% and f = 12% is the difference between a problem more shards will handle and one they cannot touch.

2

Compute the floor, f·λ, and compare it with one node

f·λ is what the hot shard carries after infinite resharding. If it is already above a node's capacity, resharding is not a candidate solution and there is no point pricing it. At f = 12% and λ = 100 000 /s the floor is 12 000 /s, which is more than the 8 000 /s node of “How Many Shards”.

3

Compute the hottest shard at the N you have, and at a much larger one

f·λ + (1 − f)λ/N at both. The gap between them is the entire benefit resharding can deliver — 17 500 to 12 086 here, and the second figure needed sixty-four times the fleet.

4

Choose s from a target multiple of the mean, and write down the read cost

Pick the imbalance you will accept, solve for s, and record 1 − f + f·s beside it. At these numbers s = 4 lands within 1.5 times the mean for a 1.36 read multiplier and s = 8 within 1.12 for 1.84. The number you must not omit from the write-up is the second one.

Floors, salts and read costs

Score 0 / 0

One key at 12% of 100 000 requests a second

The hottest shard at the current fleet, then after a large reshard, then after a salt — with the read cost of the last one stated both ways.

λ = 100 000 /s f = 12% N = 16 mean shard = 100 000/16 = 6 250 /s hot term = 0.12 × 100 000 = 12 000 /s everyone else = 0.88 × 100 000/16 = 5 500 /s hottest shard = 12 000 + 5 500 = 17 500 /s = 2.80 × mean reshard to N = 1 024 mean shard = 100 000/1 024 = 98 /s hottest shard = 12 000 + 0.88 × 97.7 = 12 086 /s = 123.76 × mean bought: 17 500 → 12 086, a 31% reduction for 64× the fleet salt into s = 8, back at N = 16 hot term = (0.12/8) × 100 000 = 1 500 /s hottest shard = 1 500 + 5 500 = 7 000 /s = 1.12 × mean reads of that key × 8 reads overall 1 − 0.12 + 0.12 × 8 = 1.84 × 1.84

Put the two middle blocks side by side. Sixty-four times the machines bought a 31% reduction; eight salt pieces bought 60% and left the fleet alone. That comparison is the lesson, and it is available before anything is built, from four numbers.

For a faded rehearsal, take f to 30% with λ = 100 000 /s and N = 16. The supplied first move is the floor, f·λ = 30 000 /s. Produce the hottest shard now, the hottest shard at N = 1 024, and the salt s that would bring the hottest shard within 1.5 times the mean; then state the read multiplier that s costs overall and the multiplier it costs a read of that key. Say which of your answers would change if λ doubled.

Common mistakes

1
Answering a hot key with more shardsThe load on the hot shard is f·λ + (1 − f)λ/N and only the second term responds. Sixty-four times the fleet bought 31% in the lab, and the remaining 12 086 /s is almost all hot key. If f·λ is above one node's capacity, no shard count is a solution and the design has to change.
2
Quoting the fleet-wide read multiplier as the cost of a salt1 − f + f·s = 1.84 is what the fleet does; a read of the salted key does s = 8 requests and waits for all of them. The second number is the one a latency budget needs, and it is the one that gets left out of the write-up.
3
Salting a key that is read far more than it is writtenSalting trades read work for write balance, so it is backwards for a read-heavy key — where a cache in front of that one key removes the load entirely and changes nothing about the partitioning. Establish the read/write mix on the key before reaching for the salt.

Check your understanding

✓
Finish when “will more shards help?” is a question you answer with arithmetic rather than an opinion.

You should be able to compute the hottest shard's load at any N, state the floor and compare it with one node's capacity, choose a salt from a target imbalance, and report both read multipliers the salt costs.

Note: Two skews down, one to go. A hot range and a hot key are both about where the work lands; the third is about when it finishes, and it appears on any job that waits for all of its partitions. “Stragglers” shows that a job's time is the maximum of N draws, which grows with N even when nothing is skewed at all.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.