What are Redis expiration policy?

Copyright: ~ reproduced please marked, thank you - if infringement please private letter to me, I will immediately delete ~ https://blog.csdn.net/baidu_26954625/article/details/90648597

本系列内容转载自git项目advancejava

Interview questions

What are redis expiration policy? What mechanisms are out of memory? LRU handwritten code to achieve what?

There are two common problems:

• how data is written to redis gone?

Some students may encounter, often lose some data in redis production environment, get a hold of, then, it may be gone. My God, classmates, you ask this question to explain redis you will not use right ah. redis is cached, you gave when storage is not it?
Shajiao cache? When using memory cache. Memory is unlimited right, memory is very precious and limited, disks are cheap and lots of. It may be a machine on the memory of dozens of G, but there are a few T ** hard disk space. redis mainly based memory to high-performance, high-** concurrent read and write operations.
Now that memory is limited, such as redis can only use 10G, if you write the data entered, 20G, and would we supposed to? Of course, the data will get rid of 10G, 10G and then retain the data of the. What data that kill? What data retention? Of course, is to get rid of infrequently accessed data, the common data retention.

• The data clearly expired, how can they occupy the memory?

This is determined by the expiration policy redis.

Face questions analysis

redis expiration policy

redis expiration policy is: + inert periodically delete delete.
The so-called periodically delete, refers to the redis default is every 100ms on a random sample of some set an expiration time of the key, to check whether it has expired, expired delete.
Redis put a 10w assume a key, are set expiration time, you every few hundred milliseconds, checks 10w a key, that redis basically died, cpu load will be very high, consumption in your checking expired key on. Note that this 100ms to traverse all of the key is not set the expiration time intervals, as is a disaster on a performance. In fact some of the key redis are randomly selected every 100ms to check and delete.

But the problem is, periodically delete key may cause a lot of back to the time and has not been removed, it zezheng it? So it is inert deleted. This means that you get a key time, redis will check that the key if you set an expiration time does it expire? If this time has expired will be deleted and will not give you anything in return.
Get key time, if the key at this time has expired, delete, does not return anything.
But in fact this is a problem, if you delete regularly missed a lot of back key, then you have no time to investigate, also did not go inert delete, then what will happen? If the accumulation of a large number of expired key in memory, causing memory block redis exhausted, zezheng?
The answer is: go out of memory mechanisms.

Memory elimination mechanism

redis memory elimination mechanism are the following:
noeviction: When the memory is not sufficient to accommodate the new data is written, the new write operation will complain that this is generally no one with it, it was disgusting.
allkeys-lru: When the memory is not sufficient to accommodate the new data is written in the key space, remove the key the least recently used (this is the most common).
allkeys-random: When the memory is not sufficient to accommodate the new data is written in the key space randomly remove a key, this is generally not people use it, why should random, the key is certainly the least recently used to kill ah.
volatile-lru: When the memory is not sufficient to accommodate the new data is written in the space provided key expiration time, the key is removed the least recently used (this is generally not appropriate).
volatile-random: When the memory is not sufficient to accommodate the new data is written in the space provided key expiration time randomly remove a key.
volatile-ttl: When the memory is not sufficient to accommodate the new data is written in the space provided key expiration time, there is an earlier expiration date of the key priority removed.

Guess you like

Origin blog.csdn.net/baidu_26954625/article/details/90648597