Redis数据持久化两种方式。RDB与AOF。
1、RDB
RDB是通过快照方式进程持久化的。满足特定的条件拿下,会将内存中的数据快照到硬盘上。RDB进行快照的条件如下
1.1、redis.conf文件配置(windows文件的是redis.windows-service.conf)
################################ 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 "" 设置为空是不开RDB
save 900 1 900秒内,有一个key更改就触发快照
save 300 10 300秒内,有10个key更改就触发快照
save 60 10000 60秒内有10000个key更改。触发快照
# The filename where to dump the DB
dbfilename dump.rdb 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 ./ 保存的路径
如下是在windows系统上运行的Redis serve端的信息
可以看出在3600秒进行了一次save动作
1.2、执行save bgsave
1.3、还行flushall
2、AOF
AOF是将每一条redis命令都写入到磁盘中
############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.
appendonly no AOF持久化开关
# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof" 文件名
# The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log . Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec".
# appendfsync always 每个命令都执行同步
appendfsync everysec 每秒同步
# appendfsync no 不同步
aof文件的内容
3、RDB与AOF的差异
RDB持久化是指在指定的时间间隔内将内存中的数据集快照写入磁盘,实际操作过程是fork一个子进程,先将数据集写入临时文件,写入成功后,再替换之前的文件,用二进制压缩存储。
AOF持久化以日志的形式记录服务器所处理的每一个写、删除操作,查询操作不会记录,以文本的方式记录,可以打开文件看到详细的操作记录。
由上图我们可以提取到如下信息
1、RDB方式持久化的数据要比AOF的小,AOF需要将每一条Redis指令都写入到持久化中。
2、RDB对比AOF,丢失数据的风险更大。备份间隔时间内服务器宕机。
3、AOF恢复数据要比RDB花费更长的时间。