redis 持久化(rdb,aof)

redis持久化有两种方式

  1. RDB

                 RDB持久化是redis默认的持久化方式,指每隔一段时间将内存中的数据以二进制的方式持久化到磁盘。查看redis的配置文件 redis.windows.conf

# 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

默认情况下,save命令有三种情况:

save 900 1    :  每隔900s,且至少有1个key  changed
save 300 10   :  每隔300s,且至少有10个key  changed
save 60 10000  :  每隔60s,且至少有10000个key   changed


满足这三种情况中的任何一种,redis会进行持久化,在默认的路径下面生成.rdb文件

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

除了redis默认的方式,还可以客户端手动执行save,bgsave命令

save: redis服务处于阻塞状态,不能响应其他客户端的请求,直到持久化完成。

bgsave: 即background save,redis会fork一个子进程来完成数据的持久化工作。不会影响阻塞redis服务

redis重启的时候会通过.rdb文件自动进行数据的恢复工作。

       2.AOF

            AOF是指通过文件的方式记录记录redis的写,删除操作(操作,而不是数据),查询操作不会记录。文件中可以看到详细的操作记录,例如

*2
$6
SELECT
$1
0
*3
$3
SET
$1
?
$1
1

默认情况下,AOF功能是关闭的,.aof文件的路径可以在配置文件中指定

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

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

AOF持久化可以配置appendfsync属性:

appendfsync   no:操作系统决定什么时候进行持久化,速度更快

appendfsync  always:   每次进行写操作后,进行持久化,安全性更高,速度相对慢

appendfsync  everysec: 默认的配置,每秒进行一次持久化,相比上述两种方式,效率折中

# 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

客户端可以手动执行bgrewirteaof命令进行AOF持久化,生成一个appendonly.aof(默认)文件。redis启动的时候会优先从appendonly.aof中恢复数据。

AOF文件重写:

 开启AOF的时候,随着时间的推移,aof文件会越来越大,redis在满足一定条件的情况会进行bgrewirteaof,优化文件,减小文件的大小,有利于数据的恢复

# 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

RDB和AOF两种方式比较:

    1.RDB是以二进制方式记录数据,AOF是以文本方式记录操作

    2.AOF的安全性比RDB高,RDB是间隔一段时间进行持久化,如果持久化之间redis发生故障,会发生数据丢失。所以这种方式更适合数据要求不是特别严格的时候

    3.AOF文件一般比RDB文件大,恢复速度比RDB方式慢

相关命令:

  info persistence

  bgsave

  save

  bgrewriteaof

猜你喜欢

转载自www.cnblogs.com/chenzhubing/p/11346106.html