The key is set to expire, but how is it handled when it expires? There are usually three kinds of rules:
timed Delete: after the key expiration time is set, a timer is started and the key is deleted when the timer expires.
Lazy Removal: Do not create a timer, expired also do not do processing, when reading the key again to determine whether to expire, if the expiration is deleted and return null, if there is no expiration on the return value.
Delete regularly: This is very well understood, after a period of cleanup, as to whether to clear all not necessarily, because if the key is too large, the overall cleanup will consume a lot of CPU time, so periodic cleanup may be part of a periodic purge, and then complete the overall cleanup during a periodic purge cycle of a whole.
Strategy |
Advantages |
Disadvantages |
Timed Delete |
Most efficient for memory space, no stale data stored |
frequent deletion of outdated data can consume more CPU Time |
Lazy Delete |
do not occupy too much of CPU Time |
There is a large amount of stale data in the database that consumes memory space |
Delete periodically |
Benefits of absorbing timing and lazy removal |
To design a good delete operation takes time and frequency of execution |
Redis uses lazy deletion and periodic deletion of both policies . This means that it periodically executes the delete expired key, but does not traverse all the keys at once, but periodically iterates through the part, traversing all over a long period. In the time period that has not yet reached the periodic deletion, the use of lazy deletion, encountered the expiration of the deletion, did not encounter the retention, left to the periodic removal of processing. This mechanism seeks a balance between memory and CPU .
The periodic delete operation is the default of 100 milliseconds, which is performed by the Servercron periodic action function. However, how long each delete operation has been and how many databases are traversed is determined by the specific algorithm.
This article is from the "Little Demon's Home" blog, so be sure to keep this source http://littledevil.blog.51cto.com/9445436/1813960
(v) Expiration key (2) Expiration key deletion