Redis' caching strategy and primary key invalidation mechanism

  As a cache system, it is necessary to regularly clean up invalid data, which requires a primary key invalidation and elimination strategy.

  In Redis, keys with a lifetime are called volatile. When creating a cache, set a lifetime for a given key, and when the key expires (with a lifetime of 0), it may be deleted.

1. Some operations that affect the survival time

 The lifetime can be removed by deleting the entire key with the DEL command , or overwriting the original data with the SET and GETSET commands. That is, after modifying the value corresponding to the key and overwriting it with the same key and value, the current data Survival time is different.

   For example, executing an INCR command on a key, an LPUSH command on a list, or an HSET command on a hash table will not modify the time-to-live of the key itself. On the other hand, if you use RENAME to rename a key, the life time of the renamed key is the same as before the rename.

  Another possibility for the RENAME command is to try to rename a key with a time-to-live to another_key with a time-to-live, then the old another_key (and its time-to-live) will be deleted and the old key will be renamed another_key , so the new another_key has the same lifetime as the original key. Use the PERSIST command to remove the key's lifetime without deleting the key, making the key a persistent key again. 

2. How to update the survival time

You can execute the EXPIRE command    on a key that already has a time-to-live , and the new time-to-live will replace the old time-to-live. The precision of the expiration time has been controlled within 1ms, and the time complexity of the primary key invalidation is O(1). The
   EXPIRE command is used in conjunction with the TTL command, and TTL can check the current lifetime of the key. Returns 1 if the setting is successful; returns 0 when the key does not exist or the time-to-live cannot be set for the key.

 Maximum cache configuration

 In redis, allow users to set the maximum used memory size

server.maxmemory

The default is 0, and the maximum cache is not specified. If new data is added and the maximum memory is exceeded, redis will crash, so it must be set. 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:

  • volatile-lru: Picks the least recently used data out of the data set (server.db[i].expires) with set expiration time
  • volatile-ttl: Select the data to be expired from the data set (server.db[i].expires) that has set the expiration time
  • volatile-random: arbitrarily select data eviction from the dataset with set expiration time (server.db[i].expires)
  • allkeys-lru: pick least recently used data from dataset (server.db[i].dict) for elimination
  • allkeys-random: Randomly select data from the dataset (server.db[i].dict) for elimination
  • no-enviction: prohibit eviction of data

 Note the 6 mechanisms here,

volatile和allkeys规定了是对已设置过期时间的数据集淘汰数据还是从全部数据集淘汰数据,

后面的lru、ttl以及random是三种不同的淘汰策略,

再加上一种no-enviction永不回收的策略。

使用策略规则:

1、如果数据呈现幂律分布,也就是一部分数据访问频率高,一部分数据访问频率低,则使用allkeys-lru
 2、如果数据呈现平等分布,也就是所有的数据访问频率都相同,则使用allkeys-random

三种数据淘汰策略:

 ttl和random比较容易理解,实现也会比较简单。主要是Lru最近最少使用淘汰策略,设计上会对key 按失效时间排序,然后取最先失效的key进行淘汰

  在Redis当中,有生存期的key被称为volatile。在创建缓存时,要为给定的key设置生存期,当key过期的时候(生存期为0),它可能会被删除。

1、影响生存时间的一些操作

  生存时间可以通过使用 DEL 命令来删除整个 key 来移除,或者被 SET 和 GETSET 命令覆盖原来的数据,也就是说,修改key对应的value和使用另外相同的key和value来覆盖以后,当前数据的生存时间不同。

   比如说,对一个 key 执行INCR命令,对一个列表进行LPUSH命令,或者对一个哈希表执行HSET命令,这类操作都不会修改 key 本身的生存时间。另一方面,如果使用RENAME对一个 key 进行改名,那么改名后的 key 的生存时间和改名前一样。

  RENAME命令的另一种可能是,尝试将一个带生存时间的 key 改名成另一个带生存时间的 another_key ,这时旧的 another_key (以及它的生存时间)会被删除,然后旧的 key 会改名为 another_key ,因此,新的 another_key 的生存时间也和原本的 key 一样。使用PERSIST命令可以在不删除 key 的情况下,移除 key 的生存时间,让 key 重新成为一个persistent key 。 

2、如何更新生存时间

   可以对一个已经带有生存时间的 key 执行EXPIRE命令,新指定的生存时间会取代旧的生存时间。过期时间的精度已经被控制在1ms之内主键失效的时间复杂度是O(1),
   EXPIRE和TTL命令搭配使用,TTL可以查看key的当前生存时间。设置成功返回 1;当 key 不存在或者不能为 key 设置生存时间时,返回 0 。

 最大缓存配置

  在 redis 中,允许用户设置最大使用内存大小

server.maxmemory

默认为0,没有指定最大缓存,如果有新的数据添加,超过最大内存,则会使redis崩溃,所以一定要设置。redis 内存数据集大小上升到一定大小的时候,就会实行数据淘汰策略。

redis 提供 6种数据淘汰策略:

  • volatile-lru:从已设置过期时间的数据集(server.db[i].expires)中挑选最近最少使用的数据淘汰
  • volatile-ttl:从已设置过期时间的数据集(server.db[i].expires)中挑选将要过期的数据淘汰
  • volatile-random:从已设置过期时间的数据集(server.db[i].expires)中任意选择数据淘汰
  • allkeys-lru:从数据集(server.db[i].dict)中挑选最近最少使用的数据淘汰
  • allkeys-random:从数据集(server.db[i].dict)中任意选择数据淘汰
  • no-enviction(驱逐):禁止驱逐数据

 注意这里的6种机制,

volatile和allkeys规定了是对已设置过期时间的数据集淘汰数据还是从全部数据集淘汰数据,

后面的lru、ttl以及random是三种不同的淘汰策略,

再加上一种no-enviction永不回收的策略。

使用策略规则:

1、如果数据呈现幂律分布,也就是一部分数据访问频率高,一部分数据访问频率低,则使用allkeys-lru
 2、如果数据呈现平等分布,也就是所有的数据访问频率都相同,则使用allkeys-random

三种数据淘汰策略:

 ttl和random比较容易理解,实现也会比较简单。主要是Lru最近最少使用淘汰策略,设计上会对key 按失效时间排序,然后取最先失效的key进行淘汰

Guess you like

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