GOAT EngineerPreparing your learning space…

System Design · Concept Library · Free article

Hot Keys & Partition Skew — When One Key Takes All the Traffic

Why a system that’s perfectly balanced on paper can still melt a single machine — because one key went viral — and the ladder of fixes, from caching the hot read to sharding the hot counter. The partition-skew problem every large system eventually hits, for interns through principal engineers.

Almost every large system spreads its load by key: hash the key, send it to the shard (or partition, or node) that owns it. That works beautifully — until one key is far more popular than all the others. A viral link, a celebrity user, a "null" default that half your traffic falls back to: suddenly all of that key’s traffic slams the single machine that owns it, while the rest of the fleet sits idle. The system is perfectly balanced by key count and completely overloaded all the same. That’s a hot key — the special case of partition skew — and it turns up everywhere you partition: database shards, Kafka partitions, Redis cluster slots, cache nodes, even a Spark job. The fixes fork on one question — is the key hot for reads or writes? Read-hot you beat by making more copies (cache or replicate the value near readers); write-hot you beat by splitting the target (shard the one counter across many sub-keys) or shrinking the writes (pre-aggregate locally and flush). But first you have to notice it, because a design that looks even on the whiteboard can melt one node in production.

Ninety-nine nodes idle, one node on fire

Hatch has done everything right. Clicks are emitted as events off the redirect, aggregated in the stream, and unique visitors are counted cheaply with HyperLogLog. The counters live in a big Redis cluster, spread across a hundred nodes so no single machine holds too much. On paper it’s beautifully balanced. Then MegaDrop goes viral, and the dashboard for that one link starts timing out — while ninety-nine of your hundred Redis nodes are barely awake.

The culprit is a single key. MegaDrop’s counter — say clicks:mega-drop — lives on exactly one node (that’s how the cluster decides who owns what). When the link is quiet, fine. When it’s pulling fifty thousand clicks a second, every one of those increments lands on that one key, on that one node, pinned to a single CPU core that can only do one thing at a time. Latency on that key climbs to hundreds of milliseconds, the writes back up, and the feature falls over — not because the cluster is out of capacity, but because one key’s worth of it is. The other ninety-nine nodes can’t help; they don’t own the key. *You didn’t run out of machines. You ran out of one machine.*

Partitioning balances by count — and bets every key is average

To see why this happens, look at what partitioning actually promises. When you spread data across machines, you pick each item’s home by its key — most simply hash(key) % number_of_shards, or a consistent-hashing ring, but the idea is the same: run the key through a function and it deterministically picks a shard. Because the hash scrambles keys evenly, the keys spread out beautifully — a hundred shards each own roughly one-hundredth of the keys. That’s the guarantee, and it’s a real one.

But read the fine print. Partitioning balances the number of keys per shard, and quietly assumes every key carries about the same load. That assumption is usually close enough — and occasionally catastrophically wrong. Real traffic is rarely uniform; it’s skewed, often wildly, following a power law where a handful of keys get the overwhelming majority of the action. One link goes viral, one creator gets famous, one product trends — and now a single key is doing the work of a million ordinary ones. The shard that owns it is buried while its siblings idle. The cluster is balanced by key count and badly imbalanced by load, at the same time — and load is the thing that melts hardware.

Every system that partitions by key meets this

It’s tempting to file this under "a Redis thing," but it is the same story anywhere load is split by key — which is everywhere at scale. The names change; the shape doesn’t:

  • Database sharding — shard by user_id and one whale customer (or one celebrity account) sends a single shard’s CPU to the moon. This is the classic hot shard / hot tenant.
  • Kafka partitions — key your events by link_id and a viral link funnels every one of its events to a single partition, so a single consumer drowns while the others starve. A hot partition.
  • Redis / cache nodes — one celebrity key on one node, exactly Hatch’s situation; the cache is balanced by key and overloaded on one.
  • Consistent-hashing rings — the same node that owns the hot key’s slice takes the hit; adding nodes rebalances keys, not the one hot key’s load.
  • Data-parallel compute (Spark / MapReduce) — a GROUP BY on a skewed key sends all its rows to one reducer, and the whole job waits on that one straggler. Here skew is usually called data skew, and it’s the number-one cause of a job that’s "99% done" for an hour.

