8 Redis 持久化RDB

1 RDB 总体介绍

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

单位时间内,更新的key越多,保存的快照间隔时间越短
60分钟改了1次key
5分钟改了100次key
1分钟内改了1w次key
就更新快照DB

# Unless specified otherwise, by default Redis will save the DB:
#   * After 3600 seconds (an hour) if at least 1 key changed
#   * After 300 seconds (5 minutes) if at least 100 keys changed
#   * After 60 seconds if at least 10000 keys changed
#
# You can set these explicitly by uncommenting the three following lines.
#
# save 3600 1
# save 300 100
# save 60 10000

1.1 备份是如何执行的

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

1.2 Fork

Fork的作用是复制一个与当前进程一样的进程。新进程的所有数据(变量、环境变量、程序计数器等)数值和原进程一致,但是是一个全新的进程,并作为原进程的子进程。

在linux程序中,fork()会产生一个核父进程完全相同的子进程,但子进程在此后会exec系统调用,出于效率考虑,linux中引入了“写时复制技术”

一般情况父进程和子进程会公用同一段物理内存,只有进程空间的各段的内容要发生变化时,才会将父进程的内容复制一份给子进程。

1.3 优势

适合大规模的数据恢复
对数据完整性和一致性要求不高更适合使用
节省磁盘空间
恢复速度快

1.4 劣势

Fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑
虽然redis在fork时使用了写时拷贝技术,但是如果数据庞大时还是比较消耗性能
在备份周期在一定间隔时间做一次备份,所以如果reids意外down掉的话,就会丢失最后一次快照后的所有修改

2 SNAPSHOTTING 快照

2.1 save 快照更新时间间隔

save <seconds> <changes>

单位时间内,更新的key越多,保存的快照间隔时间越短
60分钟改了1次key
5分钟改了100次key
1分钟内改了1w次key
就更新快照DB

# Unless specified otherwise, by default Redis will save the DB:
#   * After 3600 seconds (an hour) if at least 1 key changed
#   * After 300 seconds (5 minutes) if at least 100 keys changed
#   * After 60 seconds if at least 10000 keys changed
#
# You can set these explicitly by uncommenting the three following lines.
#
# save 3600 1
# save 300 100
# save 60 10000

2.2 bgsave 异步更新快照

Redis 会再后台异步操作快照,快照的同时还可以响应客户端请求。

2.3 动态停止快照

save 后给空值,表示禁用保存策略

redis-cli config set save "" 

2.4 dbfilename 指定快照db保存的文件名

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

2.5 dir 指定快照的保存路径

# Note that you must specify a directory here, not a file name.
dir ./

2.6 stop-writes-on-bgsave-error 关闭写操作

当redis 不可用时关闭写DB操作

# 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

2.7 rdbchecksum 检查完整性

在存快照后,可以让redis 使用CRC64算法来进行数据校验
这样做会增加10%的性能消耗,如果希望获得最大性能提升,可以关闭此功能。
推荐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

2.8 rdbcompression 压缩文件

对于存储到磁盘中的快照可以设置是否进行压缩,如果是yes的话,redis会采用LZF算法进行压缩。
如果不想消耗CPU来进行压缩的话,可以设置no关闭此功能,推荐yes

# Compress string objects using LZF when dump .rdb databases?
# By default compression is enabled 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值