• Redis存储——这些数据结构原理你都懂吗?


    一、Redis内存数据结构与编码

    想要弄清楚Redis内部如何支持5种数据类型,也就是要弄清Redis到底是使用什么样的数据结构来存储、查找我们设置在内存中的数据。

    虽然我们使用5种数据类型来缓存数据,但是Redis会根据我们存储数据的不同而选用不同的数据结构和编码。

    这些数据类型在内存中的数据结构和编码有很多种,随着我们存储的数据类型的不同、数据量的大小不同都会引起内存数据结构的动态调整。

    本文只是做数据结构和编码的一般性介绍,不做过多细节讨论,一方面是关于Redis源码分析的资料网上有很多,还有一个原因就是Redis每一个版本的实现有很大差异,一旦展开细节讨论,每一个点每一个数据结构都会很复杂,所以我们这里就不展开讨论这些,只是起到抛砖引玉作用。

    1、OBJECT encoding key、DEBUG OBJECT key

    我们知道使用type命令可以查看某个key是否是5种数据类型之一,但是当我们想查看某个key底层是使用哪种数据结构和编码来存储的时候,可以使用OBJECT encoding命令。

    同样一个key,由于我们设置的值不同,Redis选用了不同的内存数据结构和编码。虽然Redis提供String数据类型,但是Redis会自动识别我们cache的数据类型是int还是String。

    如果我们设置的是字符串,且这个字符串长度不大于39字节,那么将使用embstr来编码;如果大于39字节将使用raw来编码。Redis4.0将这个阀值扩大了45个字节。

    除了使用OBJECT encoding命令外,我们还可以使用DEBUG OBJECT命令来查看更多详细信息。

    DEBUG OBJECT能看到这个对象的refcount引用计数、serializedlength长度、lru_seconds_idle时间,这些信息决定了这个key缓存清除策略。

    2、简单动态字符串(simple dynamic String)

    简单动态字符串简称SDS,在Redis中所有涉及到字符串的地方都是使用SDS实现,当然这里不包括字面量。SDS与传统C字符串的区别就是SDS是结构化的,它可以高效的处理分配、回收、长度计算等问题。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">struct sdshdr { 
    2. unsigned int len;
    3. unsigned int free;
    4. char buf[];
    5. };

    这是Redis3.0版本的sds.h头文件定义,3.0.0之后变化比较大。len表示字符串长度,free表示空间长度,buf数组表示字符串。

    SDS有很多优点,比如,获取长度的时间复杂度O(1),不需要遍历所有charbuf[]组数,直接返回len值。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">static inline size_t sdslen(const sds s) { 
    2. struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
    3. return sh->len;
    4. }

    当然还有空间分配检查、空间预分配、空间惰性释放等,这些都是SDS结构化字符串带来的强大的扩展能力。

    3、链表(linked list)

    链表数据结构我们是比较熟悉的,最大的特点就是节点的增、删非常灵活。Redis List数据类型底层就是基于链表来实现。这是Redis3.0实现。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typedef struct list { 
    2. listNode *head;
    3. listNode *tail;
    4. void *(*dup)(void *ptr);
    5. void (*free)(void *ptr);
    6. int (*match)(void *ptr, void *key);
    7. unsigned long len;
    8. } list;
    9. typedef struct listNode {
    10. struct listNode *prev;
    11. struct listNode *next;
    12. void *value;
    13. } listNode;

    在Redis3.2.0版本的时候引入了quicklist链表结构,结合了linkedlist和ziplist的优势。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typedef struct quicklist { 
    2. quicklistNode *head;
    3. quicklistNode *tail;
    4. unsigned long count; /* total count of all entries in all ziplists */
    5. unsigned int len; /* number of quicklistNodes */
    6. int fill : 16; /* fill factor for individual nodes */
    7. unsigned int compress : 16; /* depth of end nodes not to compress;0=off */
    8. } quicklist;
    9. typedef struct quicklistNode {
    10. struct quicklistNode *prev;
    11. struct quicklistNode *next;
    12. unsigned char *zl;
    13. unsigned int sz; /* ziplist size in bytes */
    14. unsigned int count : 16; /* count of items in ziplist */
    15. unsigned int encoding : 2; /* RAW==1 or LZF==2 */
    16. unsigned int container : 2; /* NONE==1 or ZIPLIST==2 */
    17. unsigned int recompress : 1; /* was this node previous compressed? */
    18. unsigned int attempted_compress : 1; /* node can't compress; too small */
    19. unsigned int extra : 10; /* more bits to steal for future usage */
    20. } quicklistNode;

    quicklist提供了灵活性同时也兼顾了ziplist的压缩能力,quicklist->encoding指定了两种压缩算法。quickList->compress表示我们可以进行quicklist node的深度压缩能力。Redis提供了两个有关于压缩的配置:

    • List-max-zipList-size:zipList长度控制;
    • List-compress-depth:控制链表两端节点的压缩个数,越是靠近两端的节点被访问的机率越大,所以可以将访问机率大的节点不压缩,其他节点进行压缩。

    对比Redis3.2的quicklist与Redis3.0,很明显quicklist提供了更加丰富的压缩功能。Redis3.0的版本是每个listnode直接缓存值,而quicklistnode还有强大的有关于压缩能力。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">LPUSH list:products:mall 100 200 300 
    2. (integer) 3
    3. OBJECT encoding list:products:mall
    4. "quicklist"

    4、字典(dict)

    dict字典是基于Hash算法来实现,是Hash数据类型的底层存储数据结构。我们来看下Redis3.0.0版本的dict.h头文件定义:

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typedef struct dict { 
    2. dictType *type;
    3. void *privdata;
    4. dictht ht[2];
    5. long rehashidx;
    6. int iterators;
    7. } dict;
    8. typedef struct dictht {
    9. dictEntry **table;
    10. unsigned long size;
    11. unsigned long sizemask;
    12. unsigned long used;
    13. } dictht;
    14. typedef struct dictEntry {
    15. void *key;
    16. union {
    17. void *val;
    18. uint64_t u64;
    19. int64_t s64;
    20. double d;
    21. } v;
    22. struct dictEntry *next;
    23. } dictEntry;

    说到Hash table有两个东西是我们经常会碰到的,首先就是Hash碰撞问题,Redis dict是采用链地址法来解决,dictEntry->next就是指向下个冲突key的节点。

    还有一个经常碰到的就是rehash的问题,提到rehash我们还是有点担心性能的。那么Redis实现是非常巧妙的,采用惰性渐进式rehash算法。

    在dict struct里有一个ht[2]组数,还有一个rehashidx索引。Redis进行rehash的大致算法是这样的:

    首先会开辟一个新的dictht空间,放在ht[2]索引上,此时将rehashidx设置为0,表示开始进入rehash阶段,这个阶段可能会持续很长时间,rehashidx表示dictEntry个数。每次当有对某个ht[1]索引中的key进行访问时,获取、删除、更新,Redis都会将当前dictEntry索引中的所有key rehash到ht[2]字典中。一旦rehashidx=-1表示rehash结束。

    5、跳表(skip list)

    skip list是Zset的底层数据结构,有着高性能的查找排序能力。

    我们都知道一般用来实现带有排序的查找都是用Tree实现,不管是各种变体的B Tree还是B+Tree,本质都是用来做顺序查找。

    skip list实现起来简单,性能也与B Tree相接近。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typedef struct zskiplistNode { 
    2. robj *obj;
    3. double score;
    4. struct zskiplistNode *backward;
    5. struct zskiplistLevel {
    6. struct zskiplistNode *forward;
    7. unsigned int span;
    8. } level[];
    9. } zskiplistNode;
    10. typedef struct zskiplist {
    11. struct zskiplistNode *header, *tail;
    12. unsigned long length;
    13. int level;
    14. } zskiplist;

    zskiplistNode->zskiplistLevel->span这个值记录了当前节点距离下个节点的跨度。每一个节点会有最大不超过zskiplist->level节点个数,分别用来表示不同跨度与节点的距离。

    每个节点会有多个forward向前指针,只有一个backward指针。每个节点会有对象*obj和score分值,每个分值都会按照顺序排列。

    6、整数集合(int set)

    int set整数集合是set数据类型的底层实现数据结构,它的特点和使用场景很明显,只要我们使用的集合都是整数且在一定的范围之内,都会使用整数集合编码。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SADD set:userid 100 200 300 
    2. (integer) 3
    3. OBJECT encoding set:userid
    4. "intset"

    int set使用一块连续的内存来存储集合数据,它是数组结构不是链表结构。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typedef struct intset { 
    2. uint32_t encoding;
    3. uint32_t length;
    4. int8_t contents[];
    5. } intset;

    intset->encoding用来确定contents[]是什么类型的整数编码,以下三种值之一:

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#define INTSET_ENC_INT16 (sizeof(int16_t)) 
    2. #define INTSET_ENC_INT32 (sizeof(int32_t))
    3. #define INTSET_ENC_INT64 (sizeof(int64_t))

    Redis会根据我们设置的值类型动态sizeof出一个对应的空间大小。如果我们集合原来是int16,然后往集合里添加了int32整数将触发升级,一旦升级成功不会触发降级操作。

    7、压缩表(zip list)

    zip list压缩表是List、Zset、Hash数据类型的底层数据结构之一。它是为了节省内存通过压缩数据存储在一块连续的内存空间中。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typedef struct zlentry { 
    2. unsigned int prevrawlensize, prevrawlen;
    3. unsigned int lensize, len;
    4. unsigned int headersize;
    5. unsigned char encoding;
    6. unsigned char *p;
    7. } zlentry;

    它最大的优点就是压缩空间,空间利用率很高。缺点就是一旦出现更新可能就是连锁更新,因为数据在内容空间中都是连续的,最极端情况下就是可能出现顺序连锁扩张。

    压缩列表会由多个zlentry节点组成,每一个zlentry记录上一个节点长度和大小,当前节点长度lensize和大小len包括编码encoding。

    这取决于业务场景,Redis提供了一组配置,专门用来针对不同的场景进行阈值控制:

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">hash-max-ziplist-entries 512 
    2. hash-max-ziplist-value 64
    3. list-max-ziplist-entries 512
    4. list-max-ziplist-value 64
    5. zset-max-ziplist-entries 128
    6. zset-max-ziplist-value 64

    上述配置分别用来配置ziplist作为Hash、List、Zset数据类型的底层压缩阈值控制。

    8、Redis Object类型与映射

    Redis内部每一种数据类型都是对象化的,也就是我们所说的5种数据类型其实内部都会对应到Redis Object对象,然后再由Redis Object来包装具体的存储数据结构和编码。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typedef struct redisObject { 
    2. unsigned type:4;
    3. unsigned encoding:4;
    4. unsigned lru:REDIS_LRU_BITS;
    5. int refcount;
    6. void *ptr;
    7. } robj;

    这是一个很OO的设计,redisObject->type是5种数据类型之一,redisObject->encoding是这个数据类型所使用的数据结构和编码。

    我们看下Redis提供的5种数据类型与每一种数据类型对应的存储数据结构和编码。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/* Object types */ 
    2. #define REDIS_STRING 0
    3. #define REDIS_LIST 1
    4. #define REDIS_SET 2
    5. #define REDIS_ZSET 3
    6. #define REDIS_HASH 4
    7. #define REDIS_ENCODING_RAW 0
    8. #define REDIS_ENCODING_INT 1
    9. #define REDIS_ENCODING_HT 2
    10. #define REDIS_ENCODING_ZIPMAP 3
    11. #define REDIS_ENCODING_LINKEDLIST 4
    12. #define REDIS_ENCODING_ZIPLIST 5
    13. #define REDIS_ENCODING_INTSET 6
    14. #define REDIS_ENCODING_SKIPLIST 7
    15. #define REDIS_ENCODING_EMBSTR 8

    REDIS_ENCODING_ZIPMAP 3这个编码可以忽略了,在特定的情况下有性能问题,在Redis 2.6版本之后已经废弃,为了兼容性保留。

    图是Redis 5种数据类型与底层数据结构和编码的对应关系:

    但是这种对应关系在每一个版本中都会有可能发生变化,这也是Redis Object的灵活性所在,有着OO的这种多态性。

    • redisObject->refcount表示当前对象的引用计数,在Redis内部为了节省内存采用了共享对象的方法,当某个对象被引用的时候这个refcount会加1,释放的时候会减1。
    • redisObject->lru表示当前对象的空转时长,也就是idle time,这个时间会是Redis lru算法用来释放对象的时间依据。可以通过OBJECT idletime命令查看某个key的空转时长lru时间。

    二、Redis内存管理策略

    Redis在服务端分别为不同的db index维护一个dict,这个dict称为key space键空间。每一个RedisClient只能属于一个db index,在Redis服务端会维护每一个链接的RedisClient。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typedef struct redisClient { 
    2. uint64_t id;
    3. int fd;
    4. redisDb *db;
    5. } redisClient;

    在服务端每一个Redis客户端都会有一个指向redisDb的指针。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typedef struct redisDb { 
    2. dict *dict;
    3. dict *expires;
    4. dict *blocking_keys;
    5. dict *ready_keys;
    6. dict *watched_keys;
    7. struct evictionPoolEntry *eviction_pool;
    8. int id;
    9. long long avg_ttl;
    10. } redisDb;

    key space键空间就是这里的redisDb->dict。redisDb->expires是维护所有键空间的每一个key的过期时间。

    1、键过期时间、生存时间

    对于一个key我们可以设置它多少秒、毫秒之后过期,也可以设置它在某个具体的时间点过期,后者是一个时间戳。例如:

    • EXPIRE命令可以设置某个key多少秒之后过期;
    • PEXPIRE命令可以设置某个key多少毫秒之后过期;
    • EXPIREAT命令可以设置某个key在多少秒时间戳之后过期;
    • PEXPIREAT命令可以设置某个key在多少毫秒时间戳之后过期;
    • PERSIST命令可以移除键的过期时间。

    其实上述命令最终都会被转换成对PEXPIREAT命令。在redisDb->expires指向的key字典中维护着一个到期的毫秒时间戳。

    TTL、PTTL可以通过这两个命令查看某个key的过期秒、毫秒数。

    Redis内部有一个事件循环,这个事件循环会检查键的过期时间是否小于当前时间,如果小于则会删除这个键。

    2、过期键删除策略

    在使用Redis的时候我们最关心的就是键是如何被删除的,如何高效准时地删除某个键。其实Redis提供了两个方案来完成这件事情:惰性删除、定期删除双重删除策略。

    惰性删除:当我们访问某个key的时候,Redis会检查它是否过期。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">robj *lookupKeyRead(redisDb *db, robj *key) { 
    2. robj *val;
    3. expireIfNeeded(db,key);
    4. val = lookupKey(db,key);
    5. if (val == NULL)
    6. server.stat_keyspace_misses++;
    7. else
    8. server.stat_keyspace_hits++;
    9. return val;
    10. }
    11. int expireIfNeeded(redisDb *db, robj *key) {
    12. mstime_t when = getExpire(db,key);
    13. mstime_t now;
    14. if (when < 0) return 0; /* No expire for this key */
    15. if (server.loading) return 0;
    16. now = server.lua_caller ? server.lua_time_start : mstime();
    17. if (server.masterhost != NULL) return now > when;
    18. /* Return when this key has not expired */
    19. if (now <= when) return 0;
    20. /* Delete the key */
    21. server.stat_expiredkeys++;
    22. propagateExpire(db,key);
    23. notifyKeyspaceEvent(REDIS_NOTIFY_EXPIRED,"expired",key,db->id);
    24. return dbDelete(db,key);
    25. }

    定期删除:Redis通过事件循环,周期性地执行key的过期删除动作。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) { 
    2. /* Handle background operations on Redis databases. */
    3. databasesCron();
    4. }
    5. void databasesCron(void) {
    6. /* Expire keys by random sampling. Not required for slaves
    7. * as master will synthesize DELs for us. */
    8. if (server.active_expire_enabled && server.masterhost == NULL)
    9. activeExpireCycle(ACTIVE_EXPIRE_CYCLE_SLOW);
    10. }

    要注意的是:

    • 惰性删除是每次只要有读取、写入都会触发惰性删除代码;
    • 定期删除是由Redis EventLoop来触发的。Redis内部很多维护性工作都是基于EventLoop。

    3、AOF、RDB处理过期键策略

    既然键会随时存在过期问题,那么涉及到持久化Redis是如何帮我们处理的?

    当Redis使用RDB方式持久化时,每次持久化的时候就会检查这些即将被持久化的key是否已经过期,如果过期将直接忽略,持久化那些没有过期的键。

    当Redis作为master主服务器启动的时候,载入rdb持久化键时也会检查这些键是否过期,将忽略过期的键,只载入没过期的键。

    当Redis使用AOF方式持久化时,每次遇到过期的key Redis会追加一条DEL命令到AOF文件,也就是说只要我们顺序载入执行AOF命令文件就会删除过期的键。

    如果Redis作为从服务器启动的话,它一旦与master主服务器建立链接就会清空所有数据进行完整同步。当然新版本的Redis支持SYCN2的半同步,如果是已经建立了master/slave主从同步之后,主服务器会发送DEL命令给所有从服务器执行删除操作。

    4、Redis LRU算法

    在使用Redis的时候我们会设置maxmemory选项,64位的默认是0不限制。线上的服务器必须要设置的,要不然很有可能导致Redis宿主服务器直接内存耗尽,最后链接都上不去。

    所以基本要设置两个配置:

    • maxmemory最大内存阈值;
    • maxmemory-policy到达阈值的执行策略。

    可以通过CONFIG GET
    maxmemory/maxmemory-policy分别查看这两个配置值,也可以通过CONFIG SET去分别配置。

    • maxmemory-policy有一组配置,可以用在很多场景下:
    • noeviction:客户端尝试执行会让更多内存被使用的命令直接报错;
    • allkeys-lru:在所有key里执行lru算法;
    • volatile-lru:在所有已经过期的key里执行lru算法;
    • allkeys-random:在所有key里随机回收;
    • volatile-random:在已经过期的key里随机回收;
    • volatile-ttl:回收已经过期的key,并且优先回收存活时间(TTL)较短的键。

    关于cache的命中率可以通过info命令查看键空间的命中率和未命中率。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Stats 
    2. keyspace_hits:33
    3. keyspace_misses:5

    maxmemory在到达阈值的时候会采用一定的策略去释放内存,这些策略我们可以根据自己的业务场景来选择,默认是noeviction 。

    Redis LRU算法有一个取样的优化机制,可以通过一定的取样因子来加强回收的key的准确度。CONFIG GET maxmemory-samples查看取样配置,具体可以参考更加详细的文章。

    三、Redis持久化方式

    Redis本身提供持久化功能,有两种持久化机制,一种是数据持久化RDB,一种是命令持久化AOF,这两种持久化方式各有优缺点,也可以组合使用。一旦组合使用,Redis在载入数据的时候会优先载入aof文件,只有当AOF持久化关闭的时候才会载入rdb文件。

    1、RDB(Redis DataBase)

    RDB是Redis数据库,Redis会根据一个配置来触发持久化:

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#save   
    2. save 900 1
    3. save 300 10
    4. save 60 10000
    5. CONFIG GET save
    6. 1) "save"
    7. 2) "3600 1 300 100 60 10000"

    表示在多少秒之类的变化次数,一旦达到这个触发条件Redis将触发持久化动作。

    Redis在执行持久化的时候有两种模式BGSAVE、SAVE:

    • BGSAVE是后台保存,Redis会fork出一个子进程来处理持久化,不会block用户的执行请求;
    • SAVE则会block用户执行请求。
    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">struct redisServer { 
    2. long long dirty;/* Changes to DB from the last save */
    3. time_t lastsave; /* Unix time of last successful save */
    4. long long dirty_before_bgsave;
    5. pid_t rdb_child_pid;/* PID of RDB saving child */
    6. struct saveparam *saveparams; /* Save points array for RDB */
    7. }
    8. struct saveparam {
    9. time_t seconds;
    10. int changes;
    11. };

    RedisServer包含的信息很多,其中就包含了有关于RDB持久化的信息。

    redisServer->dirty至上次save到目前为止的change数。redisServer->lastsave上次save时间。

    saveparam struct保存了我们通过save命令设置的参数,time_t是个long时间戳。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typedef __darwin_time_t time_t; 
    2. typedef long __darwin_time_t; /* time() */
    3. int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
    4. for (j = 0; j < server.saveparamslen; j++) {
    5. struct saveparam *sp = server.saveparams+j;
    6. if (server.dirty >= sp->changes &&
    7. server.unixtime-server.lastsave > sp->seconds &&
    8. (server.unixtime-server.lastbgsave_try >
    9. REDIS_BGSAVE_RETRY_DELAY ||
    10. server.lastbgsave_status == REDIS_OK))
    11. {
    12. redisLog(REDIS_NOTICE,"%d changes in %d seconds. Saving...",
    13. sp->changes, (int)sp->seconds);
    14. rdbSaveBackground(server.rdb_filename);
    15. break;
    16. }
    17. }
    18. }

    Redis事件循环会周期性的执行serverCron方法,这段代码会循环遍历server.saveparams参数链表。

    如果server.dirty大于等于我们参数里配置的变化并且
    server.unixtime-server.lastsave大于参数里配置的时间,并且server.unixtime-server.lastbgsave_try减去bgsave重试延迟时间,或者当前server.lastbgsave_status== REDIS_OK则执行rdbSaveBackground方法。

    2、AOF(Append-only file)

    AOF持久化是采用对文件进行追加对方式进行,每次追加都是Redis处理的命令。有点类似command sourcing命令溯源的模式,只要我们可以将所有的命令按照执行顺序在重放一遍就可以还原最终的Redis内存状态。

    AOF持久化最大的优势是可以缩短数据丢失的间隔,做到秒级的丢失率。RDB会丢失上一个保存周期到目前的所有数据,只要没有触发save命令设置的save seconds changes阈值数据就会一直不被持久化。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">struct redisServer { 
    2. /* AOF buffer, written before entering the event loop */
    3. sds aof_buf;
    4. }
    5. struct sdshdr {
    6. unsigned int len;
    7. unsigned int free;
    8. char buf[];
    9. };

    aof_buf是命令缓存区,采用sds结构缓存,每次当有命令被执行当时候都会写一次到aof_buf中。有几个配置用来控制AOF持久化的机制。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">appendonly no 
    2. appendfilename "appendonly.aof"

    appendonly用来控制是否开启AOF持久化,appendfilename用来设置aof文件名。

    1. <pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">appendfsync always
    2. appendfsync everysec
    3. appendfsync no

    appendfsync用来控制命令刷盘机制。现在操作系统都有文件cache/buffer的概念,所有的写入和读取都会走cache/buffer,并不会每次都同步刷盘,因为这样性能一定会受影响。所以Redis也提供了这个选项让我们来自己根据业务场景控制。

    • always:每次将aof_buf命令写入aof文件并且执行实时刷盘。
    • everysec:每次将aof_buf命令写入aof文件,但是每隔一秒执行一次刷盘。
    • no:每次将aof_buf命令写入aof文件不执行刷盘,由操作系统来自行控制。

    AOF也是采用后台子进程的方式进行,与主进程共享数据空间也就是aof_buf,但是只要开始了AOF子进程之后Redis事件循环文件事件处理器会将之后的命令写入另外一个aof_buf,这样就可以做到平滑的切换。

    AOF会不断的追加命令进aof文件,随着时间和并发量的加大aof文件会极速膨胀,所以有必要对这个文件大小进行优化。Redis基于rewrite重写对文件进行压缩。

    1. "box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">no-appendfsync-on-rewrite no/yes 
    2. auto-aof-rewrite-percentage 100
    3. auto-aof-rewrite-min-size 64mb

    no-appendfsync-on-rewrite控制是否在bgrewriteaof的时候还需要进行命令追加,如果追加可能会出现磁盘IO跑高现象。

    上面说过,当AOF进程在执行的时候原来的事件循环还会正常的追加命令进aof文件,同时还会追加命令进另外一个aof_buf,用来做新aof文件的重写。这是两条并行的动作,如果我们设置成yes就不追加原来的aof_buf 因为新的aof文件已经包含了之后进来的命令。

    auto-aof-rewrite-percentage和auto-aof-rewrite-min -size64mb这两个配置前者是文件增长百分比来进行rewrite,后者是按照文件大小增长进行rewrite。

  • 相关阅读:
    【硬件相关】RDMA网络类别及基础介绍
    【1++的Linux】之文件(二)
    supervisor无法杀死threading.Thread().start()创建的子线程
    与目前主流的消费返利平台对比,共享购模式有什么优势呢?
    C++游戏设计教程(1)—— 随机数
    Win11C盘变红怎么办?Win11C盘变红的清理方法
    交通物流模型 | 基于双向时空自适应Transformer的城市交通流预测
    代码随想录打卡第六十三天|84.柱状图中最大的矩形
    【学习笔记】数据一致性分发
    黑客(网络安全)技术自学——高效学习
  • 原文地址:https://blog.csdn.net/Java_ttcd/article/details/126316887