Feel it once and you’ll recognise it forever. Below, traffic is split across six partitions. Drag the skew up — concentrate more and more of the load on the single hottest key — and watch one partition sail past its capacity and melt while the rest coast, even though the key count stays perfectly even:

The interactive version of this example is available when JavaScript loads.

Finding the hot key (it hides on the whiteboard)

Here’s what makes skew sneaky: it is invisible in the place you design. On the whiteboard every shard looks identical, each owning its tidy fraction of the keys, and the averages look wonderful — average load per node, average latency, all healthy. The hot key hides inside those averages, because one melting node barely moves a fleet-wide mean. Skew is a tail problem wearing an average’s clothing. So you have to go looking, and the tools are about distribution, not totals:

  • Per-partition, not just per-cluster, metrics. Watch CPU, latency, and QPS per node/shard/partition and alarm on the spread between them. One node at 95% while the mean is 30% is the signature of a hot key — a healthy average is exactly what hides it.
  • Sample requests by key and look at the head. Count access per key (even on a small sample) and inspect the top of the distribution. If the #1 key is doing 100× the median, you have your hot key. Redis ships this (redis-cli --hotkeys); proxies and DBs expose per-key/per-partition stats (DynamoDB’s CloudWatch per-partition metrics, for instance).
  • Watch consumer/replication lag per partition (for Kafka-style systems) — one partition whose lag keeps climbing while the rest stay flat is a hot partition betraying itself.

Is the key hot for reads, or for writes?

Before you fix a hot key, ask one question, because it splits the remedies cleanly in two: is the key being hammered by reads or by writes? They look similar — one key, one overloaded node — but they yield to opposite tactics, and confusing them is why people reach for a fix that doesn’t help.

  • Read-hot — everyone is reading the same value: a celebrity’s profile, a viral link’s destination, a trending product’s page. The value is the same for everyone, so the answer is to make more copies of it and spread the reads across them — an in-process cache on each server, replicas of the value on several nodes, a CDN at the edge. Copies are cheap and safe because reads don’t change anything.
  • Write-hot — everyone is writing to the same key: a counter incremented on every click, a like count, a leaderboard score. Now copies don’t save you — every write still has to land and be reconciled, so you can’t just read from a replica. Instead you either split the write target (shard the one counter into many sub-keys) or shrink the number of writes (aggregate locally and flush periodically). Different problem, different toolbox.

Read-hot: make more copies and spread the reads

The read-hot case is the friendlier one, because the value everyone wants is identical — a viral link’s destination, a celebrity’s profile, a trending price. When a thousand readers all want the same unchanging thing, you don’t need them all to visit the one node that owns it; you just need more copies of the answer, in more places. Reads are safe to copy because they don’t change anything. The ladder, cheapest first:

  • A local (in-process) cache on each server. Keep the hot value in each app server’s own memory, so the overwhelming majority of reads are answered right there and never reach the shared node at all — it only ever sees the occasional miss. Best for a small handful of brutally hot keys, with a short TTL to bound staleness. This is the caching page’s in-process tier, aimed squarely at hot keys.
  • Copies of the key across several nodes. Store the same value under a few differently-named keys (profile:zoe:0…3) so they hash to different nodes, and have readers pick one at random — the reads scatter instead of piling on one node. (Redis calls this out for its hot-key problem.)
  • Read replicas — for a read-hot database row. A hot read on a shard is exactly what replicas are for: fan the reads out across replicas of that shard. (This is the one place replicas do help — because it’s reads, not writes.) See read replication.
  • A CDN / edge cache — when the hot thing is servable content. A viral link’s redirect or a trending page can be cached at the edge so reads are answered near the user and never touch your origin at all — the CDN & edge caching move.

