redis持久化-RDB
文档
- redis单机安装
- redis常用的五种数据类型
- redis数据类型-位图bitmap
- redis数据类型-基数统计HyperLogLog
- redis数据类型-地理空间GEO
- redis数据类型-流Stream
- redis数据类型-位域bitfield
官方文档
- 官网操作命令指南页面:https://redis.io/docs/latest/commands/?name=get&group=string
- Redis persistence
- SAVE
- BGSAVE
RDB
说明
- redis版本:7.0.0
- RDB:redis database,redis数据库
- 根据配置的规则,每隔一定的时间,将内存中的数据集快照,也就是snapshot内存快照,写入磁盘,恢复时,再将硬盘中的快照文件直接读回到内存中
- 快照文件被称为RDB文件,全称
dump.rdb
持久化配置
redis-6.0.19的相关配置
-
解压目录下
redis.conf
文件中,相关的配置properties################################ SNAPSHOTTING ################################ # # Save the DB on disk: # # save <seconds> <changes> # # Will save the DB if both the given number of seconds and the given # number of write operations against the DB occurred. # # In the example below the behavior will be to save: # after 900 sec (15 min) if at least 1 key changed # after 300 sec (5 min) if at least 10 keys changed # after 60 sec if at least 10000 keys changed # # Note: you can disable saving completely by commenting out all "save" lines. # # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # like in the following example: # # save "" save 900 1 save 300 10 save 60 10000
-
配置表示,满足以下条件,将触发RDB持久化
-
上次保存后,如果有1个键发生变化,900秒后保存
-
上次保存后,如果有10个键发生变化,300秒后保存
-
上次保存后,如果有10000个键发生变化,60秒后保存
-
redis-7.0.0的相关配置
-
解压目录下
redis.conf
文件中,相关的配置properties################################ SNAPSHOTTING ################################ # Save the DB to disk. # # save <seconds> <changes> [<seconds> <changes> ...] # # Redis will save the DB if the given number of seconds elapsed and it # surpassed the given number of write operations against the DB. # # Snapshotting can be completely disabled with a single empty string argument # as in following example: # # save "" # # Unless specified otherwise, by default Redis will save the DB: # * After 3600 seconds (an hour) if at least 1 change was performed # * After 300 seconds (5 minutes) if at least 100 changes were performed # * After 60 seconds if at least 10000 changes were performed # # You can set these explicitly by uncommenting the following line. # # save 3600 1 300 100 60 10000
Snapshotting官方文档说明
Snapshotting
By default Redis saves snapshots of the dataset on disk, in a binary file called
dump.rdb
. You can configure Redis to have it save the dataset every N seconds if there are at least M changes in the dataset, or you can manually call theSAVE
orBGSAVE
commands.For example, this configuration will make Redis automatically dump the dataset to disk every 60 seconds if at least 1000 keys changed:
save 60 1000
This strategy is known as snapshotting.
- 保存快照,可自动触发,或手动调用命令
自动触发保存快照
-
找到启动服务使用的配置文件,通常将安装目录下的
redis.conf
复制到指定路径做为启动文件。修改配置文件,设置保存快照触发条件properties# save 3600 1 300 100 60 10000 save 300 5 60 10
- 上次保存后,如果有5次变更,300秒后保存
- 上次保存后,如果有10次变更,60秒后保存
-
修改后重启redis
修改dump文件的保存路径
-
找到启动服务使用的配置文件,通常将安装目录下的
redis.conf
复制到指定路径做为启动文件。修改配置文件,设置自定义保存路径,需提前创建好指定路径,默认路径:./
pro# The working directory. # # The DB will be written inside this directory, with the filename specified # above using the 'dbfilename' configuration directive. # # The Append Only File will also be created inside this directory. # # Note that you must specify a directory here, not a file name. # dir ./ dir /opt/module/redis/myredis/dumpfiles
-
修改后重启redis
修改dump文件名称
-
找到启动服务使用的配置文件,通常将安装目录下的
redis.conf
复制到指定路径做为启动文件。修改配置文件,设置自定义文件名称,默认名称:dump.rdb
properties# The filename where to dump the DB # dbfilename dump.rdb dbfilename dump6379.rdb
- 可以标记服务器IP、服务端口号等
-
修改后重启redis
获取配置信息命令
config get requirepass
:获取密码config get port
:获取端口号config get dir
:获取RDB文件保存路径
恢复快照记录
每次重启服务都是一次恢复的过程,下面模拟恢复指定快照文件dump6379.rdb
- 启动redis,在redis中添加几条数据
- 查询当前存在的
key
,keys *
- 关闭redis,
shutdown
dump6379.rdb
文件重命名为dump6379.rdb.bak
,模拟已将快照文件保存到其它位置,此时该路径下无dump6379.rdb
文件- 启动redis,此时查询当前存在的
key
,无数据。因为没有快照文件,所以数据不存在 - 关闭redis
- 此时会生成新的
dump6379.rdb
文件,删除此文件。将dump6379.rdb.bak
文件重命名为dump6379.rdb
,模拟将备份的快照文件放回指定的目录下 - 启动redis,此时查询当前存在的
key
,有值,说明数据已经恢复
手动触发保存快照
官方说明
The
SAVE
commands performs a synchronous save of the dataset producing a point in time snapshot of all the data inside the Redis instance, in the form of an RDB file.You almost never want to call
SAVE
in production environments where it will block all the other clients. Instead usuallyBGSAVE
is used. However in case of issues preventing Redis to create the background saving child (for instance errors in the fork(2) system call), theSAVE
command can be a good last resort to perform the dump of the latest dataset.
相关命令
save
:save命令会阻塞其它客户端,生产环境一般不使用bgsave
:Save the DB in background。子进程进行保存快照的操作,主进程继续处理客户端的请求lastsave
:查看最后保存快照的时间,返回时间戳,linux系统可以使用date
命令对时间戳进行格式化:date -d @1746104405
RDB的优点
官方说明
RDB advantages
- RDB is a very compact single-file point-in-time representation of your Redis data. RDB files are perfect for backups. For instance you may want to archive your RDB files every hour for the latest 24 hours, and to save an RDB snapshot every day for 30 days. This allows you to easily restore different versions of the data set in case of disasters.
- RDB is very good for disaster recovery, being a single compact file that can be transferred to far data centers, or onto Amazon S3 (possibly encrypted).
- RDB maximizes Redis performances since the only work the Redis parent process needs to do in order to persist is forking a child that will do all the rest. The parent process will never perform disk I/O or alike.
- RDB allows faster restarts with big datasets compared to AOF.
- On replicas, RDB supports partial resynchronizations after restarts and failovers.
总结
- 适合大规模的数据恢复
- 按照业务定时备份。定时保存RDB文件,一般应保存到其它服务器上,可形成不同的数据版本,需要恢复时,可以按需选择对应的版本
- 对数据完整性和一致性要求不高
- RDB文件在内存中的加载速度要比AOF快得多
RDB的缺点
官方说明
RDB disadvantages
- RDB is NOT good if you need to minimize the chance of data loss in case Redis stops working (for example after a power outage). You can configure different save points where an RDB is produced (for instance after at least five minutes and 100 writes against the data set, you can have multiple save points). However you'll usually create an RDB snapshot every five minutes or more, so in case of Redis stopping working without a correct shutdown for any reason you should be prepared to lose the latest minutes of data.
- RDB needs to fork() often in order to persist on disk using a child process. fork() can be time consuming if the dataset is big, and may result in Redis stopping serving clients for some milliseconds or even for one second if the dataset is very big and the CPU performance is not great. AOF also needs to fork() but less frequently and you can tune how often you want to rewrite your logs without any trade-off on durability.
总结
- 如果redis不是正确的关闭,则未达到保存点的数据可能会丢失
- RDB需要经常fork()子进程用来保存快照,如果数据集很大fork()可能会很耗时,并且如果数据集非常大并且CPU性能不好,则可能导致Redis停止为客户端服务几毫秒甚至一秒钟
检查并修复RDB文件
-
进入redis安装目录下的bin目录
/opt/module/redis/bin
shellcd /opt/module/redis/bin
-
操作
redis-check-rdb
,检查并修复RDB文件shell./redis-check-rdb /opt/module/redis/myredis/dumpfiles/dump6379.rdb
触发保存快照的场景
- 达到配置文件中设置的保存快照触发条件
- 手动执行
save
、bgsave
命令 - 执行
flushall
命令 - 正确执行
shutdown
命令,且没有设置开启AOF持久化 - 主从复制时,主节点自动触发
禁用快照
-
客户端执行命令禁用快照
shellconfig set save ""
-
修改
redis.conf
配置文件禁用快照properties# Snapshotting can be completely disabled with a single empty string argument # as in following example: # # save "" save "" # 注释掉其它的save配置 # save 3600 1 300 100 60 10000
其它RDB配置
找到启动服务使用的配置文件,通常将安装目录下的redis.conf
复制到指定路径做为启动文件,修改配置文件
-
stop-writes-on-bgsave-error
,默认yes
,表示,如果后台保存执行失败,将停止接受写入操作shellstop-writes-on-bgsave-error yes
-
rdbcompression
,默认yes
,表示,使用LZF算法进行压缩存储shellrdbcompression yes
-
rdbchecksum
,默认yes
,表示,使用CRC64算法进行数据校验shellrdbchecksum yes
-
rdb-del-sync-files
,默认no
,yes
表示,在未启用持久化的情况下删除复制使用的RDB文件shellrdb-del-sync-files no