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

What are the expiration policies in redis

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces what expiration strategies there are in redis, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Redis expiration policy

The redis expiration policy is: periodic deletion + lazy deletion.

The so-called periodic deletion means that by default, redis randomly selects some key with expiration time set every 100ms to check whether they have expired, and delete them if they expire.

Suppose you put 10w key in the redis and set the expiration time. If you check 10w key every few hundred milliseconds, then the redis will basically die, and the cpu load will be very high, which will be consumed on your check expired key. Note that it's not every 100ms that traverses all the key that sets the expiration time. That would be a performance disaster. In fact, redis is a random sample of key every other 100ms to check and delete.

But the problem is that regular deletions may cause a lot of expired key not to be deleted by the time, so what to do? So lazily deleted. That is to say, when you get a key, redis will check whether the key has expired if the expiration time is set. If it expires, it will be deleted and nothing will be returned to you.

When getting the key, if the key has expired at this time, it will be deleted and nothing will be returned.

But in fact, this is still a problem. What happens if you regularly delete and miss a lot of expired key, and then you don't check it in time, so you don't delete it lazily? What if a large number of expired key accumulates in memory, resulting in the depletion of redis memory blocks?

The answer is: follow the memory elimination mechanism.

Memory elimination mechanism

There are several memory obsolescence mechanisms for redis:

Noeviction: when there is not enough memory to hold new writes, new writes will report an error. This is generally unused, which is really disgusting.

Allkeys-lru: when there is not enough memory to hold newly written data, remove the least recently used key (this is the most commonly used) in the key space.

Allkeys-random: when there is not enough memory to hold newly written data, randomly remove a key from the key space. This is generally unused. Why should it be random? it must be to kill the least used key recently.

Volatile-lru: when there is not enough memory to hold newly written data, remove the least recently used key from the key space where the expiration time is set (which is generally not appropriate).

Volatile-random: when there is not enough memory to hold newly written data, a key is randomly removed from the key space where the expiration time is set.

Volatile-ttl: when there is not enough memory to hold newly written data, key with an earlier expiration time is removed first in the key space where the expiration time is set.

Handwritten LRU algorithm

You can handwrite the most primitive LRU algorithm on the spot, which is too large to be realistic.

Don't ask yourself to build your own LRU from the bottom by hand, but at least know how to use the existing JDK data structure to implement a Java version of LRU.

Class LRUCache extends LinkedHashMap {

Private final int CACHE_SIZE

/ * *

* how much data can be cached when passed in?

*

* @ param cacheSize cache size

* / public LRUCache (int cacheSize) {

/ / true means to have linkedHashMap sort according to the order of access, with the most recently visited in the header and the oldest in the tail. Super ((int) Math.ceil (cacheSize / 0.75) + 1,0.75f, true)

CACHE_SIZE = cacheSize;}

@ Override protected boolean removeEldestEntry (Map.Entry eldest) {

/ / when the amount of data in map is greater than the specified number of caches, the oldest data is automatically deleted. Return size () > CACHE_SIZE;}} about which expiration policies in redis are shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report