Write-hot: split the target, or shrink the writes

Write-hot is harder because copying doesn’t help: every write has to actually land and be reconciled, so you can’t just point writers at a read-only copy. There are exactly two ways out, and they’re opposite in spirit — spread the writes across more targets, or make fewer writes. Both are worth knowing; the best systems use both.

Move one: shard the counter. Instead of a single key clicks:mega-drop that every increment hammers, split it into N sub-keys — clicks:mega-drop:0 through clicks:mega-drop:9. Each write increments a random one of the sub-keys; to read the total, you read all N and sum them. Because the sub-keys hash to different nodes, the write load that used to crush one CPU now spreads across ten, and the logical counter’s write throughput multiplies by N. The price is on the read side: getting the total is now N reads plus an addition, instead of one. So you pick N for how hot the key is — enough shards to spread the writes, not so many that the read fan-out hurts. Drag the write load and the shard count and watch a single key go from melting to comfortable:

The interactive version of this example is available when JavaScript loads.

Move two: pre-aggregate locally, then flush. Notice that most of those writes are redundant: if one app server handles a thousand MegaDrop clicks in a second, it doesn’t need to send a thousand separate +1s to the shared counter — it can keep a local tally in its own memory, and once a second send a single +1000. The shared key’s write rate collapses from "every click, everywhere" to "one flush per server per interval," no matter how viral the link gets. This is the very same "aggregate before you touch the shared thing" move as stream aggregation, applied to the counter. See how few writes actually reach the shared store:

The interactive version of this example is available when JavaScript loads.

Two more moves: give the whale its own machine, and collapse the duplicate reads

Everything so far spreads or shrinks the load. But there is a whole class of hot keys where the hot thing is not a Redis counter but a row on a database shard — a whale customer’s data, a celebrity account, a mega-viral link that all lands on the one shard that owns it (the database-sharding page calls this the hot shard, and it is the exact failure that melts a sharded database). Two more moves earn their keep here, and neither is about spreading or shrinking — they are about isolation and deduplication.

Move one: dedicate a shard — give the whale its own machine. When one tenant or key is permanently and enormously hotter than the rest — not a passing viral moment but a whale customer who is simply ten thousand times bigger — the cleanest fix is to stop making it share at all: pull it out of the shared pool and put it on its own shard, often a beefier one. Now its load cannot starve its neighbours, and noisy neighbours cannot slow it down. This is the multi-tenant "cell" or "pod" isolation move — big SaaS platforms routinely route their largest customers to dedicated infrastructure for exactly this reason. Two things to keep honest: you now maintain a mapping of who lives where (a small routing directory), and you are back to one machine for that key — so dedication solves the noisy-neighbour problem, not raw single-key throughput. For a hot counter you still combine it with the sharding and pre-aggregation from the last section.

The interactive version of this example is available when JavaScript loads.

Move two: coalesce the duplicate reads (single-flight). A viral key often gets hit not by different work but by the same read, thousands of times at once — ten thousand people opening the same trending link in the same second. Done naively that is ten thousand identical queries slamming the one shard. Request coalescing (also called single-flight) notices that the first query is already in flight and makes the other 9,999 wait for its result instead of each firing its own — so the shard serves one query and everyone shares the answer. It is the in-flight cousin of caching: a cache stops repeat reads after the first one finishes, but coalescing stops the flood of identical reads that arrive while the first is still running — the cache-miss stampede (the thundering herd). Discord leaned on exactly this — request-coalescing "data services" — to tame hot-partition cascades on their message store, where a single busy channel could otherwise turn one partition’s slowness into a cluster-wide stall. Drag the concurrency up and watch coalescing collapse the flood to a single query:

The interactive version of this example is available when JavaScript loads.

