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

Example Analysis of memory obsolescence Policy and expired key deletion Policy in Redis

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you the content of a sample analysis of memory elimination policies and expired key deletion policies in Redis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Redis is one of our most commonly used tools to help us build the high availability and high performance of the system.

And we all know that redis is a completely memory-based tool, which is one of the reasons why redis is fast. When we cache data in redis, the memory is always full (and memory is very expensive, so try to save it as much as possible), so try your best to cache useful data or frequently used data in redis.

So if the redis memory we are using runs out, how do we choose between the data that already exists in redis and the data we are about to store? what are we going to do with it?

Redis officials offer eight different elimination strategies.

Redis.conf is a good thing. Almost all the configurations of redis can be found here and can be operated according to the instructions in conf.

Let's take a look at the instructions on the elimination strategy in 8 in redis.conf (redis version 4.0.9 is used in this article)

Volatile-lru-> Evict using approximated LRU among the keys with an expire set.allkeys-lru-> Evict any key using approximated LRU.volatile-lfu-> Evict using approximated LFU among the keys with an expire set.allkeys-lfu-> Evict any key using approximated LFU.volatile-random-> Remove a random key among the ones with an expire set.allkeys-random-> Remove a random key, any key.volatile-ttl-> Remove the key with the nearest expire time (minor TTL) noeviction-> Don't evict anything Just return an error on write operations.# LRU means Least Recently Used (the least recently used Time) # LFU means Least Frequently Used (least frequently used, number of times) # The default is:maxmemory-policy noeviction

The above is the eight configurations and configuration instructions on the phase-out policy extracted from redis.conf. Maxmemory-policy noeviction represents the phase-out policy by default noeviction. We can modify the appropriate policy according to our business needs.

If you can't read English, read the Chinese below.

8 elimination strategies

Volatile-lru: eliminate the least used data in the data that sets the expiration time.

Allkeys-lru: eliminate the least used data from all data.

Volatile-lfu: eliminate the least frequently used data in the data that sets the expiration time.

Allkeys-lfu: eliminate the least frequently used data from all data.

Volatile-random: eliminate any random data in the data that sets the expiration time.

Allkeys-random: randomly eliminates data from all data.

Volatile-ttl: eliminates the earliest expired data in the data that sets the expiration time.

Noeviction: default policy: data is not eliminated. Exceptions will be thrown if new or modified data is added or modified, but the read operation will proceed normally and will not be affected.

The above is the elimination strategy of insufficient memory, and the other is the deletion strategy of expired keys. The two are different, so don't get confused.

Delete policy for expired keys

Timed expiration: each key that sets the expiration time needs to create a timer that will be cleared immediately when it expires. This strategy can clean up expired data immediately and is memory-friendly, but it takes up a lot of CPU resources to process expired data, thus affecting the response time and throughput of the cache.

Lazy expiration: it is only when a key is accessed that it is determined whether the key has expired or is cleared when it expires. This strategy maximizes the savings in CPU resources, but is very memory-friendly. In extreme cases, there may be a large number of expired key that are not accessed again, so that they are not cleared and take up a lot of memory.

Periodic expiration: at regular intervals, a certain number of expires dictionaries in a certain number of databases are scanned for a certain number of key, and the expired key is cleared. This strategy is a compromise between the first two. By adjusting the time interval of timing scanning and the limited time of each scan, the optimal balance between CPU and memory resources can be achieved under different conditions.

The expires dictionary saves expiration time data for all key with expiration time set, where

Key is a pointer to a key in the key space

Value is the expiration time represented by the millisecond precision UNIX timestamp of the key.

The key space refers to all the keys saved in the Redis cluster.

Thank you for reading! On the "Redis memory elimination strategy and expired key deletion strategy example analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

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

Development

Wechat

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

12
Report