SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
  • CDN
  • Distributed Cache Architecture
  • Cache Stampede
  • Cache Warming
  • Hot Keys
  • Cache Penetration and Cache Avalanche
  • CDN Push vs Pull
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Caching/Distributed Caching

Hot Keys

One key gets far more traffic than the rest, and the one cache node holding it melts.

⏱ 12 min readAdvancedInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

A hot key is one cache key that gets far more traffic than all the others, for example a celebrity profile, a flash-sale item or a viral post.

  • A distributed cache gives each key one home node. All requests for a hot key go to that one node.
  • That node can be overloaded while the rest of the cluster sits idle. Adding more nodes does not help, because the key still lives in only one place.
  • Fixes: keep a copy close to the callers (local cache), store several copies of the key, or reduce the number of requests that reach the node.

02Concept: What is it?

A cache cluster spreads keys over many nodes with a hash. This works well when keys are about equally popular, because every node then does about the same work. But real traffic is uneven. A few keys are asked for thousands of times more often than the rest. Statisticians often describe this with a Zipf-like curve: the most popular key gets a big share, the next one a smaller share, and the rest a long, thin tail.

Hashing sends every request for one key to the same node. If key A gets 40% of all traffic, then the node that owns A has to answer 40% of all traffic by itself, no matter how many nodes you add.

Plenty of capacity, but all on the wrong node
Even keys
👥Requests
↓spread by hash
⚡Node 125%
⚡Node 225%
⚡Node 325%
⚡Node 425%
One hot key
👥Requests
↓40% ask for key A
🔥Node 160%+
⚡Node 215%
⚡Node 312%
⚡Node 413%
The cluster as a whole is fine. One node is not.
⚠️More nodes do not fix a hot key
Adding nodes moves other keys around, but the hot key still has exactly one home. You have to change how the hot key is served, not just add machines.

03Real-Life Example

🍵The One Popular Cashier
A supermarket has eight checkout lanes, and each lane is assigned a letter of the alphabet. Customers must use the lane for the first letter of their surname. On an ordinary day the lines are about equal. One day a famous person with the surname "Smith" opens a signing event, and the whole town joins the "S" lane. Seven lanes stand empty while one lane has a line out of the door. Opening a ninth lane does not help, because "Smith" people may still only use lane S.
Supermarket
Cache
A checkout lane
A cache node
The rule that sends each surname to one lane
The hash that sends each key to one node
The crowd for one surname
Traffic for one hot key
Empty lanes
Idle nodes with spare capacity
Opening a ninth lane
Adding a node (does not help)
Several lanes allowed for “Smith”
Storing the hot key under several names (copies)
Giving each shopper a printed price list
A local cache on every app server
What happens to the hot node
📈A key goes viral
→↓
🔥Its node's CPU and network fill up
→↓
🐢Slow answers and timeouts
→↓
🗄️Misses and retries hit the database

04Why Do We Need It?

It matters because a hot key can take down a cache that looks healthy on average.

📊Averages lie
Cluster CPU says 20%, while one node is at 100%. You need per-node and per-key views.
💥One node, many users
Every user who needs the hot key is slowed down, and often every page that uses it.
🔗Failure spreads
Timeouts cause retries, which add even more load to the same node.
🗄️The database is next
If the hot node fails or evicts the key, the database takes the whole crowd (a stampede).
💸Wasted money
Buying more nodes does not solve it. You pay for capacity you cannot use.
🧭Know your skew
Real traffic is skewed. A design that assumes even load is a design that will fail at the worst time.
Hashing balances keys, not traffic. A key that is hugely popular needs its own plan.

05Where Do We Use It?

⭐Celebrity profiles
One account followed by millions is read constantly.
🛒Flash-sale items
Thousands of buyers refresh the same product page and stock counter.
📰Breaking news and viral posts
A single article or post suddenly dominates traffic.
🔢Global counters
Likes, view counts and rate-limit counters that every request updates.
⚙️Shared config and feature flags
Read on every single request by every server.
🎮Leaderboards and live events
Everyone looks at the same top list at the same time.

06Types / Parts / How It Works

Spot the hot key first

