Redis 7.x 系列【16】持久化机制之 AOF

news2024/10/5 10:40:00

有道无术,术尚可求,有术无道,止于术。

本系列Redis 版本 7.2.5

源码地址:https://gitee.com/pearl-organization/study-redis-demo

1. 概述

默认情况下,Redis 使用RDB持久化,但如果进程出现问题或电源中断,可能会导致几分钟的写入数据丢失(具体取决于配置的保存点)。

Append Only FileAOF)是另一种持久化模式,它提供了更好的持久性保证。AOF以日志的形式来记录每个写操作,将Redis执行过的所有写指令记录下来(读操作不记录),只许追加文件但不可以改写文件,Redis启动之初会读取该文件重新构建数据。换言之,重启的话就根据日志文件的内容,将写指令从前到后执行一次以完成数据的恢复工作。

执行流程:

  • 客户端的请求写命令会被追加(append)到AOF缓冲区内。
  • AOF缓冲区根据AOF持久化策略(always, everysec, no)将操作同步(sync)到磁盘的AOF文件中。
    • always:始终同步,每次Redis的写入都会立刻记入日志,性能较差但数据完整性比较好。
    • everysec:每秒同步,每秒记入日志一次,如果宕机,本秒的数据可能丢失。
    • no:自己不进行更新日志的行为,而是等操作系统自动调度的时候,再去刷磁盘。
  • AOF文件大小超过重写策略或手动重写时,会对AOF文件进行重写(rewrite),压缩AOF文件容量。
  • Redis服务重启时,会重新加载(loadAOF文件中的写操作达到数据恢复的目的。
    在这里插入图片描述

优点:

  • 备份机制更稳健,丢失数据概率更低。
  • 可读的日志文本,通过操作AOF文件,可以处理误操作。

缺点:

  • 比起RDB占用更多的磁盘空间。
  • 恢复备份速度要慢。
  • 每次读写都同步的话,有一定的性能压力。
  • 存在个别Bug,造成不能恢复。

2. 配置项

redis.confAOF 相关的配置如下:

############################## 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 https://redis.io/topics/persistence for more information.

appendonly no

# The base name of the append only file.
#
# Redis 7 and newer use a set of append-only files to persist the dataset
# and changes applied to it. There are two basic types of files in use:
#
# - Base files, which are a snapshot representing the complete state of the
#   dataset at the time the file was created. Base files can be either in
#   the form of RDB (binary serialized) or AOF (textual commands).
# - Incremental files, which contain additional commands that were applied
#   to the dataset following the previous file.
#
# In addition, manifest files are used to track the files and the order in
# which they were created and should be applied.
#
# Append-only file names are created by Redis following a specific pattern.
# The file name's prefix is based on the 'appendfilename' configuration
# parameter, followed by additional information about the sequence and type.
#
# For example, if appendfilename is set to appendonly.aof, the following file
# names could be derived:
#
# - appendonly.aof.1.base.rdb as a base file.
# - appendonly.aof.1.incr.aof, appendonly.aof.2.incr.aof as incremental files.
# - appendonly.aof.manifest as a manifest file.

appendfilename "appendonly.aof"

# For convenience, Redis stores all persistent append-only files in a dedicated
# directory. The name of the directory is determined by the appenddirname
# configuration parameter.

appenddirname "appendonlydir"

# 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

# 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 no". 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

# 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

# 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 can create append-only base files in either RDB or AOF formats. Using
# the RDB format is always faster and more efficient, and disabling it is only
# supported for backward compatibility purposes.
aof-use-rdb-preamble yes

# Redis supports recording timestamp annotations in the AOF to support restoring
# the data from a specific point-in-time. However, using this capability changes
# the AOF format in a way that may not be compatible with existing AOF parsers.
aof-timestamp-enabled no

2.1 appendonly

appendonly 用于控制是否启用AOF持久化。

appendonly no

被设置为 no(默认) 时,Redis 不会使用 AOF 持久化机制。

2.2 appendfilename

appendfilename 用于配置 AOF 文件的基础名称。

appendfilename "appendonly.aof"

Redis 7 及更高版本使用一组仅追加文件进行AOF,文件主要有两种类型:

  • 基础文件:是数据集在文件创建时完整状态的快照。基础文件可以是 RDB(二进制序列化)格式或 AOF(文本命令)格式。
  • 增量文件:包含在上一个文件之后对数据集应用的其他命令。

此外,还使用清单文件来跟踪文件的创建顺序,以及它们应该被应用的顺序。

Redis 根据特定的模式创建AOF文件的名称。文件名的前缀基于appendfilename配置参数,后面跟着关于序列和类型的额外信息。

例如,如果 appendfilename被设置为appendonly.aof,那么可能会产生以下文件名:

  • appendonly.aof.1.base.rdb 作为基础文件。
  • appendonly.aof.1.incr.aof, appendonly.aof.2.incr.aof 作为增量文件。
  • appendonly.aof.manifest 作为清单文件。

2.3 appenddirname

appenddirname 配置AOF文件的存储目录。

appenddirname "appendonlydir"

2.4 appendfsync

appendfsync 用于配置AOF缓冲区将操作同步(sync)到磁盘的AOF文件中的策略。

# appendfsync always
appendfsync everysec
# appendfsync no

fsync() 是一个系统调用,它告诉操作系统将数据实际写入磁盘,而不是等待更多的数据进入输出缓冲区。不同的操作系统对 fsync() 的实现可能有所不同,一些系统会立即将数据写入磁盘,而另一些系统则会尽快尝试这样做。

Redis 支持三种不同的 fsync 模式:

  • no:不进行 fsync 调用,让操作系统自行决定何时将数据刷新到磁盘。这种方式速度最快,但可能存在数据丢失的风险。
  • always:每次写入 AOF 日志后都进行 fsync 调用。这是最慢但最安全的方式,因为每次写入都会立即被刷新到磁盘。
  • everysec:每秒只进行一次 fsync 调用。这是速度和数据安全性之间的一个折中方案。

默认值是 everysec,因为它通常是在速度和数据安全性之间最好的折中方案。你可以根据自己的需求调整这个设置。如果你能接受一些数据丢失的风险并且追求更好的性能,可以考虑使用 no 模式(但请注意,如果你能接受数据丢失,那么默认的基于快照的持久化模式可能更合适)。相反,如果你追求更高的数据安全性,即使这意味着性能会降低,可以使用 always 模式。

2.5 no-appendfsync-on-rewrite

no-appendfsync-on-rewrite 用于配置在BGSAVE或BGREWRITEAOF正在进行时,是否阻止主进程调用fsync()

no-appendfsync-on-rewrite no

AOFfsync策略设置为alwayseverysec时,如果有一个后台保存进程(后台保存或AOF日志后台重写)正在对磁盘进行大量I/O操作,在某些Linux配置下,Redis可能会在fsync()调用上阻塞过长时间。目前这个问题没有修复方法,因为即使在不同的线程中执行fsync也会阻塞我们的同步write调用。

默认为no,不阻止主进程fsync(),还是会把数据往磁盘里刷,但是遇到重写操作,可能会发生阻塞,数据安全,但是性能降低。

当设置为yes时,在BGSAVEBGREWRITEAOF正在进行时,主进程将不会执行fsync调用。这意味着在这段时间内,Redis的持久性与appendfsync no设置时相同,即数据不会立即同步到磁盘。在最坏的情况下,你可能会丢失最多30秒(使用默认Linux设置)的AOF日志数据。因此,如果你对延迟很敏感,可以将其设置为yes。但是,从持久性的角度来看,将其保持为no(默认值)是最安全的选择。

2.6 auto-aof-rewrite-percentage、auto-aof-rewrite-min-size

AOF采用文件追加方式,文件会越来越大为避免出现此种情况,新增了重写机制,当AOF文件的大小超过所设定的阈值时,Redisfork出一条新进程来将文件重写,也是先写临时文件最后再重命名。

重写过程会创建一个新的 AOF 文件,其中只包含能够恢复当前数据状态所必需的最少命令,从而减小 AOF 文件的大小。

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

auto-aof-rewrite-percentage 用于设置一个百分比阈值,当 AOF 文件的大小相对于上一次重写后的大小增长了超过这个百分比时,Redis 就会自动触发 BGREWRITEAOF 命令来重写 AOF 文件。设置为 100(即 100%),那么当 AOF 文件的大小增长了一倍时触发重写。

auto-aof-rewrite-min-size 用于设置一个 AOF 文件重写所需的最小大小(以字节为单位)。只有当 AOF 文件的大小既超过了指定的百分比( auto-aof-rewrite-percentage ),又超过了 auto-aof-rewrite-min-size 指定的最小大小时,Redis 才会触发 AOF 文件的重写。

2.7 aof-load-truncated

aof-load-truncated 用于配置如果 AOF 文件末尾被截断时的相关处理策略。

aof-load-truncated yes

Redis 启动过程中,当 AOF 数据被加载回内存时,可能会发现 AOF 文件末尾被截断。例如,在运行 Redis 的操作系统崩溃时,尤其是当 ext4 文件系统被挂载但没有使用 data=ordered 选项。然而,当 Redis 本身崩溃或中止但操作系统仍然正常工作时,这种情况不会发生。

当发生这种情况时,Redis 可以选择报错退出,或者加载尽可能多的数据(现在是默认行为)并启动,如果 AOF 文件末尾被截断。以下选项控制这种行为。

  • aof-load-truncated yes:会加载一个被截断的 AOF 文件,并且 Redis 服务器会开始记录日志以通知用户该事件。
  • aof-load-truncated no:服务器会报错并拒绝启动。当选项设置为 no 时,用户需要使用 “redis-check-aof” 工具修复 AOF 文件后再重新启动服务器。

2.8 aof-use-rdb-preamble

aof-use-rdb-preamble 用于配置是否使用 RDB 格式创建AOF的基础文件。使用 RDB 格式总是更快、更高效。

aof-use-rdb-preamble yes

2.9 aof-timestamp-enabled

aof-timestamp-enabled 用于配置 Redis 是否支持在 AOF 中记录时间戳,以支持从特定时间点恢复数据。但是,使用这种功能会以可能与现有 AOF 解析器不兼容的方式更改 AOF 格式。

aof-timestamp-enabled no

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1888491.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

企业如何在当前形势下实现精益生产?

近年来,企业面临诸多挑战,从成本控制到效率提升,每一项决策都关乎着企业的生死存亡。尤其是在全球经济一体化和市场竞争日益激烈的大背景下,企业如何实现精益生产,成为了摆在众多企业家面前的一道难题。今天&#xff0…

STM32 看门狗 HAL

由时钟图可以看出看门狗采用的是内部低速时钟,频率为40KHz 打开看门狗,采用32分频,计数1250。 结合设置的分频系数和重载计数值,我们可以计算出看门狗的定时时间: 32*1250/40kHz 1s 主函数中喂狗就行 HAL_IWDG_Ref…

ISP IC/FPGA设计-第一部分-SC130GS摄像头分析(0)

1.介绍 SC130GS是一款国产的Global shutter CMOS图像传感器,最高支持1280Hx1024V240fps的传输速率;SC130GS有黑白和彩色款,作为ISP开发选择彩色的,有效像素窗口为1288Hx1032V,支持复杂的片上操作,选择他理…

2023-2024华为ICT大赛中国区 实践赛网络赛道 全国总决赛 理论部分真题

Part1 数通模块(10题): 1、如图所示,某园区部署了IPv6进行业务测试,该网络中有4台路由器,运行OSPFv3实现网络的互联互通,以下关于该OSPFv3网络产生的LSA的描述,错误的是哪一项?(单选题) A.R1的LSDB中将存在…

若依 ruoyi vue上传控件 el-upload上传文件 判断是否有文件 判断文件大小

console.info(this.$refs.upload.uploadFiles.length)//this.$refs.upload.uploadFiles.length 获取当前上传控件中已选择的文件大小//判断是否存在已上传文件 if(this.$refs.upload.uploadFiles.length 0){this.$modal.msgWarning("请上传文件");return; }

高质量免费数字人(整合包),是的,我又出手了~

在数字时代,我们不断追求更加逼真和高效的技术应用,特别是在数字人领域。腾讯推出了一款名为MuseTalk的革命性数字人产品,支持实时音频驱动的唇部同步数字人,迅速成为行业的新宠。(先来一张美图镇楼) Muse…

【SPIE独立出版】第四届智能交通系统与智慧城市国际学术会议(ITSSC 2024)

第四届智能交通系统与智慧城市国际学术会议(ITSSC 2024)将于2024年8月23-25日在中国西安举行。本次会议主要围绕智能交通、交通新能源、无人驾驶、智慧城市、智能家居、智能生活等研究领域展开讨论, 旨在为该研究领域的专家学者们提供一个分享…

鼠标点击器免费版?详细介绍鼠标连点器的如何使用

随着科技的发展,鼠标连点器逐渐成为了我们生活和工作中不可或缺的工具。它不仅能够帮助我们完成频繁且重复的点击任务,还能在很大程度上减少我们的手部疲劳,提高工作效率。本文将详细介绍鼠标连点器的使用方法,并推荐三款好用的免…

<软考>软件设计师-7数据库技术基础(总结)

(一) 数据库基本概念 1 数据库系统 数据:是数据库中存储的基本对象,是描述事物的符号记录。 数据的种类:文本、图形、图像、音频、视频、学生的档案记录、货物的运输情况等。 数据库DB:是长期存储在计算机内、有组织的、可共享的大量数据的集合。 数据库的基本特征: -1. 数据…

王者荣耀对折叠机进行专门适配优化,折叠机用户喜提加强

王者荣耀针对折叠屏手机做了针对性的适配优化,把折叠机的大屏幕优势发挥了出来——适配之后,用折叠屏玩王者荣耀拥有了更大的视野、以及更大的触控响应区域。 【更贴合折叠屏的视野适配】 此前王者荣耀并没有对折叠屏进行专门的适配,表现到…

基于YOLOv10深度学习的CT扫描图像肾结石智能检测系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标检测

《博主简介》 小伙伴们好,我是阿旭。专注于人工智能、AIGC、python、计算机视觉相关分享研究。 ✌更多学习资源,可关注公-仲-hao:【阿旭算法与机器学习】,共同学习交流~ 👍感谢小伙伴们点赞、关注! 《------往期经典推…

家用洗地机什么牌子好?四款公认品牌好的机型推荐

每个人都希望自己的家里面能够干干净净,就算不是一尘不染,也至少应该是整洁的,但是在这个快节奏的大环境下,做清洁对于人们来说,不是没时间,就是太累了。正当此时,一款造福懒人的神器——家用洗…

HarmonyOS APP应用开发项目- MCA助手(Day01持续更新中~)

简言: gitee地址:https://gitee.com/whltaoin_admin/money-controller-app.git端云一体化开发在线文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/agc-harmonyos-clouddev-view-0000001700053733-V5 注&#xff1…

gif压缩工具免费在线哪个好?推荐4个实用压缩好物

压缩GIF动图相信不少朋友都会,但要想在压缩GIF的同时依旧最大限度地保持画质的清晰度,那可没那么简单! 不过想要实现高清压缩,也还是有许多妙招可用的!今天就给大家整理了4个我自用的压缩方法,保准能让gif…

WSL2安装ContOS7并更新gcc

目录 WSL2安装CentOS7下载安装包安装启动CentOS7 CentOS7更换国内源gcc从源码安装gcc卸载gcc CMake中使用gcc关于linux配置文件参考 WSL2安装CentOS7 Windows11官方WSL2已经支持Ubuntu、Open SUSE、Debian。但是没有centos,所以centos的安装方式略有不同。 下载安…

Pandas_DataFrame读写详解:案例解析(第24天)

系列文章目录 一、 读写文件数据 二、df查询数据操作 三、df增加列操作 四、df删除行列操作 五、df数据去重操作 六、df数据修改操作 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 系列文章目录前言一、 读写文…

强化学习-5 策略梯度、Actor-Critic 算法

文章目录 1 基于价值( value-based \text{value-based} value-based )算法的缺点2 策略梯度算法2.1 解释2.1.1 分母和分子相消2.1.2 对数函数的导数2.1.3 组合公式2.1.4 总结 3 REINFORCE算法4 策略梯度推导进阶4.1 平稳分布4.2 基于平稳分布的策略梯度…

LeetCode题练习与总结:二叉树的后序遍历--145

一、题目描述 给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。 示例 1: 输入:root [1,null,2,3] 输出:[3,2,1]示例 2: 输入:root [] 输出:[]示例 3: 输入&#xff1a…

关于批量采集1688商品主图及链接的方式:软件采集/1688官方API接口数据采集

关于批量采集,我们通常用到的是软件 采集,或者通过1688官方API数据采集的形式:用户输入一组1688商品ID,一行一个,流程会自动逐个打开对应的1688商品详情页,采集主图的所有链接。 结果保存为表格的一行&…

人工智能-NLP简单知识汇总01

人工智能-NLP简单知识汇总01 1.1自然语言处理的基本概念 自然语言处理难点: 语音歧义句子切分歧义词义歧义结构歧义代指歧义省略歧义语用歧义 总而言之:!!语言无处不歧义 1.2自然语言处理的基本范式 1.2.1基于规则的方法 通…