Redis 提供了不同级别的持久化方式:
在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里,Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到。
一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能。如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。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 ""# 存 DB 到磁盘:## 格式:save <间隔时间(秒)> <写入次数>## 根据给定的时间间隔和写入次数将数据保存到磁盘## 下面的例子的意思是:# 900 秒内如果至少有 1 个 key 的值变化,则保存# 300 秒内如果至少有 10 个 key 的值变化,则保存# 60 秒内如果至少有 10000 个 key 的值变化,则保存# # 注意:你可以注释掉所有的 save 行来停用保存功能。# 也可以直接一个空字符串来实现停用:# save ""save 900 1save 300 10save 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.# 默认情况下,如果 redis 最后一次的后台保存失败,redis 将停止接受写操作,# 这样以一种强硬的方式让用户知道数据不能正确的持久化到磁盘,# 否则就会没人注意到灾难的发生。## 如果后台保存进程重新启动工作了,redis 也将自动的允许写操作。## 然而你要是安装了靠谱的监控,你可能不希望 redis 这样做,那你就改成 no 好了。<br># 如果配置成no 那么表示你不在乎数据的一致性,或者你又其他手段发现合控制。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.# 是否在 dump .rdb 数据库的时候使用 LZF 压缩字符串# 默认都设为 yes# 如果你希望保存子进程节省点 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.# 是否校验rdb文件 但是CRC64算法会增大大约10%的性能消耗rdbchecksum yes# The filename where to dump the DB# 设置 dump 文件的默认文件文件名<br>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.# 工作目录# 例如上面的 dbfilename 只指定了文件名,# 但是它会写入到这个目录下。这个配置项一定是个目录,而不能是文件名。dir ./
注意,当执行类似 flushall 这样的提交命令的时候也会产生新的RDB文件,所以备份的时候执行的RDB文件也是空的。该命令会清除redis在内存中的所有数据。执行该命令后,只要redis中配置的快照规则不 为空, 也就是save 的规则存在。redis就会执行一次快照操作。不管规则是什么样的都会执行。如果没有定义 快照规则,就不会执行快照操作。
恢复时只要把RDB文件移动到redis安装目录下并启动服务,就能自动恢复
除了让Redis自动进行快照以外,当我们对服务进行重启或者服务器迁移我们需要人工去干预备份。 redis提供了两条命令来完成这个任务
save命令 如下图所示,当执行save命令时,Redis同步做快照操作,在快照执行过程中会阻塞所有来自客 户端的请求。当redis内存中的数据较多时,通过该命令将导致Redis较长时间的不响应。所以不建 议在生产环境上使用这个命令,而是推荐使用bgsave命令

bgsave命令 如下图所示,bgsave命令可以在后台异步地进行快照操作,快照的同时服务器还可以继续响应 来自客户端的请求。执行BGSAVE后,Redis会立即返回ok表示开始执行快照操作,在redis-cli终 端,通过 LASTSAVE 这个命令可以获取最近一次成功执行快照的时间(以 UNIX 时间戳格式表示)。
redis在进行快照的过程中不会修改RDB文件,只有快照结束后才会将旧的文件替换成新 的,也就是说任何时候RDB文件都是完整的。 这就使得我们可以通过定时备份RDB文件来实现 redis数据库的备份, RDB文件是经过压缩的二进制文件,占用的空间会小于内存中的数据,更加利于传输。
bgsave是异步执行快照的,bgsave写入的数据就是for进程时redis的数据状态,一旦完成 fork,后续执行的新的客户端命令对数据产生的变更都不会反应到本次快照。
Redis启动后会读取RDB快照文件,并将数据从硬盘载入到内存。根据数据量大小以及服务器性能不 同,这个载入的时间也不同。

RDB 文件 的优势和劣势:
一、优势
RDB 是一个非常紧凑(compact)的文件,它保存了 redis 在某个时间点上的数据集。这种文件非常适合用于进行备份和灾难恢复。
生成 RDB 文件的时候,redis 主进程会 fork()一个子进程来处理所有保存工作,主进程不需要进行任何磁盘 IO 操作。
RDB 在恢复大数据集时的速度比 AOF 的恢复速度要快。
二、劣势
RDB 方式数据没办法做到实时持久化/秒级持久化。因为 bgsave 每次运行都要执行 fork 操作创建子进程,频繁执行成本过高。
在一定间隔时间做一次备份,所以如果 redis 意外 down 掉的话,就会丢失最后一次快照之后的所有修改(数据有丢失)。如果数据相对来说比较重要,希望将损失降到最小,则可以使用 AOF 方式进行持久化。