Quick revision
The short answer
We detect hot keys by sampling requests in clients or storage nodes and keeping a top-K of the busiest keys per short window, publishing keys above a threshold such as 20,000 reads per second to a hot-key list. We spread their reads by replicating each hot key under derived keys key#0 to key#N-1 and picking one at random, or by caching hot keys in every client for about a second with request coalescing. Readers then get bounded staleness, which suits scores and counters but not balances; writes to one hot key need sub-counters or batching instead.
What to remember
In this answer, we saw that hash partitioning spreads keys but cannot spread one key, so a few hot keys saturate their owners while the cluster idles. We detected hot keys with sampled top-K sketches and an expiring hot-key list, spread their reads with server-side copies and short client caches with coalescing, and stated the consistency honestly as bounded staleness. Hot writes need sub-counters or batching instead, and the whole mechanism must stay off the critical path and be tested with Zipf-shaped load.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We detect hot keys by sampling requests in clients or storage nodes and keeping a top-K of the busiest keys per short window, publishing keys above a threshold such as 20,000 reads per second to a hot-key list. We spread their reads by replicating each hot key under derived keys key#0 to key#N-1 and picking one at random, or by caching hot keys in every client for about a second with request coalescing. Readers then get bounded staleness, which suits scores and counters but not balances; writes to one hot key need sub-counters or batching instead.
Why this question is asked
Real traffic is never uniform. A few keys, a match score or a celebrity profile, can draw more load than whole shards, and a hash-partitioned store sends all of it to the one node that owns the key. Interviewers ask this design question at senior levels because a good answer has to cover detection, two layers of mitigation, a clear consistency promise and the failure modes of the mitigation itself. In this answer, we will recap why hashing cannot help with a single key, design the detector, compare the two ways of spreading reads, state the consistency readers get, deal with hot writes, and make sure the whole mechanism can fail safely.
1Why the Cluster Is Idle While a Few Shards Burn
In a sharded key-value store, each key belongs to one shard, chosen by hashing the key, and the shard's replicas serve its reads. Hashing spreads many keys evenly, but it does nothing for the load of one key: every read of score:match-881 goes to the same owner. If one node handles about 100,000 reads per second, a key receiving 400,000 reads per second needs at least four nodes' worth of capacity, and it only has one. The cluster serves 2 million reads per second in total, so on average each node is fine, which is why the dashboards look mostly idle.
So we need two things that partitioning does not provide: a way to know which keys are hot right now, and a way to serve one key from many places.
2Detecting Hot Keys With a Top-K Sketch
Counting every key exactly would need a counter per key, which is far too much memory at this rate. Instead each storage node, or each client, samples requests (say 1 in 100) and feeds them into a heavy-hitter structure. The Space-Saving algorithm keeps a fixed number of counters, say 1,000; a new key replaces the smallest counter and inherits its count, so truly frequent keys stay in the table with counts that are close to correct. A Count-Min sketch with a small heap works too.
Every few seconds, each node scales the sampled counts back up and reports keys above a threshold, for example 20,000 reads per second, to a small hot-key service, which merges the reports and publishes the hot-key list with an expiry on every entry. Clients subscribe to that list, or poll it every second or two.
- Sample 1 in 100 requestson each storage node or client
- Update top-K tableSpace-Saving, 1,000 counters per 5 s window
- Above 20,000 reads per second?YesReport to the hot-key serviceNoKeep counting
- Publish hot-key listeach entry expires after 60 s unless renewed
- Clients refresh the listevery 1 to 2 s, keep the last copy on failure
5 steps in order. Play them, or step through them one at a time.
Detection is off the request path: a client that cannot fetch the list just reads normally.
3Spreading the Reads: Server Copies or Client Caches
Once a key is hot, there are two ways to spread its reads, and large systems use both. The first is in the storage layer: the owner replicates the hot key to N copies under derived keys, score:match-881#0 to #15, which hash to different shards. A client that knows the key is hot reads a random copy. With 16 copies, 400,000 reads per second become 25,000 per copy, a comfortable load.
The second is in the client layer: each application instance keeps hot keys in a small local cache with a short time to live, for example one second. With request coalescing, when the cached value expires, only one request per instance goes to the store while concurrent callers wait for that result. If there are 2,000 application instances, the store sees at most 2,000 reads per second for the key, whatever the user traffic is.
Caffeine's get with a loader runs the loader once per key while other threads for that key wait, which gives us coalescing for free. Client caching removes the most load, but it depends on every client being updated; server copies help even old clients that read through a proxy. Using both gives defence in depth.
4What Consistency Do Readers Get?
Neither technique is free: both serve copies, and copies can be stale. Writes still go to the owner of the original key, which then fans the new value out to the copies, and client caches refresh after their time to live. So a reader may see a value up to the refresh interval old, around one second plus fan-out time. This is bounded staleness: not linearizable, but with a known maximum delay.
Notice that a reader can also see values go backwards if consecutive requests hit different copies, one updated and one not. For a match score shown on a page, a second of staleness and an occasional flicker are acceptable, and we can hide the flicker by attaching a version to the value and ignoring older versions in the client. For an account balance or a stock count used in a decision, they are not, so such keys must never be marked eligible for this treatment.
5Hot Writes Need a Different Tool
Copies spread reads, not writes. If 100,000 clients increment a like counter on one key, every increment must still reach the owner. Two techniques help. Sharded sub-counters split the counter into N keys, each write increments a random one, and a read sums all N, which turns one hot write key into N warm ones at the cost of more expensive reads. Batching at the writer has each application instance add increments in memory for 100 milliseconds and send one combined increment, which cuts the write rate by the number of events in each batch. Both fit counters and aggregates; neither works for a value that needs read-modify-write with a check.
6Keeping the Mechanism From Becoming a Dependency
A hot-key system that can fail requests is worse than none. The rules are simple. The hot-key list is fetched in the background, never on the request path, and a client that cannot reach the hot-key service keeps its last copy and otherwise falls back to normal reads. Every entry expires on its own, so a key that cools down returns to normal routing without anyone removing it, and stale copies are dropped. The number of hot keys is capped, so a bug cannot mark the whole keyspace hot.
Finally, we load-test with realistic skew. Uniform random keys never produce a hot key, so we generate keys from a Zipf distribution, in which a few keys get most requests, and replay a recorded live-event spike, then check that the detector reacts within seconds and that the copies absorb it.
7Follow-up questions
How do you stop the hot-key list itself becoming a dependency that can fail requests?
Keep it off the request path. Clients load the list asynchronously, cache the last good version and treat a missing list as an empty one, which only means hot keys are read normally from the owner. Entries expire by themselves, the list is size-capped, and the hot-key service can be down for minutes without any request failing; we only lose the extra protection.
Why not simply add more replicas to the shard that owns the hot key?
That helps, but slowly and coarsely: adding a replica copies the whole shard, takes minutes, and the next hot key may be on a different shard. Per-key copies are created in seconds, cost only the size of a few keys, and disappear when the event ends.
Where should detection run, in clients or in storage nodes?
Storage nodes see all traffic for their keys, including from old or third-party clients, so their counts are complete. Clients see only their own traffic but can act on it immediately with a local cache. A common design detects on the storage side and publishes the list, while clients also promote keys they personally read very often.
8Conclusion
In this answer, we saw that hash partitioning spreads keys but cannot spread one key, so a few hot keys saturate their owners while the cluster idles. We detected hot keys with sampled top-K sketches and an expiring hot-key list, spread their reads with server-side copies and short client caches with coalescing, and stated the consistency honestly as bounded staleness. Hot writes need sub-counters or batching instead, and the whole mechanism must stay off the critical path and be tested with Zipf-shaped load.
Open with
First, why it happens: each key has one owner shard, so 400,000 reads per second on one score key hit one node that can do maybe 100,000, while the rest of the cluster idles.
Then, if they want more
For detection, I would sample requests on the storage nodes, say one in a hundred, and keep a top-K table per five-second window using Space-Saving or a Count-Min sketch. Keys above a threshold like 20,000 reads per second go to a small hot-key service that publishes a list where every entry expires after a minute unless renewed.
To spread the load, I use two layers. On the server, the owner replicates a hot key to, say, 16 copies under derived keys key#0 to key#15 on different shards, and clients read a random copy, so 400,000 becomes 25,000 per copy.
In the client, hot keys go into a local cache with a one-second time to live and request coalescing, so each application instance makes at most one read per second for that key.
The consistency is bounded staleness: writes go to the owner and fan out, so readers can be about a second behind and can even flicker between copies, which is fine for a score but never for a balance. Writes to one hot key cannot be copied, so for counters I use sharded sub-counters or batching.
And the list must never fail requests: it is loaded in the background, cached, capped, and expires on its own. I would load-test with a Zipf key distribution.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design hot-key handling across the storage and client layers: how hot keys are detected...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/26a40551d7/