AI Hints
Home›System Design Tutorial›Hot Spots and CAP
System Design · Lesson 46 of 120

Hot Spots

The celebrity problem.

⏱ 2 min read▶ Video at 54:13🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

Even a perfect shard key can fail. Imagine a celebrity with fifty million followers posts a photo. Every like and comment hits the one shard that holds that post. That shard is now a hot spot.

Hot Spots
From the video: Hot Spots
  • A perfect key can still fail
  • A celebrity posts
  • One shard is overloaded

Fixing hot spots

There are a few standard fixes. Cache the hot item, so most reads never reach the shard. Split the hot key into several sub keys, like post forty two, part one to ten, and add the counts together when reading. Give the biggest accounts their own handling. And watch your metrics, so you spot hot shards early.

Cache itreads skip the shard
Split the keypost42#1 … post42#10
Special-casethe biggest accounts
Monitorspot hot shards early

Splitting a hot counter

Here's the split key idea for a like counter. Instead of one counter, we keep ten, and each like goes to a random one. Run it. The thousand likes are spread across ten counters, so no single counter is hammered. When we need the total, we add them up.

hotkey.pypython
import random
random.seed(1)

counters = {f"post42#{i}": 0 for i in range(10)}
for _ in range(1000):
    part = random.randrange(10)
    counters[f"post42#{part}"] += 1

print("per counter:", list(counters.values()))
print("total likes:", sum(counters.values()))
Output
per counter: [98, 101, 93, 96, 112, 92, 102, 89, 119, 98]
total likes: 1000

Summary

Hot spots happen when one key gets far more traffic than others. Cache it, split it, or handle it specially. Next, a famous theorem about distributed data. CAP.

Key takeaways
  • One key, too much traffic
  • Cache or split
  • Watch for it
Next up: The CAP Theorem — pick two, really one