Home > Hosting > Server

What is the reason why the redis expired key is not released

2023-01-12 12:08:52

<p style="text-align: left;">This article mainly explains why the redis expired key is not released. Interested friends may wish to take a look. The method described in this article is simple, quick and practical. Let the editor take you to learn the reason why the redis expired key is not released!</p><p style="text-align: left;"><strong>Background:</strong></p><p style="text-align: left;">1, redis version 5.0;&lt; /p&gt;</p><p style="text-align: left;">2. The memory of the 32G redis cloud server continues to increase, and the memory occupied by expired keys has not been released. The memory usage is as shown in the figure below:</p><p style="text-align: left;"><img src="https://img.freeonlinedomain.com/uploads/allimg/20230112/1-23011212122AZ.jpg" alt="QQ截图20230112121117.jpg"/></p><p style="text-align: left;">Recent redis operations: 20 million never-expiring keys are randomly assigned an expiration time of 7-20 days.</p><p style="text-align: left;"><strong>Optimization process: </strong></p><p style="text-align: left;">1. Continuously observe the offline full key analysis of Alibaba Cloud for several days;</p><p style="text-align: left;">2. Discover expired keys The memory has not been released, and it has increased from 2G to 5G;</p><p style="text-align: left;">3. Analyze the reason why the expired key memory is not released;</p><p style="text-align: left;">4. Adjust the redis hz parameter from 10 to 30, and found No change;</p><p style="text-align: left;">5. Analyze the redis source code.</p><p style="text-align: left;">6. Conclusion: Redis memory is not released Reason: Recently, 20 million keys have been set with an expiration time, resulting in a garbage collection hit rate of less than 25%, and the expired keys will not be repeated. (Source code file: expire.c, activeExpireCycle function).</p><p style="text-align: left;">hz Explanation</p><p style="text-align: left;"><img src="//img.freeonlinedomain.com/uploads/allimg/20230112/1-23011212142B14.jpg" alt="456.jpg"/></p><p style="text-align: left;">7. Final processing method: no operation is required, as the number of expired keys increases, the hit rate increases, and the memory It will be released eventually (if you want to speed up the setting of the hz parameter, the larger the faster, but it will consume more CPU resources, you can refer to the Alibaba Cloud documentation).</p><p><br/></p><p><br/></p>


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us