Zum Hauptinhalt springen

The Hot Key Problem: When One Shard Does All the Work

· 11 Minuten Lesezeit
Clustron Team
Engineering für verteilte Systeme

The appeal of a sharded data store is a clean scaling story. Partition the keyspace across N shards, send each key to its owner, and the work spreads out: each shard carries roughly a fraction of the total, latency stays flat as the dataset grows, and when you run short of capacity you add shards and the fraction shrinks again. It is one of the most reliable patterns in distributed systems, and for a great many workloads it simply works. But the whole story rests on one assumption that is easy to state and easy to forget: it spreads the load evenly only if every key is touched about equally often.

Real workloads are nothing like that. Access frequency across keys is almost always heavily skewed — a small handful of keys absorb the overwhelming majority of the traffic while the long tail is nearly idle. A single celebrity's profile, the view counter on a post that just went viral, a global configuration key every request reads, the top entry of a leaderboard: each is one key, drawing enormous fan-in. And because partitioning sends a given key to exactly one shard, all of that traffic lands on exactly one shard. That shard saturates — pinned at 100%, queuing, shedding, p99 climbing — while its neighbors twiddle their thumbs.

What makes the hot key genuinely nasty, rather than merely annoying, is that the usual instincts make it worse. The cluster's average utilization looks fine, so capacity planning says there is headroom. Adding nodes lowers the average further and leaves the hot shard exactly where it was, because the hot key does not relocate when you scale out. You cannot add your way out of a hot key. This post is about why that happens and what actually does work.

Sharding spreads keys, not load​

A good hash partitioner guarantees one thing: keys are distributed uniformly across shards. Feed it a million distinct keys and each shard gets about a million over N. That guarantee is real and it is useful — but it is a statement about where keys land, not about how often they are used. The load on a shard is the sum, over the keys it owns, of each key's access frequency, and that second factor is the one that is skewed. The hash function can place keys with perfect evenness and you still get a hotspot, because the skew was never in the placement — it was in the traffic, and no placement function sees the traffic.

This is why "just use a better hash" is not an answer. The hash is already doing its job perfectly. The problem is that one of the evenly-placed keys happens to be touched ten thousand times more often than its neighbors, and evenness of placement does nothing about that.

The dashboard that lies​

The metric almost everyone watches is aggregate or average cluster utilization, and for a hot key it is actively misleading. Suppose five shards: one is pinned at 100% and four idle at 20%. The average is 36%, the dashboard is green, and the capacity report says you are running comfortably. Meanwhile every single request for the hot key must go to the one saturated shard, so a large share of your latency-SLO violations are being generated by a cluster that looks two-thirds empty.

The number that matters is not the mean — it is the maximum shard utilization, and the gap between the max and the mean is precisely the size of your skew problem. A healthy cluster has max ≈ mean. A cluster with a hotspot has a max near the ceiling and a mean that hides it. This is also why scaling out feels like it should help and does not: adding shards dilutes the mean (more idle capacity to average in) while the max stays nailed to the ceiling. You need visibility at the key or slot granularity to even locate the problem — node-level metrics will tell you a shard is hot but not which key is cooking it.

One hot key: shard 3 pinned, the rest idle100%S1S2S3S4S5aggregate ≈ 33% — but shard 3 is the real ceilingAfter replica reads + key split: load spread100%S1S2S3S4S5max ≈ 46%headroomon everyshard

Spread the reads: replicas and near-cache​

Most hot keys are read-hot — far more gets than puts — and reads are the easy case, because a read does not have to go to the owner. If the key is replicated with a replication factor of three, there are three physical copies of it on three different shards. Serve reads from all three and you have tripled the read capacity for that key without touching the write path at all. The cost is that a replica read may be very slightly stale; for the overwhelming majority of read-hot keys that is perfectly acceptable, and only matters when a client must read its own just-written value.

The stronger lever is to not send the read to the cluster at all. Cache the hot value at the client — a near-cache — with a short time-to-live, and the vast majority of reads are answered locally and never cross the wire. This is the single most effective response to a read-hot key, because it removes load rather than redistributing it. Two cautions come with it: you accept a staleness window the length of the TTL, and you must jitter that TTL across clients. A fleet of clients that all cached the value at the same instant will all expire at the same instant and stampede the owner the moment it lapses — the hot key problem and the thundering herd, arriving together.

Split the writes: the sharded counter​

Replicas do nothing for a write-hot key, because every write must go to the single owner. The canonical write-hot key is a counter — likes, views, a rate-limit tally — and the canonical fix is to stop having one. Instead of a single key views, keep N sub-counters, views:0 through views:N-1, which the partitioner scatters across N different shards. Each increment picks a sub-counter at random and bumps it; the true total is the sum of all N, read back with an occasional scatter-gather. One shard's write ceiling has become N shards' worth.

// Write: spread increments across N sub-counters on different shards.
shard = random(0, N)
store.Increment($"views:{id}:{shard}", 1) // one hot key becomes N warm ones

// Read: scatter-gather the parts (reads are rarer, so the fan-out is cheap).
total = 0
for s in 0..N:
total += store.Get($"views:{id}:{s}") ?? 0

The trade is that you give up a single atomic value and pay a fan-out on reads. For a monotonic counter whose exact value is read far less often than it is incremented, that is a fine bargain — you have converted one melting-hot key into N comfortably-warm ones.

The store routes; the pattern is yours​

It is worth being honest about the division of labor. A partition-aware client sends each operation straight to the key's owning shard, and that is exactly correct behavior — it is also exactly why a hot key lands on one shard and stays there. The store can hand you the tools to reshape the load: replica-read routing, a near-cache, atomic increments to build sharded counters on, and per-slot metrics so the hotspot is visible instead of hidden in an average. What the store cannot do is know, in advance, that views:celebrity is about to take a million reads a second and pre-split it for you. The access pattern is application knowledge, and so the mitigation has to be an application decision.

Wrap-up​

Sharding scales a store by spreading keys, but it only spreads load when every key is touched about equally — and real traffic is always skewed, so sooner or later one key runs hot and pins its shard while the cluster around it idles. Averages will tell you everything is fine; the maximum shard utilization is the number that tells the truth, and the gap between it and the mean is the problem. You do not fix it by scaling out, because the hot key does not move. You fix it by reshaping the load the store routed so faithfully: fan reads across replicas, absorb them in a jittered near-cache so the cache's own expiry does not stampede, and split write-hot counters into sub-counters summed on read. The store will route every key to its owner and give you the primitives; spotting which keys will run hot, and spreading them before they melt a shard, is the part that stays yours.