CodePark/ questions / Invalidation, stampedes & consistency
START PRACTICINGPRACTICE
Backend question · Redis & caching › Invalidation, stampedes & consistency

What is a hot key problem, and how do you deal with it?

A hot key is one key that gets a much bigger share of the traffic than other keys. Examples are a viral post or a global config value. A key lives on one node only. In Redis Cluster, it lives in one hash slot. So adding more shards does not spread its load. That one node runs out of CPU or network capacity, while the other nodes sit idle.

You can find hot keys in these ways:

  • redis-cli --hotkeys. It needs an LFU (Least Frequently Used) eviction policy.
  • MONITOR, run for a short time only, to sample commands.
  • Metrics collected in your client code.

Fixes:

  • Cache the value inside each app instance's memory for a few seconds. This removes most reads from Redis.
  • Use Redis client-side caching with CLIENT TRACKING. It does the same thing, and the server pushes a message to clients when the value changes, so they drop their copy.
  • Copy the key into N copies, like post:1:0 to post:1:9. Read a random copy, and update all copies on every write.
  • Send reads to replicas.
  • For counters that get many writes, split the counter into several sharded counters. Add them together when you read.
  • One key lives on one node, so adding shards does not help.
  • Find hot keys with --hotkeys, metrics, or a short MONITOR run.
  • Use a short-lived in-process (L1) cache or Redis client-side caching.
  • Spread copies of the key across slots, or read from replicas.
  • Split counters that get many writes into sharded counters.