Redis4.0持久化配置

版权说明:本文为博主原创,如果转载请注明来源。作为学习笔记,不能保证所有知识点是完全正确以及表达无误,用于生产环境配置时请斟酌。如有错误或建议请联系。侵删联系:linuxops@foxmail.com。感谢各位!

Redis持久化--RDB和AOF

持久化(Persistence),即把数据(如内存中的对象)保存到可永久保存的存储设备中(如磁盘)。持久化的主要应用是将内存中的对象存储在的数据库中,或者存储在磁盘文件中、XML数据文件中等等。Redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到磁盘来保证持久化。redis支持四种持久化方式,一是 Snapshotting(快照)也是默认方式;二是Append-only file(缩写aof)的方式;三是虚拟内存方式;四是diskstore方式。本文将介绍快照和AOF两种持久化方式。

一、快照RDB

RDB实在在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里。

Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能,如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。RDB的缺点是最后一次持久化后的数据可能丢失。

redis会fork一个子进程来进行持久化,fork的作用是复制一个与当前进程一样的进程。新进程的所有数据(变量、环境变量、程序计数器等)数值都和原进程一致,但是是一个全新的进程,并作为原进程的子进程。所以fork一份就会增加一倍的资源消耗.

RDB保存的是dump.rdb文件,保存路径在配置文件中指定。默认配置文件为“./”,以默认的配置文件,dump.rdb保存在redis启动的目录,可以在cli中用“CONFIG GET DIR”查询。

如下是dump.rdb默认的路径:

# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./

这个时候我在“/opt”中启动redis,那么dump.rdb就会保存在/opt中

[root@Redis01 opt]# 
[root@Redis01 opt]# pwd
/opt
[root@Redis01 opt]# ls
[root@Redis01 opt]# /usr/redis/bin/redis-server /usr/redis/redis.conf
[root@Redis01 opt]# /usr/redis/bin/redis-cli -p 6379
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> 
127.0.0.1:6379> SHUTDOWN
not connected> exit
[root@Redis01 opt]# ls
dump.rdb
[root@Redis01 opt]# pwd
/opt
[root@Redis01 opt]# ls -la
total 12
drwxr-xr-x.  2 root root 4096 Dec  7 06:26 .
dr-xr-xr-x. 22 root root 4096 Dec  7 06:24 ..
-rw-r--r--.  1 root root   88 Dec  7 06:26 dump.rdb
[root@Redis01 opt]#

如果配置文件指定一个绝对路径,那么无论是在哪一个目录中启动redis,dump.rdb都将保存在指定的目录中。

修改一下配置文件:

# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /usr/redis/dump
#修改dump.rdb文件保存的绝对路径

修改之后重启启动redis,让他产生dump.rdb文件

[root@Redis01 opt]# ls 
dump.rdb
[root@Redis01 opt]# rm -f dump.rdb 
[root@Redis01 opt]# ls
[root@Redis01 opt]# /usr/redis/bin/redis-server /usr/redis/redis.conf
[root@Redis01 opt]# /usr/redis/bin/redis-cli -p 6379
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> shotdown
(error) ERR unknown command 'shotdown'
127.0.0.1:6379> exit
[root@Redis01 opt]# ls /usr/redis/dump/
dump.rdb
[root@Redis01 opt]#

以上,配置文件指定了dump.rdb文件的绝对路径,那么无论在任何路径重启动redis,dump.rdb将会保存到指定的路径中。 所以建议在配置文件中指定dump.rdb的保存路径。

redis快照根据满足时间片段中修改的key个数来备份到dump.rdb中的。

例如一下是redis默认的配置文件:

#
#   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
在900秒内有1个key被改动,自动保存到dump.rdb文件中

save 300 10
在300秒内有10个key被改动,自动保存到dump.rdb文件中

204 save 60 10000
在60秒内有10000个key被改动,自动保存到dump.rdb文件中

以上3中条件任意一种被满足就会触发保存

RDB快照有以下几种方式产生:

  • 通过配置文件触发条件,redis自动dump。
  • 在cli通过命令手动产生dump:save时只管保存,其它不管,全部阻塞
  • 在cli通过命令BGSAVE手动产生:Redis会在后台异步进行快照操作,快照同时还可以响应客户端请求。

可以通过lastsave命令获取最后一次成功执行快照的时间

  • 执行flushall命令,也会产生dump.rdb文件,但里面是空的,不仅无意义,而且在生产环境中禁止使用。
  • shutdown也会产生rdb文件,生产环境中比较少用到。

RDB快照使用方式:

  • 将备份文件 (dump.rdb) 移动到 redis 配置文件中配置好的rdb目录中,重启rendis就会恢复dump中的内容。
  • 在cli中可以通过CONFIG GET DIR 获取目录

二、AOF:Append Only File

AOF以日志的形式来记录每个写操作,将Redis执行过的所有写指令记录下来(读操作不记录),只许追加文件但不可以改写文件,redis启动之初会读取该文件重新构建数据,换言之,redis重启的话就根据日志文件的内容将写指令从前到后执行一次以完成数据的恢复工作。

Aof默认保存的是appendonly.aof文件。保存的路径和RDB快照配置中 dir 配置的同一个路径

以下是AOF配置文件:

############################## 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,默认no

# The name of the append only file (default: "appendonly.aof")

appendfilename "appendonly.aof"
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".

Redis将OS数据缓冲区中数据刷新到磁盘的策略:

# appendfsync always
  同步持久化,每次发生数据变化会被立刻记录到磁盘(性能差,但是数据完整性较好)

