You can modify the hz parameter to change the frequency how ApsaraDB for Redis runs background tasks to delete expired keys and close client connections that are timed out.
Correlation between background tasks and the hz parameter
To regularly check the status of resources and services and take actions based on specified policies, ApsaraDB for Redis calls an internal function to run background tasks, such as the following:
- Calculate the least recently used (LRU) information and delete expired keys.
- Close client connections that are timed out.
- Perform incremental rehashing on hash tables.
- Trigger BGSAVE/AOF rewrites.
- Update statistics.
ApsaraDB for Redis runs background tasks to ensure service availability. ApsaraDB for Redis uses the hz parameter to control the frequency how background tasks are executed. The default value of this parameter is 10, indicating that ApsaraDB for Redis runs background tasks 10 times per second.
ApsaraDB for Redis runs background tasks to delete expired keys. The process is as follows:
- ApsaraDB for Redis randomly selects 20 keys for which a Time to Live (TTL) is specified, and checks whether the selected keys are expired.
- Expired keys are deleted.
- If more than 25% of the selected keys are expired, ApsaraDB for Redis runs the background task again.
Assume that there are a large number of expired keys or a sharp increase in expired keys and the rate at which ApsaraDB for Redis deletes expired keys is slow. The remaining expired keys occupy a large amount of memory and may affect the performance of ApsaraDB for Redis. To resolve this issue, increase the value of the hz parameter, and the background task is executed more frequently.
Valid values and suggested settings for the hz parameter
Valid values of the hz parameter: 1 to 500. If you increase the value of the hz parameter, background tasks are executed more frequently, but the CPU usage of ApsaraDB for Redis also increases. You can use the default value 10 in most cases. If you want to run certain background tasks more frequently, set a value between 10 and 100. We recommend that you do not set the hz parameter to a value greater than 100, because this may cause a sharp increase in the CPU usage.
Set the hz parameter in the ApsaraDB for Redis console
- Log on to the ApsaraDB for Redis console.
- In the top navigation bar, select the region where the instance is deployed.
- On the instance list page, click Instance ID or Manage in the Actions column.
- On the Instance Information page, click System Parameters in the left-side navigation pane.
- On the page that appears, find the hz parameter and click Modify in the Action column.
- In the hz Policy dialog box that appears, follow these steps:
- Specify the value of the hz parameter based on your needs.
- Click OK.
Call API operations to set parameters
You can also call the ModifyInstanceConfig operation to set parameters.