写点什么

redis 设计与实现(1)redis 数据结构

用户头像
王传义
关注
发布于: 2020 年 07 月 01 日



参考:https://wangcy6.github.io/post/2020/server_redis/



缘起





typedef struct redisObject {
unsigned type : 4;
unsigned encoding : 4;
int refcount;
void *ptr;
} robj;
类型常量 对象的名称
REDIS_STRING 字符串对象
REDIS_LIST 列表对象
REDIS_HASH 哈希对象
REDIS_SET 集合对象
REDIS_ZSET 有序集合对象





  • 整数集合

127.0.0.1:6379> sadd numbers 1 2 3 4
(integer) 4
127.0.0.1:6379> object encoding numbers
"intset"
debug object numbers
Value at:0x7f01844863c0 refcount:1 encoding:hashtable serializedlength:21 lru:16526314 lru_seconds_idle:1396
127.0.0.1:6379> object encoding page_rank
"ziplist"
127.0.0.1:6379> ZADD page_rank 2.0 oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
(integer) 1
127.0.0.1:6379> object encoding page_rank
"skiplist"







  • hashtable 的 val 可能存储很多东西



  • 转换规则

  • 有序集合 zset(ziplist,shiplist)





  • 对比mysql索引设计(hash索引,b+索引)



  • 为什么字符串太长 64字节,进行转换?我也不清楚



单机数据库如何存这些key







RDB持久化



思考:

  1. 如何持久化,废话不就是fork进程吗

  2. 主备同步不是有复制积压缓冲区吗?为什么不采用这个。



行动:







  • 还有其他缓冲区吗?

https://www.cnblogs.com/gangdou/p/7991754.html

https://dev.to/setevoy/redis-psync-scheduled-to-be-closed-asap-for-overcoming-of-output-buffer-limits-and-the-client-output-buffer-limit-6on





描述:



output-buffer-limits这个参数的作用是:

当客户端来获取数据的时候,数据会被保存在output-buffer中,

等信息传输完后output-buffer中的数据会被清理掉,redis对output-buffer做了限制。



psync的命令超过了output-buffer-limits,master主动关闭了slave的连接。

然后slave被断开了和master的连接,然后slave又重新找master请求同步,然后就陷入了恶性循环,

slave找master做全同步的过程是一个很消耗cpu消耗io消耗带宽的过程,所以会一直持续的告警。



默认1M,允许数据被丢弃



这个是共享的



这个不是为RDB 缓冲区用的



一个完整复制过程:

https://redis.io/topics/replication



















https://github.com/redis-io/redis/issues/4316

https://redislabs.com/blog/the-endless-redis-replication-loop-what-why-and-how-to-solve-it/

Replication in Redis is like to a dialog between a slave, (or an apprentice), and its master.



This conversation runs along the these lines:



  1. Apprentice: “I want to learn and to become like you.”

  2. Master: “PATIENCE YOU MUST HAVE my young padawan. Hmmmmmm.”

  3. The Master forks itself, and…The forked process dumps the dataset to a disk file (RDB),The main process continues serving regular client requests, and Any changes made to the data are copied to a replication buffer on the main process.

  4. Once the dump is done, the Master says: “Come and get it while, hot, is it.”

  5. The apprentice reads the file over the network and writes it to its own disk.

  6. Once it has the file stored locally, the apprentice loads it.

  7. After loading is finished, the apprentice asks: “Well, I finished my circle. I’m ready.

  8. If there are any changes in the buffer, the Master says: “Ready you are? What know you of ready? Feel the Force!” and replays the stored changes to the slave.

  9. After there are no changes left to replay from the buffer, the Master says: “No more training do you require. Already know you, that which you need.

  10. From that moment, any new change request that the master is given is then also replayed to the apprentice.



用户头像

王传义

关注

希望每一位来访的朋友都能有所收获! 2017.11.30 加入

如果有疑问 wang_cyi@163.com 联系

评论 (1 条评论)

发布
用户头像
slave找master做全同步的过程是一个很消耗cpu消耗io消耗带宽的过程,所以会一直持续的告警。
2020 年 07 月 01 日 16:12
回复
没有更多了
redis设计与实现(1)redis数据结构