Redis memory elimination mechanism and process expired Key

  "Forever and sometimes do, sorrow everlasting." Poetry! Good poetry ah! Even forever, there will always be at the end, then, Redis memory is not also sometimes do it? The answer is yes. Then, when the Redis memory is full, new requests again, how can we do it? At this time, everyone should understand Redis memory to eliminate the policy, understand the relevant knowledge, the will be able to understand the "Redis memory sometimes do," what will happen.

Redis memory elimination mechanism

  Redis memory elimination mechanism means that when memory usage reaches the upper limit (configurable by maxmemory, 0 for no limit, that limit server memory), according to certain algorithms to determine what data is eliminated to ensure that the new data is stored.

  Common memory elimination mechanism is divided into four categories:

  1. LRU: LRU is Least recently used, meaning the least recently used, simply means that deleting the least recently accessed data from a database, the algorithm think, you do not have long-term data, then the probability is very small will visit again , and out of the data has not been used for the longest time, only the time-related.

  2. LFU: LFU is Least Frequently Used, the least frequently used meaning, simply means that, out of a period of time, using the minimum number of data related to the frequency and time.

  3. TTL: Redis, some data is set an expiration time, and this part of the data set the expiration time, the algorithm is to solve the object. If you expire soon, sorry, my memory is not enough now, and anyway, you have to retire, early give you a ride, you get rid of it.

  4. Random out: life and death, rich in the day, whether or not to be killed, depends providence.

  Can be configured by maxmemroy-policy specifically elimination mechanism, read many articles online that only six kinds, in fact, there are eight kinds, you can see Redis5.0 configuration file, there are instructions above:

  1. volatile-lru -> find has set the expiration time of data collection, the least recently used data (access to) kill.
  2. volatile-ttl -> find has set the expiration time of data collection, the data is about to expire kill.
  3. volatile-random -> find has set the expiration time of the data set, carried out indiscriminately attack, kill random data.
  4. volatile-lfu -> expiration time has been set to identify a data set, the period of time using a minimum number of data kill.

  5. allkeys-lru -> similar to the first one, a data set from the data set the expiration time of all data changes.
  6. allkeys-lfu -> the fourth and similar data sets from the data set the expiration time of all data changes.
  7. allkeys-random -> the first three similar data sets from the data set the expiration time of all data changes.

  8. no-enviction -> do not do anything, error, insufficient memory to tell you, so that the benefits can ensure that data is not lost, and this is out of the system default policy.

Redis expires Key Clear Strategy

  Redis we have stored in the data set the expiration time, then these data when it expires, Redis is how to destroy them out of it? We work together to explore. Here are three cleaning strategies:

  Inert Delete: When visiting Key, only to judge whether it has expired, expired, directly kill. In this way very friendly on the CPU, but if long-term without a key, there has been a memory, it will be a memory waste.

  Regularly delete: Setting key expiration time, create a timer reaches the point when the expiration time, perform the removal of the Key immediately, this approach is most unfriendly.

  Deleted regularly: from time to time, to conduct an inspection of the data, which deletes expired Key, as to how much you want to delete expired Key, check how much data, decided by the algorithm. For example to facilitate understanding: 100 randomly checks Redis expiration data per second, the data deletion check all Key has expired, if expired Key proportion greater than 25% of the total, is more than 25, repeat the above check operation.

  Redis server actually uses inert and regularly delete delete two strategies: deleted by the combined use of these two strategies can be good in the rational use of CPU and strike a balance between avoiding a waste of memory.

  Well, knowledge introductions, I hope this article can help you!

Guess you like

Origin www.cnblogs.com/maguanyue/p/12090414.html