Redis elimination mechanism

We all know that the expiration time can be set when redis caches, so how does redis recognize that this key has expired and clear the key?

When the memory of redis is full, why can I continue to write cache? And some keys will be cleared, what is the reason?

The above problems involve redis' expiration strategy and elimination mechanism, we can take a look.

Why should redis have a elimination mechanism?

  The existence of the redis elimination mechanism is to better use the memory, and use a certain cache loss in exchange for the efficiency of the memory.

Redis expiration strategy

  Redis has two expiration strategies, regular deletion and lazy deletion

    • Periodic deletion : Redis randomly extracts some keys with an expiration time every 100ms, checks whether they are expired, and deletes them if they expire.
    • Lazy deletion : When obtaining a key, redis checks it. If the key has an expiration time, it determines whether the expiration time has expired. If it expires, it is deleted directly and nothing is returned.

Redis memory elimination mechanism

  When redis memory is almost exhausted, redis will start the memory elimination mechanism and clear some keys to free up memory.

  Redis provides 6 data elimination strategies, which can be configured in redis.conf : maxmemory-policy noeviction

  • noeviction : prohibit eviction of data. The default configuration is this. When the memory usage reaches the threshold, all commands that cause memory application will report an error.
  • volatile-lru : Select the least recently used data from the data set with the expiration time set to be eliminated.
  • volatile-ttl : Select the data that is about to expire from the data set with the expiration time set to be eliminated.
  • volatile-random : arbitrarily select data elimination from the data set with the expiration time set.
  • allkeys-lru : Select the least recently used data from the data set and eliminate it.
  • allkeys-random : arbitrarily select data elimination from the data set.

  When Redis decides to expel a key-value pair, it will delete this data and synchronize this data change message to the local and slave.

The above reference comes from https://www.cnblogs.com/wjh123/p/11254858.html , http://mini.eastday.com/mobile/180918003550027.html#

Guess you like

Origin www.cnblogs.com/smallzhen/p/12728455.html