redis(4)AOF持久化

1、介绍

AOF机制对每条写入命令作为日志,以append-only的模式写入一个日志文件中,在redis重启的时候,可以通过回放AOF日志中的写入指令来重新构建整个数据集。

2、AOF持久化机制的优点

  1. AOF可以更好的保护数据不丢失,AOF默认每隔1秒,通过一个后台线程执行一次fsync操作,最多丢失1秒钟的数据每隔1秒,就执行一次fsync操作,保证os cache中的数据写入磁盘中redis进程挂了,最多丢掉1秒钟的数据;
  2. AOF日志文件以append-only模式写入,所以没有任何磁盘寻址的开销,写入性能非常高,而且文件不容易破损,即使文件尾部破损,也很容易修复;
  3. AOF日志文件即使过大的时候,出现后台重写操作,也不会影响客户端的读写。因为在rewrite log的时候,会对其中的指导进行压缩,创建出一份需要恢复数据的最小日志出来。再创建新日志文件的时候,老的日志文件还是照常写入。当新的merge后的日志文件ready的时候,再交换新老日志文件即可;
  4. AOF日志文件的命令通过非常可读的方式进行记录,这个特性非常适合做灾难性的误删除的紧急恢复。比如不小心用flushall命令清空了所有数据,只要这个时候后台rewrite还没有发生,那么就可以立即拷贝AOF文件,将最后一条flushall命令删除,然后再将该AOF文件放回去,就可以通过恢复机制,自动恢复所有数据;

    3、AOF持久化机制的缺点

  5. 对于同一份数据来说,AOF日志文件通常比RDB数据快照文件更大;
  6. AOF开启后,支持的写QPS会比RDB支持的写QPS低,因为AOF默认配置每秒fsync一次日志文件;
  7. 使用AOF做数据恢复时,会比较慢,还有做冷备,定期的备份,不太方便,可能要自己手写复杂的脚本去做,做冷备不太合适;

    4、数据恢复顺序

    当redis同时有RDB和AOF两种持久化备份时,redis优先使用AOF进行数据还原。若AOF文件不存在,redis则创建一个新的AOF文件,而不从RDB恢复数据。
    若需要从RDB恢复数据,应:
    1 . 停止redis;
    2 . 修改redis配置文件关闭AOF;
    3 . 拷贝RDB文件到redis目录下;
    4 . 启动redis,确保数据已经恢复;
    5 . 在redis命令行中执行以下命令热修改redis配置开启AOF持久化;
    1
    config set appendonly yes

6 . 此时redis重新生成AOF持久化文件,且AOF和RDB的数据已经同步;
7 . 停止redis,修改redis配置文件打开AOF持久化,启动redis;

5、配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
############################## 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

# 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
  1. appendonly no表示redis默认关闭AOF持久化,通过修改no为yes打开AOF持久化;
  2. appendfsync always/everysec/no表示AOF的持久化频率,always表示redis每变化一个值就进行一次AOF持久化;everysec表示每秒进行一次AOF持久化,也是redis的默认配置;no表示redis不主动进行AOF持久化,完全由系统触发AOF持久化,可控性差;
>