Every fix has a bill — and sometimes you shouldn’t pay it

  • Sharded counters cost you on reads (N reads + a sum for every total) and add keys to manage — and you must remember to read all the shards, or you silently undercount.
  • Local pre-aggregation trades freshness (the total lags by the flush interval) and risks losing the in-flight tally if a server crashes before it flushes — fine for a click metric, unacceptable for anything you bill.
  • Salting / random keys (spreading a key by appending a random suffix) breaks anything that relied on that key staying together — per-key ordering in Kafka, or a clean time-series — and complicates aggregation, since you now have to gather the salts back together.
  • Caching / replication (the read-hot fixes) pay in staleness and invalidation, as ever.
  • A dedicated shard costs you manual placement (a routing directory to maintain) and leaves that key on a single machine — great for isolating a noisy neighbour, useless for a single-key throughput problem, so combine it with the spread/shrink fixes for a hot counter.
  • Request coalescing only helps when the concurrent reads are identical (same key, same query) and adds a sliver of latency to the followers who wait on the leader — and if the leader’s query fails, you must be careful not to fail everyone who was waiting on it.

The same four moves, everywhere

  • Kafka hot partition — re-key with a salt or a compound key to spread a viral key’s events, add partitions, or drop the key entirely for round-robin if you don’t need per-key ordering.
  • DynamoDB hot partition — the canonical write-sharding example: add a random suffix to the partition key to scatter a hot item across partitions (and modern DynamoDB adds adaptive capacity to lend a hot partition more throughput automatically).
  • Cassandra hot partition — same discipline: design the partition key so no single value can attract a disproportionate share of the writes.
  • Spark / big-data skew — a GROUP BY or join on a skewed key sends all its rows to one task; the fix is salting — append a random bucket to the key, aggregate per-salt, then combine — turning one giant task into many small ones.
  • The social "celebrity" problem — when a user with fifty million followers posts, fanning that write out to fifty million feeds is a write-hot explosion, so systems switch those accounts to fan-out on read (merge the celebrity’s posts in at read time) — a hot-key decision made at the architecture level.
  • Database hot shard — a whale tenant or celebrity row melts the one shard that owns it; the DB-layer toolkit adds dedicating a shard to the whale (multi-tenant cell isolation) and request coalescing identical reads (Discord’s fix for hot-partition cascades), on top of caching and read-replicas in front of the shard.

The two write-hot fixes, concretely

  • Sharded counter — write. INCR clicks:mega-drop:{rand 0..9} — each click bumps a random one of ten sub-keys, so the writes scatter across ten nodes instead of pinning one.
  • Sharded counter — read. MGET clicks:mega-drop:0 … clicks:mega-drop:9 then sum the ten values — the read fan-out you trade for the write spread.
  • Local pre-aggregation — in the app. Keep an in-memory Map<linkId, count>, do local[linkId]++ on each click (no network), and once a second flush: INCRBY clicks:mega-drop <local count> then reset — one shared write per second per server instead of thousands.
  • Salted partition key (Dynamo / Kafka). Write to mega-drop#{rand 0..9} so the key hashes to different partitions; read all ten salts and merge. Same idea as the sharded counter, at the partition-key level.

Pitfalls

  • Throwing read replicas at a write-hot key. They scale reads; every write still funnels to the one primary. It’s the reflex that wastes the most time.
  • Sharding everything by reflex. With no actually-hot key, you’ve added read fan-out and complexity for nothing. Detect first; shard the hot keys only.
  • Forgetting to read all the shards. A sharded counter is only correct if you sum every sub-key — miss one and you silently undercount.
  • Salting a key you needed kept together. Scatter a key that required per-key ordering (Kafka) or a contiguous time-series and you’ve traded one problem for another.
  • Relying on local pre-aggregation where you can’t lose counts. A crash before the flush loses the in-flight tally — fine for a metric, wrong for billing.
  • Dedicating a shard and forgetting it’s still one machine. Isolation stops noisy neighbours, but a single hot counter on its own shard is still a single hot counter — pair it with sharding/pre-aggregation.
  • Coalescing that fails everyone when the leader fails. If the one in-flight query errors, don’t propagate that failure to all the waiters blindly — retry or let a follower become the new leader.
  • Never measuring. Applying a fix to the wrong key (or none) because you looked at the average instead of the per-partition distribution.

