Redis Big Key and Hot Key
Big Key
In Redis, a “big key” is a key that takes up a relatively large amount of memory
There is no guidance of how to define big key but generally, below are the criteria of big key
- A String value larger than 1 MB
- A Hash, List, Set, or ZSet with more than 5,000 elements
A big key might look like this
# A huge string
SET article:content:12345 "This is the content of an article that's over 100,000 characters long..."
# A set stuffed with millions of users
SADD hot_users user:1 user:2 user:3 ... user:5000000
Big Key may cause:
- Uneven memory distribution. In cluster mode, different slots are assigned to different instances. If big keys all map to one instance, data is unevenly distributed, which also hurts query performance.
- Blocking other commands. Redis executes commands on a single thread, so operations on a big key take a long time and block other commands.
- Heavy network load. Big keys consume a lot of resources during network I/O, so fetching them generates heavy traffic. This can slow down or even block network transmission. For example, a 2 MB key requested 1,000 times means 2,000 MB of traffic
Solution: Store only the data you actually need. Low-frequency data such as users’ detailed addresses or order history doesn’t need to be crammed into Redis; query the database when it’s needed. Prevent big keys at the source, for example by caching only the top 100 popular comments instead of the whole comment list.
How to find big key
redis-cli --bigkeys -i 0.1
-i 0.1 means the scan pauses for 0.1 seconds after every 100 keys, so it doesn’t put too much load on the production server.
Hot Key
A hot key is a key that gets accessed extremely frequently such as flash-sale product page where hundreds of thousands of requests per second all hit the same key. Redis processes commands on a single thread, so a hot key can max out the CPU of a single node and leave every other request waiting.
The core idea for solving hot keys is to spread the load:
- Split the hot key. Make several copies of the key. For example, split
product:12345intoproduct:12345_0,product:12345_1, andproduct:12345_2, and pick one per request by taking the user ID modulo the number of copies. The copies land on different nodes, so the load is distributed. - Multi-level caching. Add a local cache in front of Redis, so hot data is read straight from memory without touching the network.
- Read/write splitting. Configure several replicas, send reads to the replicas, and let the primary handle only writes. Read traffic for a hot key can be spread across 5 to 10 replicas, which divides the per-node load by up to 10.
- Rate limiting and degradation. Rate-limit hot keys at the gateway or client. Requests over the threshold get fallback data or a friendly message right away. It’s better for some users to see “Too popular right now, try again later” than for the whole service to go down.
How to detect hot key
- redis-cli –hotkeys
- The MONITOR command shows every command as it executes in real time, and a script can count key frequencies from i
- Add counting logic to the Redis client SDK so each operation reports the key and its latency to Kafka. On the backend, Flink aggregates the data in real time and raises an alert when a key’s QPS crosses a threshold.