Redis持久化_RDB

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35977237/article/details/79875800

Redis提供了两个不同形式的持久化方式
RDB -> Redis DataBase
AOF -> Append Of File


################################ SNAPSHOTTING  ################################
RDB
在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是行话的Snapshot快照,它恢复时是将快照文件直接读到内存里

RDB是整个内存的压缩过的Snapshot,RDB的数据结构,可以配置符合的快照触发条件

备份是如何执行的?
Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能。 如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高校,RDB的 缺点是最后一次持久化后的数据可能丢失

关于fork
在Linux程序中,fork()会产生一个和父进程完全相同的子进程,子进程的所有数据(变量、环境变量、程序计数器等)数值都和原进程一致,但是是一个全新的进程,但子进程在此后会exec系统调用,出于效率考虑,Linux中引入了“写时复制技术”, 一般情况下父进程和子进程会共用一段物理内存,只有进程空间的各段的内容都要发生变化时,才会将父进程的内容复制一份给子进程

rdb的保存的文件
在redis.conf中配置文件名称,默认为dump.rdb
# The filename where to dump the DB
dbfilename dump.rdb

rdb文件的保存路径也可以修改。默认为Redis启动时命令行所在的目录下
# 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 ./

rdb的保存策略
#
# 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 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

即默认
15分钟内修改1次
5分钟内修改10次
1分钟内修改10000次

如果想禁用RDB持久化的策略,只要不设置任何save指令,或者给save传入一个空字符串参数即可。
手动保存快照
命令save:只管保存,其它不管,全部堵塞
bgsave:Redis会在后台异步进行快照操作,快照同时还可以响应客户端请求。可以通过lastsave命令获取最后一次成功执行快照的时间
save vs bgsave
bgsave单独开辟了一个子进程进程持久化操作
save使用主进程进行持久化操作
TIP:执行flushall命令也会产生dump.rdb文件,但其内容为空,无意义

stop-writers-on-bgsave-error
当Redis无法写入磁盘的话,是否直接关闭Redis的写操作,默认为yes
即,如果设置为no,则表示你不在乎数据不一致或者有其他的手段发现和控制
# 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

rdbcompression
进行rdb保存时,是否将文件进行压缩操作,默认为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

rdbchecksum
在存储快照后,是否让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


rdb的备份
先通过config get dir 查询 rdb文件 的目录
将*.rdb的文件拷贝到别的地方
rdb的恢复
关闭Redis
将备份的文件拷贝到工作目录下
启动Redis,备份数据会直接加载



rdb的优点
适合大规模的数据恢复
对数据完整性和一致性要求不高
rdb的缺点
在备份周期中,一定间隔时间做一次备份,所以如果Redis意外down掉的话,就会丢失最后一次快照后的所有修改
虽然Redis在fork时使用了写时拷贝技术,但是如果数据庞大时还是会占用cpu性能


动态所有停止RDB保存规则的方法
其可以类似于配置文件的save部分指定为"",会禁止所有RDB保存操作
redis-cli config set save ""

RDB VS AOF
官方推荐两个都启用
如果对数据不敏感,可以单独使用RDB
不建议单独使用AOF,因为可能会出现Bug
如果只是做纯内存缓存,可以两者都不使用



关于RDB持久化的全部配置文件
################################ 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 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

# 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

# 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

# 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

# The filename where to dump the DB
dbfilename dump.rdb

# 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 ./

猜你喜欢

转载自blog.csdn.net/qq_35977237/article/details/79875800