redis五种数据类型具体时候的底层编码

redis随着值的类型不同,其在底层编码类型会不相同。目前现有的编码格式有

#define OBJ_ENCODING_RAW 0     /* Raw representation */
#define OBJ_ENCODING_INT 1     /* Encoded as integer */
#define OBJ_ENCODING_HT 2      /* Encoded as hash table */
#define OBJ_ENCODING_ZIPMAP 3  /* Encoded as zipmap */
#define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */
#define OBJ_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
#define OBJ_ENCODING_INTSET 6  /* Encoded as intset */
#define OBJ_ENCODING_SKIPLIST 7  /* Encoded as skiplist */
#define OBJ_ENCODING_EMBSTR 8  /* Embedded sds string encoding */
#define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of ziplists */
#define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks */

1、redis中的string类型的值底层编码

1.1、string的底层编码

在redis中要对应string类型的value进行编码,有int、raw、embstr三种编码格式。
当value值为数字的时候,使用的是int类型进行编码,
当value为字符类型的时候且长度小于等于44时,使用的是embstr格式
当value为字符类型的时候且长度大于44时,使用的是raw类型的编码格式

1.2、 验证上面所说

  • 当value为int类型时候
127.0.0.1:6379> set intkey 122
OK
127.0.0.1:6379> strlen intkey
(integer) 3
127.0.0.1:6379> object encoding intkey
"int"
  • 当vule为字符串且长度小于等于44
 127.0.0.1:6379> set stringkey a1230123456789012345678901234567890123456789
OK
127.0.0.1:6379> strlen stringkey
(integer) 44
127.0.0.1:6379> object encoding stringkey
"embstr"
  • 当vule为字符串且长度大于44
127.0.0.1:6379> append stringkey 1
(integer) 45
127.0.0.1:6379> strlen stringkey
(integer) 45
127.0.0.1:6379> object encoding stringkey
"raw"

从上面我们可以看出当我们设置intkey的值为122时候,这个时候redis的底层编码使用的是int类型。
当我们设置stringkey的值为小于或者等于44个字节长度的时候,底层的编码使用的是embstr编码格式;当字符串的长度大于44的时候,编码格式就是使用RAW格式

2、list的底层编码

list这种数据类型在redis底层的编码格式只有quickList这一种编码格式。
兼顾了ziplist的节省内存,并且一定程度上解决了连锁更新的问题,我们的
quicklistNode节点里面是个ziplist,每个节点是分开的。那么就算发生了
连锁更新,也只会发生在一个quicklistNode节点

quicklist.文件中源码
typedef struct
{
struct quicklistNode *prev; //前指针
struct quicklistNode *next; //后指针
unsigned char *zl; //数据指针 指向ziplist
unsigned int sz; //ziplist大小 /* ziplist
size in bytes */
unsigned int count : 16; /* count of items in
ziplist */ //ziplist的元素
unsigned int encoding : 2; /* RAW==1 or LZF==2 */ //
是否压缩, 1没有压缩 2 lzf压缩
unsigned int container : 2; /* NONE==1 or ZIPLIST==2
*/ //预留容器字段
unsigned int recompress : 1; /* was this node previous
compressed? */
unsigned int attempted_compress : 1; /* node can't
compress; too small */
unsigned int extra : 10; /* more bits to steal for
future usage */ //预留字段
} quicklistNode;

在这里插入图片描述
我们可以redis.conf文件中通过参数list-max-ziplist-size配置QuickListNode的大小,如果参数配置为正数,那么代表每个quickListNode的ziplist的entry大小。如果配置为负数,代表的是一个ziplist的大小,具体数值固定为-5 ~ -1 ,默认配置是-2。-5到-1代表的值见下面:

# Lists are also encoded in a special way to save a lot of space.
# The number of entries allowed per internal list node can be specified
# as a fixed maximum size or a maximum number of elements.
# For a fixed maximum size, use -5 through -1, meaning:
# -5: max size: 64 Kb  <-- not recommended for normal workloads
# -4: max size: 32 Kb  <-- not recommended
# -3: max size: 16 Kb  <-- probably not recommended
# -2: max size: 8 Kb   <-- good
# -1: max size: 4 Kb   <-- good
# Positive numbers mean store up to _exactly_ that number of elements
# per list node.
# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size),
# but if your use case is unique, adjust the settings as necessary.
list-max-ziplist-size -2

一般来说推荐配置为-2,-1这两个值。不推荐使用其他的值

验证我们说的list结构使用的是quicklist

127.0.0.1:6379> lpush listKey acv jks
(integer) 2
127.0.0.1:6379> object encoding listkey
(nil)
127.0.0.1:6379> llen listKey
(integer) 2
127.0.0.1:6379> object encoding listKey
"quicklist"

3、hash类型的底层编码

