Redis数据持久化之RDB模式

1、RDB是什么?

  • RDB(Redis DataBase):是指在指定的时间间隔内将内存中的数据集快照写入磁盘,即Snapshot快照,RDB保存的是dump.rdb文件 它恢复时是将当前启动目录下的快照文件(dump.rdb)重新加载回内存。

2、RDB的工作原理

  • Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。RDB的缺点是最后一次持久化后的数据可能丢失。
  • Fork的作用是复制一个与当前进程一样的进程。新进程的所有数据(变量、环境变量、程序计数器等)数值都和原进程一致,但是是一个全新的进程,并作为原进程的子进程。

3、RDB的配置文件详解

################################ SNAPSHOTTING(快照)  ################################
#
# Save the DB on disk:
# 将数据库保存到磁盘上:
#
#   save <seconds> <changes>
#   语法:save 时间(s) 更改次数(get操作不计算在其中)
#
#   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
#	900秒(15分钟)后,如果至少更改了一个键
#
#   after 300 sec (5 min) if at least 10 keys changed
#	300秒(5分钟)后,如果至少更改了10个键 
#
#   after 60 sec if at least 10000 keys changed
#   3600秒(1小时)后,如果至少更改了10个键 
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#	注意:可以通过注释所有save配置来禁用快照
#
#   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 ""

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.
# 默认情况下,如果启用RDB快照,Redis将停止接受写操作(至少一个保存点)和最新的后台保存失败。
#
# 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.
# 如果后台保存过程再次开始工作,Redis将自动允许再次写入。 
#
# 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.
# 但是,如果您已经设置了对redis服务器的适当监视以及持久性,您可能希望禁用此功能,以便Redis即使磁盘有问题,也要照常工作。权限等等。
#
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.
# 对于存储到磁盘中的快照,可以设置是否进行压缩存储。如果是的话,redis会采用LZF算法进行压缩。如果你不想消耗CPU来进行压缩的话,可以设置为no关闭此功能
#
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.
# 
#在存储快照后,还可以让redis使用CRC64算法来进行数据校验,但是这样做会增加大约10%的性能消耗,如果希望获取到最大的性能提升,可以设置为no关闭此功能
#
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.
# 数据库将写入此目录中,并指定文件名上面使用了“dbfilename”配置指令。
#
# The Append Only File will also be created inside this directory.
# 只追加的文件也将在此目录中创建。 
#
# Note that you must specify a directory here, not a file name.
# 注意,必须在此处指定目录,而不是文件名。
#
dir ./

4、如何触发RDB快照?

① redis.conf中的快照配置
② save命令:save时只管保存,其它全部阻塞(get/set/del)。
③ bgsave命令:Redis会在后台异步进行快照操作,快照同时还可以响应客户端请求。可以通过lastsave命令获取最后一次成功执行快照的时间。
④ flushAll命令:也会产生dump.rdb文件、但没有任何内容。

5、RDB如何恢复?

  • 将备份的dump.rdb文件移动到redis服务启动目录下即可,可以通过 config get dir获取目录。

6、RDB持久化的优缺点?

  • 优点:,
    • 适合大规模的数据恢复。
    • 对数据的完整性和一致性要求不高。
  • 缺点:
    • 在一定间隔时间做一次备份,所以如果redis意外down掉的话,就会丢失最后一次快照后的所有修改。
    • Fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑。
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值