相关文章:
了解完 RDB 之后,我们再来了解下另外一种持久化方式 AOF
一、AOF
-
AOF (Append Only File)
-
以日志的形式记录 Redis 的每一个写操作,将 Redis 执行过的所有写指令保存下来,以追加的形式保存到 AOF 文件 (默认为 appendonly.aof) 中
-
当需要恢复数据时,Redis 会重新执行 AOF 文件中的写指令,来达到恢复数据的目的
-
-
优势
-
具有更高的数据安全性,AOF 默认同步策略为每秒同步一次,因此即使 Redis 服务出现问题,那么最多也只会丢失最近一秒内修改的数据
-
AOF 文件采用追加的形式进行保存,因此即使在写入过程中出现问题,也不会破坏日志文件中已经存在的内容;如果当我们的写入过程只进行了一半就出现了问题,那么在下一次 Redis 启动之前,我们可以通过 redis-check-aof 工具来帮助我们解决数据一致性的问题
-
当日志文件过大时,我们可以通过重写机制来对 AOF 文件进行重写
-
AOF 文件有序地保存了对数据库执行的所有写入操作,格式十分清晰,易于理解
-
-
劣势
-
对于相同的数据集而言,AOF 文件的体积通常要大于 RDB 文件的体积,且恢复速度慢
-
根据同步策略的不同,AOF 的效率往往会低于 RDB,当我们采用同步禁用 (appendfsync no) 策略时,其效率和 RDB 相同
-
二、AOF 相关配置
-
appendonly
############################## 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
- 当 appendonly 设置为 yes 时,表示开启 AOF 备份
-
appendfilename
# The name of the append only file (default: "appendonly.aof") appendfilename "appendonly.aof"
- appendfilename 用于指定 AOF 文件的名称,默认为 appendonly.aof
-
appendfsync
# 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
-
appendfsync 用于指定 AOF 文件的同步策略
指令 作用 appendfsync always 一旦缓存区的内容发生了变化,就将其写入到 AOF 文件中 appendfsync everysec 将缓存区的内容每隔一秒写入到 AOF 文件中 appendfsync no 将写入 AOF 的操作交给操作系统来决定,一般而言为了提高效率,操作系统会等待缓存区域被填满后才会同步数据到磁盘中
-
三、如何解决 AOF 文件大小不断增大的问题?
-
AOF 通过日志重写来解决该问题,原理如下
-
首先调用 fork 指令,来创建一个子进程
-
然后子进程根据当前内存中的数据生成对应指令,并记录在一个临时 AOF 文件中,不需要依赖原有的 AOF 文件 (即不需要读取原有的 AOF 文件进行分析或指令合并)
-
主进程会持续将新的变动同时写入到内存和原有的 AOF 文件中
-
当子进程完成了临时 AOF 文件的创建后,会发送信号给主进程,此时主进程会往临时 AOF 文件中同步增量变动
-
最后使用临时 AOF 文件替换原有的 RDB 文件
-
-
我们可以通过 bgrewriteaof 指令来手动触发 AOF 文件的重写
四、自动触发 AOF 持久化的方式
-
自动触发 AOF 持久化的操作涉及到以下两个参数以及两个状态
# 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
-
两个参数 (可以通过
config get 参数
命令查看):参数 含义 auto-aof-rewrite-min-size 表示进行重写时,AOF 文件最小的体积大小,默认为 64 MB auto-aof-rewrite-percentage 表示进行重写时, (当前 AOF 文件的大小 - 上一次重写时 AOF 文件的大小) / 上一次重写时 AOF 文件的大小
的百分比,默认为 100% -
两个状态 (可以通过
info persistence
命令查看)状态 含义 aof_current_size 当前 AOF 文件的大小 aof_base_size 上一次重写时 AOF 文件的大小 -
只有当 auto-aof-rewrite-min-size 和 auto-aof-rewrite-percentage 两个参数同时满足时,才会触发 AOF 的重写机制
-