This topic describes the default policies for automatically deleting expired keys and the methods for manually deleting expired keys in ApsaraDB for Redis.
Background information
If your instance has data expiration policies and more keys than expected, or a large number of keys expire at a specific time point or within a specific time range (for example, hundreds of thousands of keys expire within 1 to 2 hours), your instance may contain excessive expired keys to be deleted.
Default policies
ApsaraDB for Redis provides the following default policies to automatically delete expired keys:
Detect and delete: The system regularly detects and deletes expired keys.
Access and delete: When you access an expired key, the system deletes the key.
Forcefully evict: When the memory usage of an instance reaches the
Maxmemory
value, operations are blocked, keys are evicted, or an out-of-memory error is triggered. In this case, ApsaraDB for Redis uses thevolatile-lru
policy. For more information, see themaxmemory-policy
parameter in the Supported parameters topic.
The preceding policies involve no manual operations.
Manually delete expired keys
You can also manually delete expired keys by using one of the following methods:
Delete data by using the Clear Data feature in the ApsaraDB for Redis console. For more information, see Delete data.
WarningThis method consumes the least amount of time yet more instance resources. This may affect your online business. Proceed with caution. We recommend that you perform this operation during off-peak hours.
Set
hz
to a greater value to clear expired keys more frequently. For more information, see Enable dynamic frequency control for background tasks.ImportantThe greater the value is, the more CPU resources the instance consumes. Proceed with caution.
We recommend that you increase the value in increments of 10 and observe the CPU utilization of the instance to prevent excessive loads on the CPU.
Configure a time-to-live (TTL) expiration policy for keys based on your needs to prevent long-term storage of irrelevant keys in your database.
References
Applicable scope
ApsaraDB for Redis
Tair