Explanation of Expiration and Elimination Strategies of Redis from Entry to Master [Advanced]


insert image description here

0. Preface

There is a lot to explore when it comes to expiration and eviction strategies in Redis. The following is a detailed explanation of Redis expiration and elimination strategies, I hope it will be helpful to you.

Redis expiration policy:

The expiration policy in Redis refers to how the system handles these expired key-value pairs after the lifetime of the key-value pairs set in Redis expires. Redis uses two main expiration strategies: regular deletion and lazy deletion.

1. Periodic deletion (timer mode):

Every once in a while, Redis will actively check some key-value pairs with expiration time set, and then delete the keys that have expired. This process is realized through the timer mechanism inside Redis. The advantage of the periodic deletion strategy is that the memory usage is relatively balanced, but the disadvantage is that there may be a large number of expired but not yet deleted keys in the memory.

2. Lazy deletion (check method when accessing):

When a client accesses a key, Redis will first check whether the key is expired, and delete the key if it is expired. The advantage of this method is that it can minimize memory usage. The disadvantage is that if some keys are not accessed for a long time, they may always exist in memory, resulting in memory waste.
insert image description here

Redis elimination strategy:

When the memory of Redis reaches the maximum limit, a certain elimination strategy needs to be adopted to clean up some key-value pairs to release memory space. Redis provides a variety of elimination strategies for users to choose from. Common elimination strategies include: noeviction, allkeys-lru, volatile-lru, allkeys-random, volatile-random, etc.

1. noeviction:

When the memory limit is reached, Redis will directly return an error and refuse to perform new write operations. This strategy is very important to ensure data integrity, but will cause write operations to fail.

2. allkeys-lru:

Redis will give priority to eliminating the least recently used (Least Recently Used, LRU) key-value pair, that is, the key-value pair that has not been accessed for the longest time. This strategy is suitable for most scenarios and can effectively clean up keys that have not been used for a long time.

3. volatile-lru:

Similar to the allkeys-lru policy, but only applies to keys with an expiration time set. This strategy is suitable for scenarios where expired keys need to be cleaned up first.

4. allkeys-random和volatile-random:

These two strategies are to randomly eliminate key-value pairs, that is, to randomly select keys for elimination from all keys or keys with an expiration time set. This strategy is suitable for scenarios where there is no special requirement for the elimination order.

The above are just some common cases of Redis expiration and elimination strategies. In actual applications, different strategies may be selected according to specific needs. In addition, it should be noted that when Redis executes the elimination strategy, it will try to ensure that the expired keys set by the user will not be deleted to ensure data integrity.

insert image description hereHello everyone, I'm Freezing Point, today's Redis expiration and elimination strategy is explained in detail, and that's all the content. If you have questions or opinions, you can leave a message in the comment area.

Guess you like

Origin blog.csdn.net/wangshuai6707/article/details/131745737