Redis—B站学习—redis持久化

RDB(Redis DataBase):目前本人理解是Redis默认持久化配置是开启RDB的

1.redis持久化之RDB是什么

  1. 在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里
  2. Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。
  3. 整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。
  4. RDB的缺点是最后一次持久化后的数据可能丢失。

2.Fork的作用

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

3.RDB保存

  1. rdb 保存的是dump.rdb文件

4.RDB配置文件中的位置

  1. 配置文件的内容
  1. save: save 秒钟 写操作次数,例:save 120 10表示120秒内操作10次key就会进行rdb持久化,保存到磁盘中指定路径的rdb文件中,如果需要即刻生效,那么就在操作之后打出save命令并且执行
  2. RDB是整个内存的压缩过的Snapshot,RDB的数据结构,可以配置符合的触发条件 save 900 1
    save 300 10
    save 60 10000
  3. 禁用RDB方法
  1. 您可以通过注释掉所有“保存”行来完全禁用保存。
  2. 可以通过添加带有单个空字符串参数的save指令来删除所有先前配置的保存点,例:save ""
  1. stop-writes-on-bgsave-error yes:表示数据出错了就刹车,不保存了。如果配置成no,表示你不在乎数据不一致或者有其他的手段发现和控制
  2. rdbcompression yes:默认是yes,对于存储到磁盘中的快照,可以设置是否进行压缩存储。如果是的话,redis会采用LZF算法进行压缩。如果你不想消耗CPU来进行压缩的话,可以设置为关闭此功能
  3. rdbchecksum:在存储快照后,还可以让redis使用CRC64算法来进行数据校验,但是这样做会增加大约10%的性能消耗,如果希望获取到最大的性能提升,可以关闭此功能
################################ 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.
#
//在下面的示例中,行为将是保存(触发RDB)
#   In the example below the behaviour will be to save:
//15分钟内至少改一个key
#   after 900 sec (15 min) if at least 1 key changed
//5分钟内至少改10次key
#   after 300 sec (5 min) if at least 10 keys changed
//1分钟内至少改10000次key
#   after 60 sec if at least 10000 keys changed
#
//禁用RDB的方法一下两种
//1.您可以通过注释掉所有“保存”行来完全禁用保存。
#   Note: you can disable saving completely by commenting out all "save" lines.
#
//2.还可以通过添加带有单个空字符串参数的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

//表示数据出错了就刹车,不保存了。如果配置成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

//默认是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

//在存储快照后,还可以让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

//转储数据库的文件名
# The filename where to dump the DB
dbfilename dump.rdb

# Remove RDB files used by replication in instances without persistence
# enabled. By default this option is disabled, however there are environments
# where for regulations or other security concerns, RDB files persisted on
# disk by masters in order to feed replicas, or stored on disk by replicas
# in order to load them for the initial synchronization, should be deleted
# ASAP. Note that this option ONLY WORKS in instances that have both AOF
# and RDB persistence disabled, otherwise is completely ignored.
#
# An alternative (and sometimes better) way to obtain the same effect is
# to use diskless replication on both master and replicas instances. However
# in the case of replicas, diskless is not always an option.
rdb-del-sync-files no

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

5.如何触发RDB快照

  1. 配置文件中默认的快照配置,冷拷贝后重新使用,可以cp dump.rdb dump_new.rdb备份一下RDB持久化文件,主机和备机一定要是两台
  2. 命令save或者是bgsave
  1. Savesave时只管保存,其它不管,全部阻塞
  2. BGSAVE:Redis会在后台异步进行快照操作,快照同时还可以响应客户端请求。可以通过lastsave命令获取最后一次成功执行快照的时间
  1. 执行flushall命令,也会产生dump.rdb文件,但里面是空的,再执行dump.rdb文件无意义,将不会有数据

6.如何恢复

  1. 将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务即可
  2. CONFIG GET dir获取目录

7.优势

  1. 适合大规模的数据恢复
  2. 对数据完整性和一致性要求不高
  3. 官网内容:

8.劣势

  1. 在一定间隔时间做一次备份,所以如果redis意外down掉的话,就会丢失最后一次快照后的所有修改
  2. fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑
  3. 官网内容:

9.如何停止RDB持久化

  1. 动态所有停止RDB保存规则的方法:redis-cli config set save “”

10.RDB小总结

redis 容器默认配置文件路径 redis默认rdb_Redis