(v) Expiration key (2) Expiration key deletion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.