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

What are the expiration policies of Redis?

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

Share

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

This article introduces the relevant knowledge of "what are the expiration strategies of Redis". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

When using redis, you usually set an expiration time, but of course, some do not set an expiration time, that is, it will not expire forever. When the expiration time is set, how does redis determine whether it expires and what policy is used to delete it.

Set expiration time

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

The unique way of setex (String key, int seconds, String value) strings

Except for the unique method of setting the expiration time of the string itself, all 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 the three expiration policies of persist key to delete it regularly.

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.

Advantages:

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

Key is not deleted when it expires. Every time you get a value through key, check whether it expires. 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 do not delete it at this time, we will get the expired key)

Disadvantages:

If a large amount of key is not obtained for a long time after the timeout, then a memory leak may occur (useless garbage takes up a lot of memory)

Delete periodically

Delete expired key operation at regular intervals

Advantages:

By limiting the duration and frequency of delete operations, we can reduce the consumption of CPU time by delete operations-- 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 better to "delete regularly" (it will cause a certain amount of memory consumption, but not as lazy as lazy ones).

In terms of CPU time-friendliness, it is not as good as "lazy delete" (compare and delete operations on a regular basis, cpu is not as good as lazy, but better than timing)

Difficulties:

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.

Description:

Memcached only uses lazy deletions, while redis uses both lazy deletes 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.

Delete the process periodically

To put it simply, random deletion of each library with a specified number of libraries is less than or equal to a 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 currentdb 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 currentdb is 11. The next periodic deletion starts traversing from the 11th library, assuming that currentdb is equal to 15, and then the traversal starts from library 0 (at this time currentdb==0)

This is the end of the content of "what are the expiration policies of Redis". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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