Redis数据库底层设计
- redis的底层结构是使用一个hashtable加上链表
- 寻址通过hash(key)%hashtable.size=[0,hashtable.size-1],将指针存储到对应的位置。
- 如果寻址找到了同一个位置,会使用头插法放在链表中
hashTable+链表图例
ReHash
- 当元素放置超过hashtable的大小,就会做一个扩容,扩容大小为size*2
- 扩容后会将旧的hashtable中的元素搬到新的hashtable中进行rehash。
何时进行rehash
- 通过get和set使用时,redis进行rehash是通过一次搬取1个hash槽
- 通过事件轮询,redis进行rehash,一次搬取100个hash槽
- 渐进式hash能够减少对服务器占用的时间过长,导致业务卡顿一段时间
关于rehash时的访问数据问题
rehash时,访问数据会先访问老的,后访问新的,新增则直接访问新的hashtable
Redis数据类型及源码
- redis中的string类型
redis的string类型是使用simple dynamic string(SDS)来实现的,C语言字符串使用char数组表示,以\0结尾,会使得字符串出现安全问题。
struct __attribute__ ((__packed__)) sdshdr5 {
unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr8 {
uint8_t len; /* used */
uint8_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr16 {
uint16_t len; /* used */
uint16_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr32 {
uint32_t len; /* used */
uint32_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr64 {
uint64_t len; /* used */
uint64_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
- 一个flag为一个字节,8个bit,前3个为数据类型,后5个位长度len,sds5为5个字节,范围为0~2^5,sds8为8个字节,范围为2 ^5 ~2^8
- alloc类似于早期的free字段,避免每多一个字符就重新开辟内存空间,每次扩容后将多余的大小给free,每次只要追加。
- redis数据库
struct dict {
dictType *type;
dictEntry **ht_table[2];
unsigned long ht_used[2];
long rehashidx; /* rehashing not in progress if rehashidx == -1 */
/* Keep small vars at end for optimal (minimal) struct padding */
int16_t pauserehash; /* If >0 rehashing is paused (<0 indicates coding error) */
signed char ht_size_exp[2]; /* exponent of size. (size = 1<<exp) */
};
rehashidx为上一次rehash的索引,dictEntry 有两个dict字典,需要完成渐进式rehash
- hash表结构
- 键值对类型
typedef struct dictEntry {
void *key;
union {
void *val;
uint64_t u64;
int64_t s64;
double d;
} v;
struct dictEn try *next; /* Next entry in the same hash bucket. */
void *metadata[]; /* An arbitrary number of bytes (starting at a
* pointer-aligned address) of size as returned
* by dictType's dictEntryMetadataBytes(). */
} dictEntry;
key为sds类型,val会指向redisObject对象
- redisObject
struct redisObject {
unsigned type:4;
unsigned encoding:4;
unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
* LFU data (least significant 8 bits frequency
* and most significant 16 bits access time). */
int refcount;
void *ptr;
};
encoding为val的编码类型,refcount为引用数,ptr为数据地址
- redisObject一共有64字节,前面4个占用16个字节,外加ptr指向的sds类型数据为48个字节,但是sds8需要占用额外4个字节(len、alloc、flags、\0),所以为44个字节,小于等于44,可以将数据和redisObject存储在一起,即为embstr
- 整体结构
- list的底层设计
- list 有两者存储类型,一种是quicklist,另一种是ziplist
- ziplist
zlbytes:占4个字节,记录整个压缩列表占用的内存字节数。
zltail_offset:占4个字节,记录压缩列表尾节点entryN距离压缩列表的起始地址的字节数。
zllength:占2个字节,记录了压缩列表的节点数量。
entry[1-N]:长度不定,保存数据。
zlend:占1个字节,保存一个常数255(0xFF),标记压缩列表的末端。(\0)
- set类型的底层原理(intset和hashtable)
- 当数据都是整型值的时候,set的encoding会采取intset有序的set
- 当数据存在其他类型的时候,set的encoding会采取hashtable