Detecting and Handling Big Keys and Hot Keys
Definitions of Big Keys and Hot Keys
The definitions are for reference only. The actual service scenarios must be considered when you define big keys and hot keys.
Term |
Definition |
---|---|
Big key |
There are two types of big keys:
|
Hot key |
A hot key is most frequently accessed, or consumes significant resources. For example:
|
Impact of Big Keys and Hot Keys
Category |
Impact |
---|---|
Big key |
Instance specifications fail to be modified. Specification modification of a Redis Cluster instance involves rebalancing (data migration between nodes). Redis has a limit on key migration. If the instance has any single key bigger than 512 MB, the modification will fail when big key migration between nodes times out. The bigger the key, the more likely the migration will fail. |
Data migration fails. During data migration, if a key has many elements, other keys will be blocked and will be stored in the memory buffer of the migration ECS. If they are blocked for a long time, the migration will fail. |
|
Cluster shards are unbalanced.
|
|
Latency of client command execution increases. Slow operations on a big key block other commands, resulting in a large number of slow queries. |
|
Flow control is triggered on the instance. Frequently reading data from big keys exhausts the outbound bandwidth of the instance, triggering flow control. As a result, a large number of commands time out or slow queries occur, affecting services. |
|
Master/standby switchover is triggered. If the high-risk DEL operation is performed on a big key, the master node may be blocked for a long time, causing a master/standby switchover. |
|
Hot key |
Cluster shards are unbalanced. If only the shard where the hot key is located is busy processing service queries, there may be performance bottlenecks on a single shard, and the compute resources of other shards may be wasted. |
CPU usage surges. A large number of operations on hot keys may cause high CPU usage. If the operations are on a single cluster shard, the CPU usage of the shard where the hot key is located will surge. This will slow down other requests and the overall performance. If the service volume increases sharply, a master/standby switchover may be triggered. |
|
Cache breakdown may occur. If Redis cannot handle the pressure on hot keys, requests will hit the database. The database may break down as its load increases dramatically, affecting other services. |
Big keys and hot keys can be avoided through proper design. For details, see Suggestions on Using DCS.
Detecting Big Keys and Hot Keys
Method |
Description |
---|---|
Through Big Key Analysis and Hot Key Analysis on the DCS console |
|
By using the bigkeys and hotkeys options on redis-cli |
|
Searching for big keys using Redis commands |
If there is a pattern of big keys, for example, the prefix is cloud:msg:test, you can use a program to scan for keys that match the prefix, and then run commands to query the number of members in the key and query the key sizes to find big keys.
CAUTION:
This method consumes a large number of computing resources. To ensure service running, do not use this method on instances with heavy service pressure. |
Searching for big keys using redis-rdb-tools |
redis-rdb-tools is an open-source tool for analyzing Redis RDB snapshot files. You can use it to analyze the memory usage of all keys in a Redis instance. To use this method, you must export the RDB file of an instance on the Backups & Restorations page of the DCS console.
CAUTION:
This method does not affect service running, but is not as timely as online analysis. |
Optimizing Big Keys and Hot Keys
Category |
Method |
---|---|
Big key |
Split big keys. Scenarios:
|
Store big keys on other storage media. If a big key cannot be split, it is not suitable to be stored in Redis. You can store it on other storage media, such as SFS or other NoSQL databases, and delete the big key from Redis.
CAUTION:
Do not use the DEL command to delete big keys. Otherwise, Redis may be blocked or even a master/standby switchover may occur. The UNLINK command can be used to delete big keys in Redis 4.0 and later. |
|
Set appropriate expiration and delete expired data regularly. Appropriate expiration prevents expired data from remaining in Redis. Due to Redis's lazy free, expired data may not be deleted in time. If this occurs, scan expired keys. |
|
Hot key |
Split read and write requests. If a hot key is frequently read, configure read/write splitting on the client to reduce the impact on the master node. You can also add replicas to meet the read requirements, but there cannot be too many replicas. In DCS, replicas replicate data from the master in parallel. The replicas are independent of each other and the replication delay is short. However, if there is a large number of replicas, CPU usage and network load on the master node will be high. |
Use the client cache or local cache. If you know what keys are frequently used, you can design a two-level cache architecture (client/local cache and remote Redis). Frequently used data is obtained from the local cache first. The local cache and remote cache are updated with data writes at the same time. In this way, the read pressure on frequently accessed data can be separated. This method is costly because it requires changes to the client architecture and code. |
|
Design a circuit breaker or degradation mechanism. Hot keys can easily result in cache breakdown. During peak hours, requests are passed through to the backend database, causing service avalanche. To ensure availability, the system must have a circuit breaker or degradation mechanism to limit the traffic and degrade services if breakdown occurs. |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot