SoFunction
Updated on 2025-04-06

Redis's data expiration strategy and data elimination strategy

1. Data expiration strategy

Will the Redis key be deleted immediately after it expires?

Whether to delete it immediately is determined based on Redis's data expiration strategy

Redis sets the expiration time of data. After the data expires, the data needs to be deleted from memory. You can delete it according to different rules, and these deletion rules are called data deletion strategies (data expiration policy)

Redis's expiration strategy is divided into the following two strategies

1. Lazy deletion

After setting an expiration time for a key in Redis, we ignore it. When the key is needed, check whether it has expired. If it expires, delete it; otherwise, return the key.

# Example: Set expired for the name attributeset name zhangsan 10

# Get it after it expires. If you find that the name has expired, delete the key directlyget name

Advantages and disadvantages of lazy deletion:

Advantages: CPU friendly, no need to waste time for expiration checks for many unused keys

Disadvantages: Not memory friendly. If a key expires but has not tried to get it (not used), then you will not notice that the key has been abandoned and will be saved in memory. The memory will never be released.

2. Delete regularly

Every once in a while, we check some keys (take a part of the key from a certain number of caches) to check whether it expires, and delete it if it expires.

There are two modes of regular strategy:

SLOW mode: timing task, execution frequency is 10hz (10 times in 1 second, that is, execution once in 100ms), each time does not exceed 25ms. You can adjust this frequency by modifying the hz option of the configuration file.

FAST mode: The execution frequency is not fixed, but the interval between the two deletions will not be less than 2ms, and each time will not exceed 1ms.

Pros and cons of regular deletion:

Advantages: The impact of the deletion operation on the CPU can be reduced by limiting the duration and frequency of the deletion operation being performed. In addition, regular deletion can also effectively release the memory usage of expired keys

Disadvantages: If the adjustment is too fast, it may affect the CPU resources. Keep checking whether the key has expired; if the adjustment is too slow, the effect will not be achieved.

The actual expired deletion strategy used by Redis is actually: lazy deletion + regular policy. Both strategies are used simultaneously.

2. Data elimination strategy

1. Data elimination strategy concept

The concept of data elimination strategy and data elimination strategy are different, and can be considered as a processing strategy under abnormal circumstances.

Data elimination strategy refers to: when there is not enough memory in redis, add a new key (new data) to redis, then redis will delete the data in memory according to certain rules. This data deletion rule is called memory elimination strategy.

2. 8 data elimination strategies

When memory is insufficient, redis supports the following 8 data elimination strategies to choose which keys to delete: (No need to remember so much, know that there are default noeviction, LRU and LFU lines)

1. noeviction: When memory is insufficient, no key is eliminated, but new data is not allowed. This is the default strategy.

2. volatile-ttl: For keys with TTL (expiration time) set, compare the TTL of these keys. The smaller the expiration remaining time, the more you are eliminated.

3. allkeys-random: Randomly eliminated from all keys (random??? Delete the kind of "It takes a long time to find out from the database, and it is a hot key again" and be honest, and directly cache and break down)

4. volatile-random: Randomly eliminate keys with TTL (expiration time)

5. Allkeys-lru: Eliminate all keys based on LRU algorithm

6. volatile-lru: For keys with TTL (expiration time) set, they are eliminated based on the LRU algorithm.

7. Allkeys-lfu: Eliminate all keys based on LFU algorithm

6. volatie-lfu: For keys with TTL (expiration time) set, they are eliminated based on the LFU algorithm.

3. What are LRU algorithms and LFU algorithms?

LRU (Least Recently Used): Least (Least Recently)

Least recent use: use the current time, subtract the last access time of the key. The larger the value, the higher the priority of elimination.

For example: key1 was visited once before 3s, and key2 was visited once before 9s, so the deleted key2

LFU (Least Frequently Used): Frequently

Minimum frequency usage: count the access frequency of each key over a period of time. The smaller the frequency, the higher the priority of elimination.

For example: key1 has been visited 4 times in the last 5s, key2 has been visited 9 times in the last 5s, and the deleted key1

4. Data elimination strategy-use suggestions

1. Priority is given to the alkeys-lru strategy. Take advantage of the LRU algorithm to leave the most recently accessed data in cache. If the business has obvious distinction between hot and cold data, it is recommended to use it.

2. If the data access frequency in the business is not very different and there is no obvious difference between hot and cold data, it is recommended to use alkeys-random and randomly choose to eliminate it.

3. If there is a requirement for top-up in the business, you can use the volatile-lru strategy. At the same time, the top-up data does not set the expiration time, and these data will not be deleted, which will eliminate other data that set the expiration time.

4. If there is short-term and high-frequency access in the business, you can use the allkeys-lfu or volatile-lfu strategy.

For example: If there is 10 million data in the database and only 200,000 pieces of data can be cached in Redis, how can we ensure that the data in Redis are all hot data?

Use allkey-lru strategy to select the least used data recently and eliminate it. What is left behind is definitely the most frequently visited hot data recently.

The redis in my previous company saved some simple configuration items, and I didn't see any elimination strategy to configure. I should use the default noeviction, which has extremely low memory usage and only used dozens of MB.

This is the end of this article about Redis’s data expiration strategy and data elimination strategy. For more related Redis data expiration strategy and data elimination strategy, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!