Redis 中的 存储策略
rdb 与 aof 同时开启的选择优先级
rdb是什么
Redis 如何保存 RDB 文件 (Fork 一个子线程)
默认文件名
存放位置
如何触发RDB快照
如何恢复
RDB优势
RDB劣势
如何停止
完整配置
接下来从下上面的几个点对Redis 的 RDB 做一个介绍
========== Redis 中的 存储策略
Redis 中的数据持久化(存储策略)主要分为 RDB 跟 AOF 两大类
RDB (Redis Database)
- RDB持久化方式能够在指定的时间间隔能对你的数据进行快照存储.
AOF (Append Only File)
- AOF持久化方式记录每次对服务器写的操作,当服务器重启的时候会重新执行这些命令来恢复原始的数据,
- AOF命令以redis协议追加保存每次写的操作到文件末尾.
- Redis还能对AOF文件进行后台重写,使得AOF文件的体积不至于过大.
============ rdb与aof同时开启的选择优先级
- 你也可以同时开启两种持久化方式
- 在这种情况下, 当redis重启的时候会优先载入AOF文件来恢复原始的数据,
- 因为在通常情况下AOF文件保存的数据集要比RDB文件保存的数据集要完整.
=============== rdb是什么
RDB 全称 Redis Database
Redis 的数据快照文件。
在指定的时间间隔内将内存中的数据集快照写入磁盘,
也就是Snapshot快照,恢复时将快照文件(xx.rdb )直接读入到内存中
================ Redis 如何保存 rdb 文件
Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到一个临时文件中,
待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。
整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能
如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方
式要比AOF方式更加的高效。
RDB的缺点是最后一次持久化后的数据可能丢失。
Fork :
fork的作用是复制一个与当前进程一样的进程。
新进程的所有数据(变量、环境变量、程序计数器等)数值都和原进程一致,
但是是一个全新的进程,并作为原进程的子进程
============= RDB 默认的文件名 dump.rdb
在 redis.conf 中可以配置,可更改
# The filename where to dump the DB
dbfilename dump.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快照
1.在Redis 的 redis.conf 中设置快照的策略
save <seconds> <changes>
seconds 秒 里 发生 changes 更改生成 快照文件 dump.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
2.执行 save / bgsave
save 指令:
save 指令会阻塞当前进程, 即redis 不能 读写, 直到 save 完成
bgsave 指令:
redis 会在后台异步进行快照操作,快照同时可以响应客户端请求。
可以通过执行lastsave命令获取最后一次执行快照的时间。
3.执行 flushall
执行flushall 清空所有库的所有键,也会产生 dump.rdb ,
但是这种情况是对 flushall 之后的时间点进行的,所以 .rdb 文件的内容为空
=============== 如何恢复
将 dump.rdb 移到到redis 的工作目录,启动Redis 服务即可
================ RDB优势
-
- RDB是一个非常紧凑的文件,它保存了某个时间点得数据集,非常适用于数据集的备份,比如你可以在每个小时报保存一下过去24小时内的数据,同时每天保存过去30天的数据,这样即使出了问题你也可以根据需求恢复到不同版本的数据集.
- RDB是一个紧凑的单一文件,很方便传送到另一个远端数据中心或者亚马逊的S3(可能加密),非常适用于灾难恢复.
- RDB在保存RDB文件时父进程唯一需要做的就是fork出一个子进程,接下来的工作全部由子进程来做,父进程不需要再做其他IO操作,所以RDB持久化方式可以最大化redis的性能.
- 与AOF相比,在恢复大的数据集的时候,RDB方式会更快一些.
================ RDB 劣势
- 如果你希望在redis意外停止工作(例如电源中断)的情况下丢失的数据最少的话,那么RDB不适合你.虽然你可以配置不同的save时间点(例如每隔5分钟并且对数据集有100个写的操作),是Redis要完整的保存整个数据集是一个比较繁重的工作,你通常会每隔5分钟或者更久做一次完整的保存,万一在Redis意外宕机,你可能会丢失几分钟的数据.
- RDB 需要经常fork子进程来保存数据集到硬盘上,当数据集比较大的时候,fork的过程是非常耗时的,可能会导致Redis在一些毫秒级内不能响应客户端的请求.如果数据集巨大并且CPU性能不是很好的情况下,这种情况会持续1秒,AOF也需要fork,但是你可以调节重写日志文件的频率来提高数据集的耐久度.
=============== 如何停止
如果不想让Redis 做 rdb 保存 , 数据只在 server 启动的时候存在,可以这么设置:
动态所有停止RDB保存规则的方法:
redis-cli config set save ""
=============== 完整配置
################################ 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 ./
Redis 中的 存储策略
rdb与aof同时开启的选择优先级
.rdb是什么
.fork
.rdb保存的是 dump.rdb 文件
.配置位置
.如何触发RDB快照
.如何恢复
.优势
.劣势
.如何停止