appendfsync everysec
出厂默认设置,异步操作,每秒记录(如果一秒内宕机,数据会丢失一秒)

# appendfsync no
  当设置appendfsync为no的时候,Redis不会主动调用fsync去将AOF日志内容同步到磁盘,
  所以这一切就完全依赖于操作系统的调试了。对大多数Linux操作系统,是每30秒进行一次fsync,
  将缓冲区中的数据写到磁盘上。

# When the AOF fsync policy is set to always or everysec, and a background
# saving process (a background save or AOF log background rewriting) is
# performing a lot of I/O against the disk, in some Linux configurations
# Redis may block too long on the fsync() call. Note that there is no fix for
# this currently, as even performing fsync in a different thread will block
# our synchronous write(2) call.
#
# In order to mitigate this problem it's possible to use the following option
# that will prevent fsync() from being called in the main process while a
# BGSAVE or BGREWRITEAOF is in progress.
#
# This means that while another child is saving, the durability of Redis is
# the same as "appendfsync none". In practical terms, this means that it is
# possible to lose up to 30 seconds of log in the worst scenario (with the
# default Linux settings).
#
# If you have latency problems turn this to "yes". Otherwise leave it as
# "no" that is the safest pick from the point of view of durability.

no-appendfsync-on-rewrite no
当rewrite AOF子进程或RDB子进程正在执行时,Server是否支持fsync,
即当新修改的数据写入AOF文件后,是否将数据刷新到硬盘。
默认配置为on,即当rewrite子进程正在执行的时候要调用fsync(无论后台是否有子进程在刷盘)。
Redis在后台写RDB文件或重写afo文件期间会存在大量磁盘IO,
此时,在某些linux系统中,调用fsync可能会阻塞。

# Automatic rewrite of the append only file.
# Redis is able to automatically rewrite the log file implicitly calling
# BGREWRITEAOF when the AOF log size grows by the specified percentage.
#
# This is how it works: Redis remembers the size of the AOF file after the
# latest rewrite (if no rewrite has happened since the restart, the size of
# the AOF at startup is used).
#
# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached but it is still pretty small.
#
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

"auto-aof-rewrite-percentage 100":指定Redis重写aof文件的条件,默认为100,
表示与上次rewrite的aof文件大小相比,当前aof文件增长量超过上次afo文件大小的100%时,
就会触发background rewrite。若配置为0,则会禁用自动rewrite。

"auto-aof-rewrite-min-size 64mb":指定触发rewrite的aof文件大小。若aof文件小于该值,
即使当前文件的增量比例达到auto-aof-rewrite-percentage的配置值,也不会触发自动rewrite。
即这两个配置项同时满足时,才会触发rewrite

# An AOF file may be found to be truncated at the end during the Redis
# startup process, when the AOF data gets loaded back into memory.
# This may happen when the system where Redis is running
# crashes, especially when an ext4 filesystem is mounted without the
# data=ordered option (however this can't happen when Redis itself
# crashes or aborts but the operating system still works correctly).
#
# Redis can either exit with an error when this happens, or load as much
# data as possible (the default now) and start if the AOF file is found
# to be truncated at the end. The following option controls this behavior.
#
# If aof-load-truncated is set to yes, a truncated AOF file is loaded and
# the Redis server starts emitting a log to inform the user of the event.
# Otherwise if the option is set to no, the server aborts with an error
# and refuses to start. When the option is set to no, the user requires
# to fix the AOF file using the "redis-check-aof" utility before to restart
# the server.
#
# Note that if the AOF file will be found to be corrupted in the middle
# the server will still exit with an error. This option only applies when
# Redis will try to read more data from the AOF file but not enough bytes
# will be found.

aof-load-truncated yes
指redis在恢复时,会忽略最后一条可能存在问题的指令。默认值yes。
即在aof写入时,可能存在指令写错的问题(突然断电,写了一半),
这种情况下,yes会log并继续,而no会直接恢复失败.

AOF触发条件:

AOF根据配置文件指定fsync方式实时或者定时追加写入到aof文件。可以在cli中使用命令bgrewriteaof来触发。

AOF的重写:

AOF采用文件追加方式,文件会越来越大,为避免出现此种情况,新增了重写机制,当AOF文件的大小超过所设定的阈值时,Redis就会启动AOF文件的内容压缩,只保留可以恢复数据的最小指令集。

redis会fork出一条新进程来将文件重写(也是先写临时文件最后再rename),遍历新进程的内存中数据,每条记录有一条的Set语句。

重写aof文件的操作,并没有读取旧的aof文件,而是将整个内存中的数据库内容用命令的方式重写了一个新的aof文件,这点和快照有点类似。

重写触发条件

Redis会记录上次重写时的AOF大小,默认配置是当AOF文件大小是上次rewrite后大小的一倍且文件大于64M时触发。

优点:1、提供多种fsync机制,满足不同的场景需求。2、appendfsync always和appendfsync everysec 适合数据完整性较强的场景。

劣势:1、相同数据集的数据而言aof文件要远大于rdb文件,恢复速度慢于rdb。2、aof运行效率要慢于rdb,每秒同步策略效率较好,不同步效率和rdb相同。

注意:RDB快照和AOF两种持久化同时开启,重启redis时优先读取aof文件。如果aof文件损坏,重启redis时不会抛出异常,但是启动不会成功。aof文件修复命令:redis_check_aof --fix appendonly.aof;RDB文件修复命令:redis_check_rdb --fix dump.rdb