Video in progress
One very popular key (a hot key) overloaded a Redis node. How do you detect it and what do you do?
Adding nodes to the cluster doesn't help, because a hot key lands on one shard. To detect it, you look at redis-cli --hotkeys or client metrics; the fixes are a local cache, splitting the key into several copies, or making the value smaller.
📖 Answer
The symptom: CPU at 100% on one node of the Redis Cluster and 20% on the others. Network traffic is high on that node too. The cause is usually the home page configuration, a popular product, or a single key during a big promotion.
Detection:
redis-cli --hotkeys(works if the LFU eviction policy is enabled);redis-cli --bigkeys— large values cause a similar problem;- a metric in the application counting requests by key prefix.
Fixes, in the usual order:
-
Local cache. If the key rarely changes, a 5–10 second Caffeine cache in each instance. Requests to Redis drop to the level of the instance count.
-
Splitting into copies. Store the key in N copies and read a random one:
int replicas = 8;
String key = "promo:banner#" + ThreadLocalRandom.current().nextInt(replicas);
String value = redis.opsForValue().get(key);
Each copy lands in a different slot, that is, on a different node. On a write, though, you have to update all of them.
-
Making the value smaller. Reading a 500 KB JSON every time eats the network. Split it into pieces or fetch only the field you need (
HGET). -
Read replicas and allowing the client to read from replicas — if eventual consistency is acceptable.
Write hot keys (for example, a single counter) are solved differently: N sub-counters (counter:{i}), summed up on read.
Sign up to mark studied questions and save your progress.