Key expiration policy in Redis

1. Immediately delete:

        When setting the expiration time of the key, a callback event will be created, and when the expiration time is reached, the callback event will be automatically executed to delete the key. But immediate removal is the least cpu friendly.

2. Lazy deletion:

        Lazy deletion means that after a key value expires, the key value will not be deleted immediately, but will be added to the deletion dictionary ( dict and expires), and will not be checked until the next time it is used, and then it can be deleted. So the disadvantage of lazy deletion is that it will waste memory .

3. Periodically delete:

        Every once in a while, check the deletion dictionary and delete the expired keys in it.

        It can be seen that the second type is passive deletion , the first and third types are active deletion , and the first type is more real-time. Perform delete operations at regular intervals, and reduce the impact of delete operations on the CPU by limiting the duration and frequency of delete operations. On the other hand, regular deletion also effectively reduces the memory waste caused by lazy deletion.

Therefore, the expired key-value deletion strategy used by redis is: lazy deletion plus regular deletion, and the two are used together.

Guess you like

Origin blog.csdn.net/m0_62565675/article/details/131713915