Module 4 · Caching and performanceLesson 42 of 886 minIntermediate
Hot keys
When one key gets a huge share of traffic, the single node holding it becomes the bottleneck, however many nodes you have.
Turn on the hot key and watch one cache node light up. Then turn on "Copy hot key everywhere" and see the load spread out again.
Sharding and consistent hashing spread keys evenly across nodes. But traffic isn't spread by key count; it's spread by requests. If one key gets half of all requests, the node that owns it gets half the load, even with 100 nodes.
Where hot keys come from
- A celebrity's profile or a viral post.
- A global counter everyone increments ("total likes").
- A flash sale product.
- A config value every request reads.
Fixes for hot reads
- Replicate the key: store copies on several nodes (
post:42#1,post:42#2...) and pick one at random per read. - Local in-memory cache: each app server keeps the hottest few keys in its own memory for a second or two. Thousands of requests never leave the server.
- Put it on the CDN, if it's public content.
Fixes for hot writes
- Split the counter: keep 16 sub-counters, increment a random one, and sum them when reading.
- Batch: collect increments in memory for a moment and write the total.
- Queue writes and apply them in the background.
Detecting them
Per-node dashboards show a node at 100% but not why. Track top keys by request count (Redis has --hotkeys, and many caches can sample keys). Then decide which fix fits.
The same problem appears in databases (a hot shard or a hot row) and queues (a hot partition in Kafka). The fixes are the same family of ideas: copy it, split it, or buffer it.