Large keys and hot keys can degrade service performance, cause request timeouts, or even lead to system failures. This topic describes how to quickly find and optimize large and hot keys. It also analyzes their causes and effects, and provides preventive measures to reduce their impact on your business.
Step 1: Quickly find large and hot keys
Alibaba Cloud console tools
Tair and Redis provide the Top Key Statistics and Offline Full Key Analysis features in the console to help you quickly find large and hot keys.
Method | Limits | Description | Procedure |
Top Key Statistics (Recommended) | This feature is supported only by Redis Open-Source Edition 5.0 or later, and memory-optimized and persistent memory instances of Tair (Enterprise Edition). |
|
|
This feature is not supported for disk-based instances. |
|
If your instance does not support these features, you can use the following methods.
Other methods to find large and hot keys
Step 2: Optimize large and hot keys
Large keys
Solution | Scenarios | Recommended actions |
Clean up expired data | A large amount of expired data has accumulated, such as uncleaned incremental data in a HASH. | You can use the HSCAN command with the HDEL command to clean up invalid data. This prevents the instance from being blocked, which can occur when you clean up a large amount of data at once. |
Compress large keys | Compressible data such as JSON and XML text data, including logs and configurations. |
Note Compression and decompression operations consume extra CPU resources and may affect processing performance. |
Split large keys | Frequently accessed HASH, ZSET, and other data structures, such as leaderboards. |
Splitting large keys can effectively prevent data skew. |
Offload large keys | Large files or Binary Large Objects (BLOBs) of the String type. | You can store unsuitable data in other storage systems, such as OSS, and delete it from the instance.
|
Hot keys
Solution | Scenarios | Recommended actions |
Replicate hot keys in a cluster architecture | A hot key is stored as a whole in a single shard, and requests cannot be distributed by migrating partial data. | Copy the hot key and migrate the replicas to other data shards. For example, copy a hot key named `foo` to create three identical keys named `foo2`, `foo3`, and `foo4`. Migrate these three keys to other data shards to relieve the pressure on the single data shard with the hot key. Note The disadvantage of this solution is that you must modify your code to maintain multiple replicas, and it is difficult to ensure data consistency between them. For example, an update operation must be synchronized across all replicas. Use this solution as a temporary measure to mitigate urgent issues. |
Read-heavy and write-light workloads | If the read request load is still high after you enable this feature, add more read-only nodes to further relieve the load. Note In scenarios with extremely high request volumes, primary/secondary synchronization will inevitably have latency, which can cause you to read dirty data. Therefore, do not enable read/write splitting in scenarios with high read and write pressure and strict data consistency requirements. |
Step 3: Prevent large and hot keys from affecting your business
Causes of large and hot keys
In Tair and Redis, the minimum unit for data distribution is a key. A single key is stored in a specific data shard and is not split. Factors such as insufficient business planning, accumulation of invalid data, and sudden increases in access volume can all lead to the generation of large and hot keys in an instance. Examples include the following:
Category | Cause |
Large key |
|
Hot key |
|
Effects of large and hot keys
Category | Effect |
Large key |
|
Hot key |
|
Prevention strategies
Strategy | Description |
Set reasonable alert thresholds for metrics such as CPU usage, memory usage, and Connections. For example, set an alert for when memory usage exceeds 70% or when memory grows by more than 20% in one hour. When an alert is triggered, follow the instructions in Step 1 and Step 2 of this topic to locate and optimize large and hot keys. This resolves the issue before it affects your business. | |
Use Tair (Enterprise Edition) to avoid cleaning up invalid data | For scenarios involving large keys of the hash type, Tair (Enterprise Edition) provides an enhanced data structure, TairHash. It supports setting an expiration time and version for each field. Using TairHash correctly, you can significantly reduce O&M workload, simplify business code complexity, and effectively handle issues caused by large and hot keys. |