Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis expiration strategy and its implementation principle

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

When we use redis, we usually set an expiration time, of course, some do not set an expiration time, that is, it will not expire forever.

When we set the expiration time, how does redis determine whether it expires and according to what strategy to delete it.

Redis sets the expiration time:

Expire key time (in seconds)-this is the most common way

Setex (String key, int seconds, String value)-unique to strings

Note:

Except for the unique method of setting the expiration time of the string itself, other methods need to rely on the expire method to set the time.

If the time is not set, the cache will never expire.

If you set the expiration time and then want the cache to never expire, use persist key

Three expiration policies:

Scheduled deletion

Meaning: while setting the expiration time of the key, create a timer for the key and let the timer delete the key when the expiration time of the key comes

Advantage: make sure the memory is released as soon as possible

Disadvantages:

If there are a lot of expired key, deleting these key will take up a lot of CPU time. When CPU time is tight, CPU can't spend all its time doing urgent things. It also needs to take time to delete these key.

The creation of timers takes time. If you create a timer for each key that sets the expiration time (there will be a large number of timers), the performance will be seriously affected.

Lazy Chinese deletion

Meaning: key is not deleted when it expires. Check whether it expires every time you get the value through key. If it expires, delete it and return null.

Advantages: deletion occurs only when the value is obtained through key, and only the current key is deleted, so the time consumed by CPU is relatively small, and the deletion has reached the point where it is necessary to do so (if we don't delete it at this time, we will get the expired key)

Disadvantages: if a large number of key are not obtained for a long time after the timeout, memory leaks may occur (useless garbage takes up a lot of memory)

Delete periodically

Meaning: delete expired key operation at regular intervals

Advantages:

By limiting the duration and frequency of delete operations, the consumption of CPU time by delete operations can be reduced-the disadvantage of dealing with "scheduled deletion"

Regularly remove the shortcomings of expired key- in dealing with "lazy deletion"

Disadvantages:

In terms of memory friendliness, it is not as good as "regular deletion" (it will cause a certain amount of memory consumption, but not as lazy as the lazy type). In terms of CPU time friendliness, it is not as good as "lazy delete" (it will be compared and deleted regularly, and cpu is not as good as lazy, but better than timing).

Difficulty: reasonably set the execution time of the deletion operation (how long each deletion will take) and the frequency of execution (how often) (this depends on the running condition of the server). Each execution time is too long or the execution frequency is too high, which is a kind of pressure on cpu.

After each periodic deletion operation is performed, you need to record which flag bit the traversal loop to, so that when the next periodic time comes, the loop traversal will start from the last location.

Note: memcached only uses lazy deletions, while redis uses both lazy deletions and periodic deletions, which is also a difference between the two (it can be seen that redis is superior to memcached)

For lazy deletion, it is not only when you get key that you check whether key expires, but also on some methods of setting key (eg.setnx key2 value2: this method is similar to memcached's add method, if the set key2 already exists, then this method returns false and does nothing; if the set key2 does not exist, then this method sets the cache key2-value2. Suppose that when you call this method, you find that key2 already exists in the redis, but the key2 has expired. If you do not delete at this time, the setnx method will directly return false, that is, the key2-value2 has not been reset successfully at this time, so it is necessary to check the expiration of the key2 before setnx executes.

Expiration policy adopted by Redis

Lazy deletion + regular deletion

Lazy deletion process:

Check whether the key is out of date when performing operations such as get or setnx

If it expires, delete the key, and then perform the appropriate action

If it does not expire, perform the corresponding operation directly.

Periodic deletion process (to put it simply, random deletion for each of the specified number of libraries is less than or equal to the specified number of expired key):

Traverse each database (that is, the number of "database" configured in redis.conf, default is 16)

Check the specified number of key in the current library (by default, each library checks 20 key, note that the loop is executed 20 times, and the loop body is described below)

If no key in the current library sets the expiration time, the traversal of the next library is performed directly.

Randomly get a key with the expiration time set, check whether the key expires, and delete the key if it expires

Determine whether the periodic deletion operation has reached the specified length of time, and if so, directly exit the periodic deletion.

For periodic deletion, there is a global variable current_db in the program to record the next library to be traversed. Suppose there are 16 libraries, and we delete and traverse 10 libraries this time, then the current_db is 11. The next periodic deletion starts traversing from the 11th library, assuming that current_db is equal to 15, and then the traversal starts from library 0 (at this time current_db==0)

Summary

In practice, if we want to design our own expiration policy, when using lazy deletion + regular deletion, it is particularly critical to control the time and frequency, which needs to be adjusted in the light of server performance, concurrency and other conditions to achieve the best.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report