我们可以使用hash类型来存储json对象的时候,需要同时满足下面两个条件时,使用的是ziplist来存储,否则使用dict来进行存储
使用ziplist需要满足的两个条件是:
1、哈希对象保存的键值对数量<512个
2、每一个字段值的都小于64B
验证上述的说法是否正确

  • 设置一个字段时且值的长度小于64B
127.0.0.1:6379> hset hkey name 123
(integer) 1
127.0.0.1:6379> type hkey
hash
127.0.0.1:6379> object encoding hkey
"ziplist"

这个时候我们使用的是ziplist进行编码的

  • 设置一个字段时但字段的值大于64
127.0.0.1:6379> hset hkey2 name a123012345678901234567890123456789012345678901234567890123456789
(integer) 1
127.0.0.1:6379> object encoding hkey2
"ziplist"
127.0.0.1:6379> hstrlen hkey2 name
(integer) 64
127.0.0.1:6379> hset hkey2 name a1230123456789012345678901234567890123456789012345678901234567890
(integer) 0
127.0.0.1:6379> object encoding hkey2
"hashtable"
127.0.0.1:6379> hstrlen hkey2 name
(integer) 65

从上面可以看出,当字段的大小为64的时候其编码格式为"ziplist",字段值再增大一个时候,编码格式就变成了hashtable。

  • ziplist编码具有什么优势?
    ziplist的编码格式如下:
    <zl总字节数><zl的最后一个字节偏移量><zl的entry数> <zllen个entry >
    ziplist可以根据不同的类型及不同大小来分配不同大小的空间,达到节省内存的目的
  • ziplist缺点是什么?
    由于ziplist使用的是一个完整的内存块,如果遇到一个元素需要进行更新的会导致联动更新,严重影响到性能,所以只适合数据量比较小的场景。

4、set类型的底层编码

Redis用intset或hashtable存储set。满足下面条件,就用inset存储

  • 1、存储的所有元素都是整数
  • 2、 如果元素个数超过512个,也会用hashtable存储。跟一个配置有关:
set-max-intset-entries 512

验证上面所说,当skey中只有整数时且数量在512个内,使用的是intset。

127.0.0.1:6379> sadd skey 1 2 3 4 3
(integer) 4
127.0.0.1:6379> object encoding skey
"intset"

给上面的集合中添加一个非整数的值,当添加完成后我们再次进行查看的时候发现其数值变成了hashtable

127.0.0.1:6379> sadd skey a
(integer) 1
127.0.0.1:6379> object encoding skey
"hashtable"

512个整数时候编码是intset,然后当我们再新增一个时候,发现其编码编程了hashtable了。

127.0.0.1:6379> sadd skey 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
(integer) 512
127.0.0.1:6379> scard skey 
(integer) 512
127.0.0.1:6379> object encoding skey
"intset"
127.0.0.1:6379> sadd skey 512
(integer) 1
127.0.0.1:6379> object encoding skey
"hashtable"
127.0.0.1:6379> scard skey
(integer) 513

那我们这里hashtable是一个k-v键值对,这只存储一个元素需要怎么处理呢?这里的元素存储在key上,然后v赋值为null就可以了。
在这里插入图片描述

5、zset类型的底层编码

默认使用ziplist编码(第三次见到了,hash的小编码,quicklist的Node,
都是ziplist)。
在ziplist的内部,按照score排序递增来存储。插入的时候要移动之后的数据。
如果元素数量大于等于128个,或者任一member长度大于64字节使用skiplist存储。

  • 当数量少于128个时候且单个member的长度小于64字节的使用ziplist
127.0.0.1:6379> zadd zkey 1 a 
(integer) 1
127.0.0.1:6379> zadd zkey 2 b
(integer) 1
127.0.0.1:6379> object encoding zkey
"ziplist"
  • 单个元素的长度等于64字节的时候
127.0.0.1:6379> zadd zkey 3 a123012345678901234567890123456789012345678901234567890123456789
(integer) 1
127.0.0.1:6379> object encoding zkey
"ziplist"
  • 单个元素大于64字节的时候
127.0.0.1:6379> zadd zkey 4 a1230123456789012345678901234567890123456789012345678901234567890
(integer) 1
127.0.0.1:6379> object encoding zkey
"skiplist"
127.0.0.1:6379> zcard zkey
(integer) 4

总结

  • string 类型的数据在redis中都是按照sds格式进行存储的
  • hash类型在字段数量不大于512个及单个字段值不大于64B的时候采用ziplist,否则使用hashTable来进行存储
  • list类型是采用quickList列表来进行存储的,其是一个包含ziplist的双向链表组成的
  • set类型在满足所有元素都是整数且元素个数不超过512个的时候采用的intset编码,否则使用hashTable来进行存储
  • zset类型在同时满足元素不大于128个及单个元素的长度不超过64B的时候采用ziplist来进行存储的,否则使用skipList来进行存储
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值