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 matters should be paid attention to when setting the expiration time of Redis key

2025-01-28 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 matters need to be paid attention to when Redis key expires". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Students familiar with Redis should know that each Key of Redis can set an expiration time, and when the expiration time is reached, the key will be automatically deleted.

Note 1. DEL/SET/GETSET and other commands will clear the expiration time

When you use DEL, SET, GETSET and other commands that overwrite the value of the key to operate a key with an expiration time set, the expiration time of the corresponding key will be cleared.

//Set mykey expiration time to 300s127.0.0.1:6379> set mykey hello ex 300OK//View expiration time 127.0.0.1: 6379> ttl mykey(integer) 294//Overwrite mykey content with set command 127.0.0.1: 6379> set mykey ollehOK//Expiration time cleared 127.0.0.1: 6379> ttl mykey(integer)-12, INCR/LPUSH/HSET commands do not clear expiration time

Using INCR/LPUSH/HSET, which modifies only the value of a key, rather than overwriting the entire value, does not clear the key expiration time.

INCR:

//Set the expiration time of incr_key to 300s127.0.0.1:6379> set incr_key 1 ex 300OK127.0.0.1:6379> ttl incr_key(integer) 291//Increment 127.0.0.1: 6379> get incr_key"2"//Query the expiration time and find that the expiration time has not been cleared 127.0.0.1: 6379> ttl incr_key(integer) 277

LPUSH:

//Add a key of type list and add a value of 1 127.0.0.1: 6379> LPUSH list 1(integer) 1//Set expiration time of 300s for list 127.0.0.1: 6379> expire list 300(integer) 1//View Expires 127.0.0.1: 6379> ttl list(integer) 292//Add value to list 2127.0.0.1:6379> lpush list 2(integer) 2//View all values of list 127.0.0.1: 6379> lrange list 0 11) "2"2) "1"//You can see that adding values to list does not clear expiration time 127.0.0.1: 6379> ttl list(integer) 2523. PERIST command clears expiration time

The PERIST command also clears the expiration time when it is used to convert a key with an expiration time set to a persistent key.

127.0.0.1: 6379> set persistent_key haha ex 300OK127.0.1:6379> ttl persistent_key(integer) 296//Make key persistent 127.0.0.1: 6379> persistent_key(integer) 1//Expiration time cleared 127.0.0.1: 6379> ttl persistent_key(integer) -1

4. Use the RENAME command to transfer the expiration time of the old key to the new key.

If you rename KEY_A to KEY_B using, for example, the RENAME KEY_A KEY_B command, the new key KEY_B will inherit all the properties of KEY_A regardless of whether KEY_B has an expiration time set.

//Set key_a to expire at 300s127.0.0.1:6379> set key_a value_a ex 300OK//Set key_b to expire at 600s127.0.0.1:6379> set key_b value_b ex 600OK127.0.0.1: 6379> ttl key_a(integer) 279127.0.0.1:6379> ttl key_b(integer) 591//rename key_a to key_b127.0.0.1: 6379> rename key_a key_bOK//the new key_b inherits the expiration time of key_a 127.0.0.1: ttl key_b(integer) 248

There is limited space here, so I won't list the cases where key_a is renamed to key_b one by one. You can try the case where key_a sets the expiration time on your computer and key_b does not set the expiration time.

5. Using EXPIRE/PEXPIRE to set the expiration time to negative or EXPIREAT/PEXPIREAT to set the expiration time stamp to past will cause the key to be deleted.

EXPIRE:

127.0.0.1: 6379> set key_1 value_1OK127.0.0.1:6379> get key_1"value_1"//Set expiration time to-1127.0.0.1: 6379> expire key_1 -1(integer) 1//Found key deleted 127.0.0.1: 6379> get key_1(nil)

EXPIREAT:

127.0.0.1: 6379> set key_2 value_2OK127.0.0.1:6379> get key_2"value_2"//Set the timestamp to the past time 127.0.0.1: 6379> expire key_2 10000(integer) 1//key deleted 127.0.0.1: 6379> get key_2(nil)6. EXPIRE command can update the expiration time

You can update the expiration time of a key by using the expire command.

//Set key_1 expiration time to 100s127.0.0.1:6379> set key_1 value_1 ex 100OK127.0.0.1:6379> ttl key_1(integer) 95//Update key_1 expiration time to 300s127.0.0.1:6379> expire key_1 300(integer) 1127.0.0.1:6379> ttl key_1(integer) 295

In Redis 2.1.3 and later, updating the expiration time of a key with an expiration time set fails using the expire command. And when using LPUSH/HSET commands to modify the value of a key with an expiration time set, Redis will delete the key.

Redis expiration policy

Have you ever thought about a problem, if there are a large number of keys in Redis, how can we efficiently find out the expired keys and delete them? Is it to traverse every key? If there are too many expired keys in the same period, will Redis cause the read and write instructions to stall because it keeps handling expired events?

Redis is single-threaded, so some time-consuming operations will cause Redis to stall, such as when Redis data volume is particularly large, use the keys * command to list all keys.

Redis actually uses a combination of lazy deletion and periodic deletion to handle expired keys.

lazy delete

Lazy deletion means that when the client accesses the key, redis checks the expiration time of the key and deletes it immediately if it expires.

This approach seems perfect, checking the key expiration time at the time of access, without using too much extra CPU resources. However, if a key has expired, if it has not been accessed for a long time, then the key will remain in memory, seriously consuming memory resources.

periodically delete

The principle of periodic deletion is that Redis will put all the keys with expiration time set into a dictionary, and then check the expiration time of some random keys from the dictionary every once in a while and delete the expired keys.

Redis defaults to 10 expiration scans per second:

20 random keys from an expired dictionary

Remove expired keys from these 20 keys.

If more than 25% of keys are expired, repeat step 1.

At the same time, in order to ensure that there is no excessive cycle, Redis also sets the upper limit of scanning time, which will not exceed 25ms by default.

"Redis key expiration time needs to pay attention to what" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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