All Redis data structures support setting expiration time. When the time expires, Redis automatically deletes the data structure. You can imagine that there is a god of death inside Redis, always watching all the keys with expiration time set, and harvesting them as soon as their lifespan is up.
You can consider from the perspective of Death whether it cannot be processed because too many keys expire at the same time. At the same time, because Redis is single-threaded, the harvesting time will also occupy the processing time of the thread. If the harvesting is too busy, it will cause lags in online read and write instructions.
redis will put each key with an expiration time set into an independent dictionary. This dictionary will be traversed regularly to delete expired keys. In addition to scheduled traversal, it also uses a lazy strategy to delete expired keys. The so-called lazy strategy means that when the client accesses the key, redis checks the expiration time of the key and deletes it immediately if it expires. Scheduled deletion is a centralized process, while lazy deletion is a sporadic process.
Redis will perform expiration scans ten times per second by default. Expiration scans will not traverse all keys in the expiration dictionary, but use a simple greedy strategy.
Randomly select 20 keys from the expired dictionary;
Delete the expired keys among these 20 keys;
If the expired key ratio exceeds 1/4, then Repeat step 1;
At the same time, in order to ensure that expired scanning does not cause excessive cycles and cause thread stuck, the algorithm also increases the upper limit of scanning time, which will not exceed 25ms by default.
Imagine that all the keys in a large Redis instance expire at the same time, what will happen?
There is no doubt that Redis will continue to scan the expired dictionary (cycle multiple times), and will not stop until the expired keys in the expired dictionary become sparse (the number of cycles drops significantly). This will lead to obvious lags in online read and write requests. Another reason for lagging is that the memory manager needs to frequently reclaim memory pages, which will also cause a certain amount of CPU consumption.
When the client request arrives, if the server happens to enter the expired scan state, the client's request will wait for at least 25ms before processing. If the client sets the timeout period relatively short;
If the timeout is set to 10ms, a large number of connections will be closed due to timeout, resulting in a large number of exceptions on the business end. And at this time, you cannot see the slow query record from the slowlog of Redis, because slow query refers to the slow logical processing process and does not include waiting time.
The above is the detailed content of What are the redis expiration strategies?. For more information, please follow other related articles on the PHP Chinese website!