Redis data elimination mechanism

Redis data elimination mechanism

 

 

Overview

In Redis, the user is allowed to set the maximum used memory size server.maxmemory, which is useful in the case of memory constraints. For example, 4 Redis service points are deployed on an 8G machine, and each service point is allocated a memory size of 1G, which reduces the memory shortage and obtains more stable services. When the size of the Redis memory data set rises to a certain size, the data elimination strategy will be implemented.

 

 

 

 

Redis provides 6 data elimination strategies

 

  1. volatile-lru: Pick the least recently used data out of the data set (server.db[i].expires) with set expiration time
  2. volatile-ttl: Select the data to be expired from the data set (server.db[i].expires) that has set the expiration time
  3. volatile-random: arbitrarily select data eviction from the dataset with set expiration time (server.db[i].expires)
  4. allkeys-lru: pick least recently used data from dataset (server.db[i].dict) for elimination
  5. allkeys-random: Randomly select data from the dataset (server.db[i].dict) for elimination
  6. no-enviction: Prohibit eviction of data, do not eliminate data, and report an error directly

Default use: no-enviction

 

After Redis determines to evict a key-value pair, it will delete the data and publish the data change message to the local (AOF persistence) and slave (master-slave connection).

 

 

 

 

LRU data elimination mechanism

 

       Each Redis object will set the corresponding lru, that is, the time of the most recent access. It is conceivable that redisObject.lru will be updated every time the data is accessed.

 

Elimination principle:

 

       Randomly select several key-value pairs in the data set, and take out the key-value pair with the largest lru and eliminate it. Therefore, you will find that Redis does not guarantee to obtain the least recently used (LRU) key-value pair in all data sets, but only a few randomly selected key-value pairs.

 

 

 

 

TTL data elimination mechanism

 

       The Redis dataset data structure stores the key-value pair expiration time table, namely redisDb.expires. When using the SET command, there is an option for the key-value pair timeout time.

 

Elimination principle:

 

       Randomly select several key-value pairs from the redisDB.expires table of expiration time, and take out the key-value pair with the largest ttl for elimination. You will also find that Redis does not guarantee to obtain the fastest expired key-value pair in all the expiration time tables, but only a few randomly selected key-value pairs.

 

 

 

 

When to start phasing out data

 

       When Redis executes a command for each service client, it will detect whether the memory used is excessive. In case of excess, data elimination is performed.

 

       The data elimination strategy is actually just one of the data deletion strategies. From a general perspective, you should look at the redis data deletion strategy

 

 

 

There are three data deletion policies:

 

  1. Passive deletion: Only when the key is operated (such as GET), Redis will passively check whether the key expires, if it expires, delete it and return NIL.
  2. Active deletion: Periodically delete expired data, which can be configured
  3. When the currently used memory exceeds the maxmemory limit, the data elimination strategy is triggered

Passive delete features:

 

  1. This deletion strategy is friendly to the CPU, and the deletion operation will only be performed when necessary, and will not waste unnecessary CPU time on other expire keys.
  2. But this strategy is not memory friendly, a key has expired, but will not be deleted before it is operated, still occupying memory space. If a large number of expired keys exist but are rarely accessed, it will cause a lot of wasted memory space. The expireIfNeeded(redisDb *db, robj *key) function is located in src/db.c.

 

 

   

refer to 

http://wiki.jikexueyuan.com/project/redis/data-elimination-mechanism.html

http://www.tuicool.com/articles/vQNZJb3

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326478432&siteId=291194637