Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis开发与运维 #19

Open
Danceiny opened this issue Jun 23, 2019 · 18 comments
Open

Redis开发与运维 #19

Danceiny opened this issue Jun 23, 2019 · 18 comments

Comments

@Danceiny
Copy link
Owner

Danceiny commented Jun 23, 2019

《Redis开发与运维 》Google Drive

@Danceiny
Copy link
Owner Author

Danceiny commented Jun 23, 2019

查看内部数据结构实现

> object encoding KEY

image

@Danceiny
Copy link
Owner Author

字符串

image

@Danceiny
Copy link
Owner Author

哈希

image

@Danceiny
Copy link
Owner Author

列表

image

@Danceiny
Copy link
Owner Author

集合

image

@Danceiny
Copy link
Owner Author

有序集合

image

@Danceiny
Copy link
Owner Author

GEO

  • geopos key member [member ...]
  • geodist key member1 member2 [unit]

unit: m, km, mi, ft

获取指定位置范围内的地理位置信息集合

  • georadius key longtitude latitude radius m|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]
  • georadiusbymember key member radius m|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]

获取geohash

  • GEO的数据类型为zset,score就是geohash(member就是地名,因此删除就是zrem key member
  • geohash字符串越长,位置就越精确,geohash长度为9时,精度在2米左右
  • 两个字符串越相似,则距离越近,redis使用字符串前缀匹配算法实现相关命令
  • geohash编码和经纬度是可以互转的

@Danceiny
Copy link
Owner Author

RESP协议

发送命令

*<参数数量> CRLF
$<参数1的字节数> CRLF
<参数1> CRLF
...
$<参数N的字节数> CRLF
<参数N> CRLF

image

@Danceiny
Copy link
Owner Author

Danceiny commented Jul 20, 2019

输入缓冲区 qbuf qbuf-free

每个客户端的输入缓冲区硬编码为1GB,一旦超过该值,客户端会被关闭。
输入缓冲区不受maxmemory限制,如果一个redis实例设置maxmemory为4G,已经存储2G数据库,此时输入缓冲区使用了3G,可能会导致数据丢失、键值淘汰、OOM等。
输入缓冲区过大的原因:

  • redis的处理速度跟不上输入缓冲区的输入速度,且每次进入缓冲区的命令包含了大量的bigkey
  • redis发生阻塞

输出缓冲区 obl oll omem

image

输出缓冲区由两部分组成:固定缓冲区(16KB)和动态缓冲区。

typedef struct redisClient {
    // 动态缓冲区列表
    list *reply;
    // 动态缓冲区列表长度(对象个数)
    unsigned long reply_bytes;
    // 固定缓冲区已经使用的字节数
    int bufpos;
    // 字节数组作为固定缓冲区
    char buf[REDIS_REPLY_CHUNK_BYTES];
} redisClient;

如何预防输出缓冲区出现异常?(比输入缓冲区异常概率更大)

  • 限制普通客户端输出缓冲区
  • 适当增大slave的输出缓冲区,如果master写入较大,slave的输出缓冲区可能会比较大,一旦slave客户端连接因为输出缓冲区溢出被kill,会造成复制重连。(????)
  • 限制容易让输出缓冲区增大的命令,如高并发下的monitor命令。

@Danceiny
Copy link
Owner Author

Danceiny commented Jul 20, 2019

客户端类型

image
image

客户端配置

image

@Danceiny
Copy link
Owner Author

AOF

image
image
image

@Danceiny
Copy link
Owner Author

image
image
image

Redis阻塞 https://redis.io/topics/latency

@Danceiny
Copy link
Owner Author

阻塞的外在原因

  1. CPU竞争
  2. 内存交换
  3. 网络问题
    image

@Danceiny
Copy link
Owner Author

image
image

  1. 对象内存
    key对象和value对象。

  2. 缓冲内存

  • 客户端缓冲

输入缓冲无法控制,最大1G;输出通过client-output-buffer-limit控制。

  • 复制积压缓冲
  • AOF缓冲

内存碎片

默认使用的内存分配器采用jemelloc,可选的还有glibctcmalloc

@Danceiny
Copy link
Owner Author

redisObject对象

image

@Danceiny
Copy link
Owner Author

JSON string OR HASH ?

image

@Danceiny
Copy link
Owner Author

Danceiny commented Aug 24, 2019

image
image

@Danceiny
Copy link
Owner Author

Danceiny commented Aug 24, 2019

ziplist

image

  • zlbytes int32,整个ziplist所占字节长度。
  • zltail int32,距离尾结点的偏移量,方便pop。
  • zllen int16,节点数量。
  • prev_entry_bytes_length: 前一个entry所占空间,可以实现列表反向迭代。
  • encoding 当前节点编码和长度,前两位表示编码类型:字符串、整数;其余位表示数据长度。
  • contents 节点的值
  • zlend 列表结尾,一个字节。
    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant