When you use Tair, 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
The numbers used in the preceding examples are for reference only. You must determine whether a key is a large key or a hotkey based on the actual usage of the instance.
Term | Description | Example |
large key | The size of a key and the number of members in the key determine whether the key is considered a large key. |
|
hotkey | The frequency at which a key is requested determines whether the key is considered a hotkey. |
|
Issues caused by large keys and hotkeys
Category | Description |
Large keys |
|
Hotkeys |
|
Causes of large keys and hotkeys
Large keys and hotkeys may occur for a variety of reasons, such as incorrect use of Tair, insufficient workload planning, accumulation of invalid data, and traffic spikes.
Large keys
Incorrect use of Tair: If Tair 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 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 of Tair instances in a customized manner and identify large keys in these instances. You can view the statistics of keys in an instance, such as their memory usage, distribution, and time-to-live (TTL). You can use these statistics to optimize the instance and prevent issues such as insufficient memory and performance degradation that are caused by the improper distribution of keys. | |
Identify large keys and hotkeys by using the bigkeys and hotkeys parameters in redis-cli. |
| Redis provides the bigkeys parameter to enable redis-cli to traverse all keys in a Tair instance and return the overall statistics of keys and the largest keys of each data type. The bigkeys parameter can return statistics for keys of the STRING, LIST, HASH, SET, ZSET, and STREAM types. 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. Starting from Redis 4.0, the hotkeys parameter is provided to help you quickly identify hotkeys. Sample command: |
Analyze a specific key by using built-in commands in Redis |
| 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 Tair 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 Tair 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 Tair RDB files in a customized manner. You can analyze the memory usage of all keys in a Tair instance, and query and analyze the statistics of each key in a fine-grained manner. |
Identify hotkeys by using the MONITOR command |
| The MONITOR command that is available in Tair 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 Tair instances. We recommend that you use the MONITOR command only in special cases. |
Optimize large keys and hotkeys
Category | Optimization method |
Large keys |
|
Hotkeys |
|