剖析Redis RDB文件

通过redis-cli中执行save或者bgsave可以得到RDB文件(文件名由配置文件中dbfilename指定,例如dbfilename “dump.rdb”),这个文件包含Redis实例中全量的数据,那么dump.rdb的文件格式大概是什么样的呢?

RDB文件定义

----------------------------# RDB is a binary format. There are no new lines or spaces in the file.
52 45 44 49 53              # Magic String "REDIS"
00 00 00 03                 # RDB Version Number in big endian. In this case, version = 0003 = 3
----------------------------
FE 00                       # FE = code that indicates database selector. db number = 00
----------------------------# Key-Value pair starts
FD $unsigned int            # FD indicates "expiry time in seconds". After that, expiry time is read as a 4 byte unsigned int
$value-type                 # 1 byte flag indicating the type of value - set, map, sorted set etc.
$string-encoded-key         # The key, encoded as a redis string
$encoded-value              # The value. Encoding depends on $value-type
----------------------------
FC $unsigned long           # FC indicates "expiry time in ms". After that, expiry time is read as a 8 byte unsigned long
$value-type                 # 1 byte flag indicating the type of value - set, map, sorted set etc.
$string-encoded-key         # The key, encoded as a redis string
$encoded-value              # The value. Encoding depends on $value-type
----------------------------
$value-type                 # This key value pair doesn't have an expiry. $value_type guaranteed != to FD, FC, FE and FF
$string-encoded-key
$encoded-value
----------------------------
FE $length-encoding         # Previous db ends, next db starts. Database number read using length encoding.
----------------------------
...                         # Key value pairs for this database, additonal database
FF                          ## End of RDB file indicator
8 byte checksum             ## CRC 64 checksum of the entire file.

备注: 参考Github官网RDB文件格式定义

获取RDB文件

我们首先通过redis-cli中执行save得到dump.rdb文件,然后用WinHex打开这个文件:

《剖析Redis RDB文件》 dump.rdb.png

说明:导出RDB文件时整个Redis实例中db=0里有个string类型的username,值为afei;db=6里有个string类型的uname,值为root,且设置了失效时间;

剖析RDB文件

根据WinHex达到的dump.rdb文件一个一个字节剖析文件真实的内容,以52这个16进制数为例,其十进制数值为82,通过ASCII码对照表可知,82对应的字符是R,相应的:45->69->E,44->68->D,49->73->I,53->83->S,最终该RDB文件内容如下:REDIS

验证RDB文件定义

前面5个16进制52 45 44 49 53已经被验证为REDIS
30 30 30 36则表示RDB文件版本号
FE表示db选择器编码,00则表示选择编号为0这个db
00这个ASCII码对应的字符为NUT,即
08这个ASCII码对应的字符为BS,即BackSpace退格按键;
75 73 65 72 6E 61 6D 65 04 61 66 65 69 则表示:

username 
afei

说明:04这个ASCII对应的字符是EOT,表示文尾,end of transmission;所以username的值afei是另起一行;

FE表示db选择器编码,06则表示选择编号为6这个db
FC表示设置了毫秒级失效,接下来的8个字节表示失效时间的Unix时间戳;
4F D7 D3 E4 5D 01 00 00 00 表示失效时间Unix时间戳;
05这一个字节表示value的类型:string,hash,list ,set,sorted set;
75 6E 61 6D 65 04 72 6F 6F 74则表示:

uname 
root

FF表示RDB文件结束;
C7 41 31 D7 AA 1F 24 A2这8个字节表示整个RDB文件的rdbsum CRC 64;

    原文作者:阿飞的博客
    原文地址: https://www.jianshu.com/p/c210851d3558
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