When you use Tair (Redis OSS-compatible), you may encounter performance degradation, a poor user experience, and even large-scale failures if you do not identify and handle large keys or hotkeys in a timely manner. This topic describes the causes of large keys and hotkeys, the issues that may be caused by large keys and hotkeys, and how to identify and optimize large keys and hotkeys in a timely manner.
Definitions of large key and hotkey
Term | Description |
large key | The size of a key and the number of members in the key determine whether the key is considered a large key. The following list provides some examples:
|
hotkey | The frequency at which a key is requested determines whether the key is considered a hotkey. The following list provides some examples:
|
The specific values provided in the preceding examples are for reference only and may vary based on real-world scenarios.
Issues caused by large keys and hotkeys
Category | Description |
Large keys |
|
Hotkeys |
|
Causes of large keys and hotkeys
Large keys and hotkeys may occur due to a variety of reasons, such as incorrect use of Tair (Redis OSS-compatible), insufficient workload planning, accumulation of invalid data, and traffic spikes.
Large keys
Incorrect use of Tair (Redis OSS-compatible): If Tair (Redis OSS-compatible) is used in an improper scenario, the size of a key may be larger than necessary. For example, if a STRING key is used to store a large binary file, the size of the key may be larger than necessary.
Insufficient workload planning: Before a feature is released, a failure to sufficiently plan for workloads can result in problems. For example, members may not be properly split between keys and some keys may have more members than required.
Accumulation of invalid data: This occurs when invalid data is not deleted on a regular basis. For example, the number of members of a HASH key constantly increases when invalid data is not cleared in a timely manner.
Code failures: Code failures occur on consumer applications that use LIST keys, which causes the members of the keys to only increase.
Hotkeys
Unexpected traffic spikes: Unexpected traffic spikes may occur for a variety of reasons, such as high product popularity, hot news, a large number of "likes" flooding in from the viewers of a livestream, or battles between multiple large teams in a game.
Identify large keys and hotkeys
Tair (Redis OSS-compatible) provides a variety of methods for you to identify large keys and hotkeys.
Method | Advantage and disadvantage | Description |
Use the real-time key statistics feature (recommended) |
| You can use the real-time key statistics feature to display the statistics of large keys and hotkeys in an instance in real time. You can also query the historical statistics of large keys and hotkeys that were generated within the last four days. You can use this feature to obtain key statistics such as memory usage and access frequency. Then, you can troubleshoot issues and optimize instances based on the statistics. |
| The offline key analysis feature allows you to analyze RDB backup files in a customized manner and identify large keys in instances. You can view the statistics of keys in an instance, such as the memory usage, distribution, and time-to-live (TTL) of keys. You can use these statistics to optimize the instance and prevent issues such as insufficient memory and performance degradation that are caused by improper distribution of keys. | |
Identify large keys and hotkeys by using the bigkeys and hotkeys parameters in redis-cli |
| redis-cli provides the bigkeys and hotkeys parameters, which can be used to analyze all keys in an instance by iterating through the keys. These parameters provide comprehensive statistics about the keys and also identify the largest key for each data type. bigkeys can analyze and provide information about only six types of data: STRING, LIST, HASH, SET, ZSET, and STREAM. Sample command: Note If you want to analyze only large keys of the STRING type or identify the HASH keys that have more than 10 members, the bigkeys parameter cannot fulfill your needs. |
Analyze a specific key by using built-in commands |
| The following section lists low-risk commands for analyzing keys of various data types to determine whether a key is a large key:
Note The DEBUG OBJECT and MEMORY USAGE commands consume large amounts of resources when they are run. In addition, the time complexity of these commands is O(N), which indicates that these commands may block instances. Therefore, we recommend that you do not use these commands. |
Identify hotkeys at the business layer |
| This method allows you to add code to the business layer to record requests that were sent to instances and asynchronously analyze the collected statistics. |
Identify large keys in a customized manner by using the redis-rdb-tools project |
| The redis-rdb-tools project is written in the Python programming language. redis-rdb-tools is an open source tool that can be used to analyze RDB files in a customized manner. You can analyze the memory usage of all keys in an instance, and query and analyze statistics of each key in a fine-grained manner. |
Identify hotkeys by using the MONITOR command |
| The MONITOR command can display the statistics of all requests related to an instance, including statistics about time, clients, commands, and keys. In case of an emergency, you can run the MONITOR command and export the output to a file. You can then analyze and classify the requests in the output to identify hotkeys generated during the emergency period after you disable the MONITOR command. Note However, the MONITOR command significantly degrades the performance of the instance. We recommend that you use the MONITOR command only in special cases. |
Optimize large keys and hotkeys
Category | Optimization method |
Large keys |
|
Hotkeys |
|