Expiry policies
If we set the expiration time of key for 1 minute, how does Redis delete this key after 1 minute?
The Redis expiration policy adopts the lazy deletion + periodic deletion policy.
1. Lazy deletion
When the expiration time of a key is set, the client checks whether the key expires each time it accesses the key. If the key expires, the client deletes the key directly.
2. Delete periodically
When the expiration time of a key is set, each time the client accesses the key (read and write), it checks whether the key is expired. If the key is expired, it is deleted directly (this is passive deletion). But there are some key data access to only one or a cold, so need to delete, 10 times per second by default Redis detection, detection of object is all set the expiration time key collection, each time from this collection of random testing 20 key to see whether they expire, direct delete if expired, if the proportion of key expired more than a quarter, Repeat the procedure (the greedy algorithm). At the same time, in order to ensure that the expired scan will not be excessively looped, resulting in thread deadlock, the algorithm also increases the upper limit of the scan time, which will not exceed 25ms by default.
What happens if all the keys in a large Redis instance expire at the same time?
The scheduled task of proactively expired key is executed in the main thread of Redis. That is to say, if a large number of expired keys need to be deleted during the process of actively expired key, then the application must wait for the execution of the expired task when accessing Redis. Redis should be able to service this client request. In this case, the application access Redis delay becomes larger.
There is a time limit of 25ms for scanning. How can it cause a lag?
If 1,001 clients send requests simultaneously, and the first 1,000 requests take 25ms to execute, how long does it take for the 1,001 instructions to execute? 25000ms, 25 seconds, this is the lag time of the client, which is caused by the continuous small lag time of the server.
How do I solve the problem that a large number of key sets expire and become stuck?
Scheme 1: When setting the expiration time of the key, add a random time
redis.expireat(key, expire_time + random(300))
Copy the code
This way, Redis will not be overwhelmed by deleting too many keys in the set to block the main thread.
Solution 2: Redis 4.0 or later, enable the lazy-free mechanism
lazyfree-lazy-expire yes
Copy the code
In addition to optimizing and modifying the configuration at the business level, you can also detect this situation through operation and maintenance.
You can run the INFO command on Redis to get all the status data of the instance.
What we need to focus on here is the expired_keys item, which represents the number of expired keys that have been removed over the entire instance so far.
You need to monitor this indicator. If there is a sudden increase of this indicator in a short period of time, you need to alert it in time, and then compare it with the time point reported by the business application to confirm whether the time is consistent. If it is consistent, you can confirm that the delay caused by the expired key is really large.
Expiration policy for the slave: The slave does not perform an expiration scan. The slave passively handles expiration. When a key expires, the primary library adds a del instruction to the AOF file and synchronizes it to all secondary libraries. The secondary library deletes the expired key by executing the del instruction.