MethodHow it worksNote
Per-node monitoringOne node much busier than the othersThe first clue, but it does not name the key
Key counters or samplingCount requests per key, or sample a fraction and look at the top keysNames the key. Sampling keeps the cost low.
Client or proxy statisticsThe client library or proxy counts the keys it sendsSees the traffic before it reaches the node
Slow or timeout logsWhich keys appear in the slow requestsShows the damage after it starts

Ways to serve a hot key

FixHow it worksGood and bad
Local cache in frontEach app server keeps the hot value in memory for a second or a fewBiggest relief, almost no cache traffic. Values can be slightly stale.
Copy the key under several namesStore key#1 ... key#N with the same value. Each copy hashes to a different node and clients pick oneSpreads the load over N nodes. Updating means writing all copies.
Read replicasReplicas of the owning node also answer readsSpreads reads. Replicas may lag a little.
Request coalescingMany identical requests at one server share a single fetchCuts duplicate work, but does not remove the per-server load.
Split a hot counterKeep N sub-counters and add them up when readingSpreads writes. Reading costs more.
Rate limit or shed loadRefuse some requests for the hot keyProtects the node, but some users get nothing.
Copying a hot key over several nodes
👥Clientspick a copy
→↓1/4 each
⚡Node 2key#1
⚡Node 5key#2
⚡Node 1key#3
⚡Node 4key#4
Same value, different home nodes
Each copy has its own name, so the hash puts it on a different node. The clients take turns.
VisualizerSkewed traffic: one node carries the crowdRaise the skew and watch one node pass its limit. Add nodes (it barely helps). Then copy the hot key or put a local cache in front, and watch the load spread.
What do you do about the hot keys?

Every request for a key goes to the one node that owns the key. Nothing else is done.

Load on each cache node (the dashed line is the limit of one node, 20,000 req/s, illustrative)
Node 1
12,328
Node 2
7,615
Node 3
7,064
Node 4
3,212
Node 5
31,783
Node 6
17,999
Busiest node
31,783
node 5, req/s
Average node
13,334
req/s
Overloaded nodes
1
of 6
Top key share
36%
of all traffic
Key 1 gets 36% of all traffic (28,515 req/s). Hashing sends all of it to node 5, which is above its limit. The other nodes have spare room, but they cannot help, because they do not own the key. Try adding nodes: the key still has only one home.

A simplified model with 200 keys, a fixed popularity table and a fixed hash, so the same settings always give the same picture. The node limit, traffic numbers and 50 app servers are illustrative, not measurements. Which node owns which key depends on the hash, so the busiest node can move when you change the number of nodes.

07How to Use It / Steps / Implementation

Dealing with hot keys

  1. 1
    Watch per-node and per-key load
    Alert on one node being far above the others, not only on the cluster average. Sample the top keys.
  2. 2
    Put a small local cache in front of hot data
    A copy of a second or two in each app server cuts requests to the cache node by a large factor. Choose a TTL you can live with.
  3. 3
    Replicate the keys you know are hot
    For a flash sale, store the product under several names ahead of time. Have clients choose a copy in turn.
  4. 4
    Write every copy
    When the value changes, update all copies, or let them expire quickly, so the copies do not drift far apart.
  5. 5
    Coalesce identical requests
    If a hundred requests on one server want the same key at the same moment, send one request and share the answer.
  6. 6
    Split hot counters
    Spread writes over several sub-counters and add them when reading. Accept that the total is a little old.
  7. 7
    Protect the node
    Use limits and timeouts, so a hot key slows down only itself. Make sure the database is safe if the key is lost (see Cache Stampede).
💡Plan for known events
You can often predict hot keys: a sale, a launch, a live match. Prepare copies and local caches before the event, instead of reacting during it.

08Key Terms to Know

TermMeaning in easy words
Hot keyA key that gets a much larger share of traffic than the others
Hot spotA node, shard or partition that is much busier than the rest
SkewUneven traffic: a few items are far more popular than the rest
Zipf-likeA pattern where the most popular item is much bigger than the next, and so on in a long tail
Shard / nodeOne machine in the cache cluster that owns a set of keys
Key copyThe same value stored under several names so that it lands on several nodes
Local cacheA small copy kept in each app server’s own memory
Request coalescingMerging many identical requests into one fetch
Read replicaA copy of a node that can answer reads
Load sheddingRefusing some requests on purpose to protect a node
Sub-counterOne part of a counter that is split to spread the writes

