Redis学习笔记—持久化机制之RDB
Posted 爱锅巴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis学习笔记—持久化机制之RDB相关的知识,希望对你有一定的参考价值。
Redis支持RDB和AOF两种持久化机制,持久化功能有效地避免因进程退出造成的数据丢失问题,当下次重启时利用之前持久化的文件即可实现数据恢复;本篇介绍RDB。
RDB (Redis DataBase)持久化是把当前进程数据生成快照保存到硬盘的过程,触发RDB持久化过程分为手动触发和自动触发
手动触发
两个用于持久化的手动触发命令:save和bgsave
save命令:阻塞当前Redis服务器,直到RDB过程完成为止,对于内存比较大的实例会造成长时间阻塞,线上环境不建议使用。运行save命令对应的Redis日志如下:
127.0.0.1:6379> save
OK
bgsave命令:显然save命令在庞大数据量的情况下是不允许使用的,所以Redis提供了bgsave
127.0.0.1:6379> bgsave
Background saving started
自动触发
因为save命令会阻塞Redis服务,所以所有的自动触发操作都是使用的bgsave方式,
自动触发的场景:
- 根据配置文件中save配置,比如“save m n”。表示m秒内数据集存在n次修改时,自动触发bgsave
- 如果从节点执行全量复制操作,主节点自动执行bgsave生成RDB文件并发送给从节点
- 执行debug reload命令重新加载Redis时,会自动触发save操作
- 默认情况下执行shutdown命令时,如果没有开启AOF持久化功能则自动执行bgsave
恢复数据
将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务即可,redis就会自动加载文件数据至内存了。Redis 服务器在载入 RDB 文件期间,会一直处于阻塞状态,直到载入工作完成为止。
获取 redis 的安装目录可以使用 config get dir 命令
127.0.0.1:6379> config get dir
1) "dir"
2) "/usr/local/redis-5.0.3"
停止 RDB 持久化
1.可以直接删除配置文件中的所有save配置
2.通过命令
redis-cli config set save " "
RDB的优缺点
优点: 可以做定时执行备份用于灾难恢复,而且恢复数据远远快于AOF方式
缺点:RDB备份的数据没办法做到实时持久化/秒级持久化,因为bgsave每次运行都要执行fork操作创建子进程,属于重量级操作,频繁执行成本过高
RDB相关参数配置:(redis.conf配置文件中)
save 比如“save m n”。表示m秒内数据集存在n次修改时,自动触发bgsave,可以自行添加;下面为redis.conf默认配置和详细的注释
################################ SNAPSHOTTING ################################
#
# Save the DB on disk:
#
# save
#
# 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 behaviour 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
save 900 1 表示900秒内发生1次更改会执行持久化
save 300 10 表示300秒内发生10次更改会执行持久化
save 60 10000 表示60秒内发生10000次更改会执行持久化
2.stop-writes-on-bgsave-error 默认是yes,启用了RDB且最后一次后台保存数据失败,Redis是否停止接收数据。这会让用户意识到数据没有正确持久化到磁盘上,否则没有人会注意到灾难(disaster)发生了。如果Redis重启了,那么又可以重新开始接收数据了
# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes
3.rdbcompression 默认值是yes,对于存储到磁盘中的快照,可以设置是否进行压缩存储。如果是的话,redis会采用LZF算法进行压缩。如果你不想消耗CPU来进行压缩的话,可以设置为关闭此功能,但是存储在磁盘上的快照会比较大
# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes
4.rdbchecksum 默认值是yes,在存储快照后,我们还可以让redis使用CRC64算法来进行数据校验,但是这样做会增加大约10%的性能消耗,如果希望获取到最大的性能提升,可以关闭此功能
# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes
5.dbfilename 设置快照的文件名,默认是 dump.rdb
# The filename where to dump the DB
dbfilename "dump.rdb"
6.dir 设置快照文件的存放路径,这个配置项一定是个目录,而不能是文件名。默认是和当前配置文件保存在同一目录。也就是说通过在配置文件中配置的 save 方式,当实际操作满足该配置形式时就会进行 RDB 持久化,将当前的内存快照保存在 dir 配置的目录中,文件名由配置的 dbfilename 决定。
# 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 "/usr/local/redis-5.0.3"
以上是关于Redis学习笔记—持久化机制之RDB的主要内容,如果未能解决你的问题,请参考以下文章