Best practices

  • Detect before you fix — watch per-partition load (not the mean) and sample the key distribution; skew hides in the average.
  • Classify read-hot vs write-hot — it picks the toolbox, and picking wrong wastes effort.
  • Read-hot → copy and spread the reads — local cache, key copies, read replicas, or a CDN for servable content.
  • Write-hot → spread or shrink the writes — shard the counter (increment a random sub-key, sum on read) and/or pre-aggregate locally and flush; combine both for extreme keys.
  • Persistently huge key → isolate it — give a whale tenant its own dedicated shard so it can’t hurt its neighbours; and coalesce identical concurrent reads (single-flight) so a burst of the same read hits the shard once.
  • Size the fix to the heat — shard/salt count matched to the key’s load, and leave the cold long tail as plain single keys.
  • Accept the trades on purpose — read fan-out, staleness, lost-on-crash, broken ordering — and only where the skew justifies them.

Rapid-fire cheat-sheet (intern → principal)

"What happens when one user or link goes viral?"
That key becomes a hot key: all its traffic lands on the single shard that owns it, which melts while the rest of the cluster idles. Partitioning balances key count, not load. I’d confirm it (per-partition metrics, key sampling), classify it read-hot vs write-hot, and apply the matching fix.
"Why won’t read replicas fix a hot counter?"
Replicas scale reads only; every write still funnels to the single primary that owns the key. A write-hot key is unmoved by replicas — it needs write sharding or local pre-aggregation.
"How do you scale a single counter under massive writes?" (senior)
Shard it: split the counter into N sub-keys, increment a random one per write, and sum all N on read — writes spread across N nodes, at the cost of a read fan-out. And/or pre-aggregate locally: each server keeps an in-memory tally and flushes periodically, so the shared key sees one write per server per interval instead of every increment.
"How would you handle a hot partition in Kafka or DynamoDB?" (staff)
Spread the key: salt it (append a random suffix) or use a compound key so it scatters across partitions; add partitions; or, in Kafka, drop the key for round-robin if you don’t need per-key ordering. DynamoDB’s random-suffix write sharding is the textbook version.
"A single whale tenant is melting one database shard — what do you do?" (staff)
If it’s a persistent whale (not a passing spike), dedicate a shard: pull that tenant onto its own machine so it can’t starve its neighbours — the multi-tenant cell-isolation move — maintaining a small routing directory. Put a cache and read-replicas in front of the hot shard for reads, and coalesce identical concurrent reads (single-flight) so a burst hits it once. Remember a dedicated shard is still one machine, so pair it with sharding/pre-aggregation for a hot counter.
"How is request coalescing different from caching?" (senior)
A cache stops repeat reads after the first completes and stores the result. Coalescing stops the duplicate reads that arrive while the first is still in flight — thousands of identical concurrent reads collapse to one query whose result everyone shares. It’s the fix for the cache-miss stampede (thundering herd); the two complement each other.
"How do you even detect a hot key?"
Not from averages — a healthy fleet-wide mean hides one melting node. Watch per-partition/per-node load and alarm on the spread; sample requests by key and look at the head of the distribution (e.g. redis-cli --hotkeys, per-partition CloudWatch metrics).
"When would you NOT shard or pre-aggregate?"
When nothing is actually hot. These fixes add read fan-out and complexity, so you apply them only to the measured-hot keys and leave the cold long tail as plain single keys. Over-mitigating a cold key just taxes every read.

The takeaway

By GOAT Engineer. Back to Concept Library. Privacy Policy · Contact.