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

The setting of expired key in redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

EXPIRE key seconds

It is used to set an expiration time for a key, and the second parameter indicates the number of seconds after which the key expires. When a key expires, the key will be deleted automatically. In Redis terminology, keys with expiration times are often referred to as volatile.

When you use delete or overwrite operations on this key, the expiration time is cleaned up, including DEL, SET,GETSET, and all * STORE commands. Commands that modify key values do not change the expiration time, such as changing the IINCR of the value, adding the LPUSH of the new value to the queue, and modifying the HSET of members in the hash table.

If you just want to clear the expiration time, you can call the PERSIST command so that the key does not expire. The expiration time is an attribute of the key and does not change because the key changes its name (RENAME). When other keys are overridden with RENAME, only an override effect is passed, and the renamed key retains its expiration time.

One thing to note: calling the EXPIRE/PEXPIRE command with a non-positive number on a key or calling EXPIREAT/PEXPIREAT with a past time will remove the key directly.

Update expiration time

Call EXPIRE on a key that has an expiration time to update the expiration time of that key.

Return value

Calling EXPIRE key seconds returns 0 or 1, 0 indicates that the key does not exist, and 1 indicates that the key has been set to time out.

How does Redis handle keys with expiration time

Key with expiration time

Normally, the key for redis persists until the key is shown to be deleted (via the DEL command), or cleared due to memory constraints. Setting a timeout for a key requires additional memory to record the relevant information, and redis ensures that the key will be removed when the key expires.

Expiration time accuracy

Starting with redis2.6, the deviation in expiration time is between 0 and 1 millisecond.

Expiration time logic

The expiration time of the key uses the absolute timestamp of Unix (in milliseconds as precision). Even if the redis instance is closed, the key will expire at that unix timestamp (but will not be cleaned up immediately).

If you want to move the data from one redis to the redis on another computer by moving the RDB file, you need to make sure that the unix absolute timestamps on both computers are the same.

If you set the expiration time for the keys, then do not change your computer time casually, because redis will often check the system time, and if you adjust the time forward, there will be keys that should not expire. If you adjust the time backwards, there will be keys that should not expire.

How redis cleans up expired keys:

Redis cleans up expired keys in two ways: passive cleanup and active cleanup.

(1) passive cleanup: when accessing a key with an expiration time, if you find that the key has expired, the key will be cleared.

(2) redis conducts 10 checks per second, each of which includes:

Randomly select 20 keys with expiration time, and then delete all expired keys.

If more than 25% of the 20 keys have expired, perform step 1 again).

This is a simple probability algorithm, and we assume that the selected key can be used as a sample of all keys with expiration time. Through the above operations, we can basically determine that the ratio of timed-out keys to keys with timeout is less than 25%. So because of the uncleaned expired keys, the excess memory should also be less than 25%.

How to handle expiration cleanup in copy (replication) and AOF files

In order not to break consistency, when a key expires to clean up, the DEL operation will be synchronized with the AOF file and all replica nodes. In this way, the expiration cleanup operation is carried out by the master node, and the slave node does not handle the key expiration cleanup, but only waits for the DEL command of the master node, so that the key space of the master node and the slave node will remain the same.

These are the details of the redis key expiration settings, please pay attention to other related articles!

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

Database

Wechat

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

12
Report