Full answer
Why it happens
Sharding assumes load spreads evenly across keys. Real traffic is skewed. A celebrity's post, a flash sale item, or the exam results page for one popular board can attract a huge share of requests. Because a key maps to one shard, that one node saturates (CPU, network, or a single-threaded Redis core) while the others sit idle. Adding more shards does not help: the key still lives on one node.
Detecting
- Per-shard metrics: one node with much higher CPU, QPS or latency than its peers.
- Key sampling: tools like
redis-cli --hotkeys(with an LFU eviction policy) or client-side sampling of keys and counts. - Database: partition-level metrics in managed stores, or slow-query logs showing the same key.
- Count-min sketch or top-K sketches in the client or proxy to track heavy hitters cheaply.
Handling hot reads
- Local in-process cache for a few seconds on each app server. If 200 app servers each cache the key for 2 seconds, the cache node sees at most about 100 requests per second for it (200 / 2), no matter how many users read it.
- Replicate the hot key: store copies as
post:99#1...post:99#8on different nodes and read a random copy. Writes must update all copies. - Read replicas for the shard that owns it.
- Put the content on a CDN if it is public.
Handling hot writes
- Split the key: a like counter becomes 16 sub-counters
likes:99:0..15; each increment picks one at random, reads sum all 16. Writes spread across nodes; reads cost more but can be cached. - Batch: aggregate increments in memory for 1 second, then write once.
- Queue: accept writes into a log (Kafka) and apply them at a steady rate.
- Salt the partition key in databases: append a bucket number to the key so writes spread, and query all buckets on read.
Trade-offs
Local caching adds staleness. Key replication multiplies write cost and needs invalidation of every copy. Splitting counters makes reads fan out. Choose based on whether the hot path is read or write, and accept approximate or slightly stale values where the product allows.
By level
Fresher: Explain why one very popular item can overload one server even when there are many servers, and suggest caching it. Suggesting a short local cache is a good fresher-level fix.
Mid: Describe detection with metrics, local caching with TTL, key splitting for counters, and the read cost of splitting. Expect separate fixes for read hot spots and write hot spots.
Senior: Design adaptive hot-key handling (automatic detection and replication), quantify load with numbers, handle write hot spots with logs and batching, and explain impact on consistency and tail latency. Numbers should back each technique, for example how many sub-keys are needed.
Key points
- Skewed traffic overloads the one node owning a popular key
- Adding shards does not fix a single hot key
- Detect with per-shard metrics, key sampling and heavy-hitter sketches
- Fix hot reads with local caching, key replication and CDNs
- Fix hot writes with key splitting, batching, queues and salted keys
Common mistakes
- Adding more shards and expecting the hot key to spread
- Forgetting that key replicas must all be updated
- Making counters exact when approximate is acceptable
- Missing that single-threaded engines saturate one core
Follow-ups
- How would you design like counts for a post getting a million likes per minute?
- How do you pick the number of sub-keys?
- How would you detect a hot key in real time?
Tips
- Name the celebrity problem and give a number
- Separate read hot spots from write hot spots
- Show the local-cache arithmetic
Evaluation rubric
- Explains cause clearly
- Gives practical detection methods
- Separate correct techniques for read and write hot spots
- Discusses staleness and cost trade-offs