09Pros, Cons and Trade-offs

✅ Pros
  • A local cache removes most traffic from the hot node
  • Key copies use the whole cluster
  • Easy to prepare for a known event
  • Monitoring top keys finds problems early
  • Coalescing cuts duplicate work for free
⚠️ Cons
  • Local copies can be slightly stale
  • Several copies must all be updated
  • You must know or detect which keys are hot
  • More logic in the client or proxy
  • Splitting counters makes reads more costly

Trade-offs

ChoiceWe gainWe lose
Local cache for hot keysHuge drop in cache trafficStaleness of a few seconds, more memory per server
N copies of a hot keyLoad spread over N nodesN writes per update, more memory
Read replicasMore read capacity per keyReplication lag, more machines
Rate limiting a hot keyA safe nodeSome requests are refused
Adding nodes onlyRoom for other keysNothing for the hot key itself
Pre-splitting before an eventNo surprises on the dayWork and memory for keys that may not get hot

10Common Problems and Mistakes

Common mistakes

  1. Adding nodes to fix it. The hot key still lives on one node.
  2. Only watching the average. The cluster looks healthy while one node melts.
  3. Updating only one copy. Clients that read other copies get old values.
  4. A local cache with no limit. Caching everything locally just moves the problem into the app memory.
  5. Letting the hot node fail with no plan. The key vanishes, and the database gets a stampede.
  6. Treating a hot key as a one-off. The next viral item will be a different key.
  7. Hashing the copy suffix badly. If the copies land on the same node, nothing has been gained. Check where they go.

Things to watch

🌊Retry storms
Timeouts on the hot node make clients retry, which makes it worse. Use backoff and limits.
🧩Key moves
The hot node may move to another when nodes join or leave. Consistent hashing reduces, but does not remove, this.
⏱️Stale copies
Local and replicated copies are only as fresh as their TTL or sync.
🔥Hot writes
A hot key that is also written often (a counter) needs split counters, not just copies.

11Real-World Examples

🐦Social feeds
A post from a very popular account is read by millions in a short time. Popular items are usually cached in more than one place.
🛍️Flash sales
Stock and price keys are copied ahead of time, and held briefly in each app server.
🎟️Ticket launches
One event page gets the whole queue at once.
🌍CDNs
The same idea at the edge: a popular file is spread over many servers instead of one.
⚽Live sports scores
One match key read by a huge number of viewers.
🧱Cache proxies
Some cache clients and proxies detect hot keys and cache them near the caller automatically.
📝Note
Product features differ and change. Check what your cache client or proxy offers for hot-key detection before building your own.
Mini case: a viral product page
👥100,000 req/sfor one product
→↓local hit
🖥️App servers1 s local copy
→↓a few req/s each
⚡4 copieson 4 nodes
Requests that reach the cacheillustrative
A short local cache and a few copies keep the owning node calm.

12Related Topics to Read Next

Distributed Cache Architecture→

How keys are spread over cache nodes in the first place.

Consistent Hashing→

The way keys get their home node, and why one key stays on one node.

Local Cache vs Distributed Cache→

A local cache is the first defence against a hot key.

Cache Stampede→

What happens when a hot key expires or its node fails.

Sharding→

The same hot-spot problem appears when data is split over databases.

Load Balancing Algorithms→

Ways to spread work that look at load, not only at keys.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔What: one key gets far more traffic than the rest.
  • ✔Why it hurts: hashing gives a key one home node, so that node is overloaded while others are idle.
  • ✔Not a fix: adding nodes. The key still lives in one place.
  • ✔Detect: per-node load, then per-key counts or sampling.
  • ✔Fix 1: a small local cache with a short TTL in each app server.
  • ✔Fix 2: store key#1..key#N on different nodes, and write all copies.
  • ✔Also: read replicas, coalescing, split counters, rate limits.
  • ✔One line: “Hashing balances keys, not traffic, so give a popular key more than one home.”
← Previous
Cache Warming
Next →
Cache Penetration and Cache Avalanche