【redis】通过配置文件简述redis的rdb和aof

news2024/9/24 11:21:16

redis的持久化方式有2种,rdb,即通过快照的方式将全量数据以二进制记录在磁盘中,aof,仅追加文件,将增量的写命令追加在aof文件中。在恢复的时候,rdb要更快,但是会丢失一部分数据。aof丢失数据极少,但是恢复数据很慢。redis默认使用rdb进行持久化。

下面结合配置文件对2种方式进行讲解。

RDB

redis默认的持久化方式就是rdb。可以手动通过命令触发,如save,前台阻塞去rdb持久化;bgsave,后台rdb持久化(其实就是另起一个线程去做持久化)

################################ SNAPSHOTTING  ################################
# Save the DB to disk.
#
# save <seconds> <changes> [<seconds> <changes> ...]
#
# Redis will save the DB if the given number of seconds elapsed and it
# surpassed the given number of write operations against the DB.
#
# Snapshotting can be completely disabled with a single empty string argument
# as in following example:
#
# save ""
#
# Unless specified otherwise, by default Redis will save the DB:
#   * After 3600 seconds (an hour) if at least 1 change was performed
#   * After 300 seconds (5 minutes) if at least 100 changes were performed
#   * After 60 seconds if at least 10000 changes were performed
#
# You can set these explicitly by uncommenting the following line.
#
# save 3600 1 300 100 60 10000


如果不想开启rdb,可以使用save ""来关闭rdb。
默认的,rdb会开启# save 3600 1 300 100 60 10000 的频率,意思为,3600秒(1小时)有1次写命令,进行rdb备份;或者300秒(5分钟)有100次写命令,进行rdb备份;或者60秒(1分钟)有10000次写命令,进行rdb备份。判断顺次进行,满足任意一个,都会开启rdb。
如果想修改,按照这个格式自己修改就可以。

# The filename where to dump the DB
dbfilename dump.rdb

其实就是命名一下rdb备份文件,默认命名为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 /var/lib/redis/6379

指定备份文件的路径,aof的文件也会放在这个路径下。

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

翻译如下:

默认情况下,Redis将数据集异步转储到磁盘上。这种模式在许多应用程序中已经足够好了,但是Redis进程的问题或停电可能会导致几分钟的写丢失(取决于配置的保存点)。

#只追加文件是另一种持久性模式,提供了更好的持久性。例如,使用默认的数据同步策略(见后面的配置文件),Redis可以在一个戏剧性的事件中只丢失一秒钟的写,比如服务器停电,或者如果Redis进程本身发生了错误,只丢失一次写,但操作系统仍然正常运行。

可以同时启用AOF和RDB持久性而不会出现问题。如果在启动时启用AOF, Redis将加载AOF,这是具有更好的持久性保证的文件。

由rdb的备份机制可知,默认的备份机制为,1小时1次写命令 | 5分钟100次写命令 | 1分钟10000次写命令。这样的触发机制,不管写命令是多是少,如果遇到停电等问题,丢失的数据相对来说都会比较多。如果你不想丢失这么多的数据,就需要开启aof。如果rdb和aof同时开启,aof和rdb都会备份,但是恢复时会使用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

这部分配置的是aof触发追加的时机,或者说策略。在讲策略之前,需要先了解fsync和内核。
如果没有调用fsync,内核会等待更多的数据,直到在buffer缓冲区满了之后向磁盘进行IO。
fsync是一个底层的函数,redis通过调用这个fsync函数,可以使内核不必等待buffer满而直接去向disk磁盘进行IO。
而aof的追加策略有3种,
always是说每次写命令,都直接调fsync,向磁盘中的文件里追加。
no不是不追加,而是不调用fsync,会在内核满了以后由内核向磁盘文件中IO追加。
everysec比较折中,每一秒会固定调用一次fsync,此外如果一秒中内核的buffer满了多次,内核也会多次向磁盘进行IO。


# 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

在我们执行rdb的bgsave或者aof的bgrewriteaof时,是另外起一个线程进行IO的。而代码片段里讲,即使是这样,也会阻塞接受命令的主线程。即使现在的版本已经到7了还没有解决这个问题,所以暂时的办法就是你可以选择在后台备份rdb文件或者重写aof文件时,暂停备份。在最坏的情况下,最长有可能丢失30s的备份。

# 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

在介绍这2个参数之前,我们需要先介绍一下aof文件的重写。aof文件在过大时,会进行重写。在后台重写的命令为bgrewriteaof(另外开一个线程,但是会阻塞主线程对aof文件的fsync),前台重写
在4版本之前,仅会将抵消掉的命令在重写后清除。
在4以后到7之前,重写时会将全量数据转为rdb的格式(但要注意存储在aof文件中),有新的写命令追加进来后,仍然以aof格式存储在同一个aof文件中。仍然为aof格式,一同存储在aof文件中。

而7开始,aof文件一共分为3个,appendonly.aof.3.base.rdb appendonly.aof.3.incr.aof appendonly.aof.manifest
appendonly.aof.1.incr.aof文件,存储的是重写后的增量部分的写命令,为aof格式。
appendonly.aof.1.base.rdb文件,存储的是重写时的全量数据,转为rdb格式存在这个文件中。
上述2个文件,每次重写,数字部分的版本号都会+1.
appendonly.aof.manifest文件存储了关于另外2个文件的信息。
这样,aof在恢复时,先通过vase.rdb快速恢复大部分数据,再通过aof文件恢复重写后的增量数据,兼顾了快和全。

aof文件的重写,可以通过bgrewriteaof命令手动触发重写,也可以配置策略,当aof文件超过多大时自动重写。

好,知道了aof的重写以后,可以去介绍这2个参数了。redis每次重写以后,都会记录重写后的aof文件(在7版本aof文件重写默认为rdb格式)的大小,配置文件的注释中称其为base size,而新的aof文件的大小称其为current size。

当current size超过了base size指定百分比时,就会触发重写。如auto-aof-rewrite-percentage为100%时,一个200M的aof文件重写后,会和之前存量的rdb文件里的数据合并为一个rdb文件,假设合并后的新的rdb文件为500M,那么base size为500M,新的aof文件如果超过了500M*100%,那么就会触发重写。


# 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

上面说过,7版本的aof文件重写后默认为rdb文件,在这里可以选择no,重写后为aof文件。默认为yes rdb文件。

上面,通过结合配置文件,讲了一下rdb和aof,下面简单的看一下2个文件实际长什么样吧。

在这里插入图片描述

rdb
rdb的文件默认叫dump.rdb,和aof文件所在目录appendonlydir同级。dump.rdb中可见

REDIS0010ú      redis-ver^F7.0.12ú
redis-bitsÀ@ú^Ectime­ Ádú^Hused-memÂ^X^[^M^@ú^Haof-baseÀ^@ÿOx!Íܧ¤µ
~                                                                                                                              
~                                                                                                                              
~                                                                                                                              
~        

aof

base文件里面存储的重写时的全量数据,默认以rdb格式存储,更快。也可以指定为aof格式存储。
appendonly.aof.3.base.rdb

REDIS0010ú      redis-ver^F7.0.12ú
redis-bitsÀ@ú^EctimeÂø<94>¿dú^Hused-memÂ0W^O^@ú^Haof-baseÀ^Aÿo{Ag+/ÿ7
~                                                                                                                              
~                                                                                                                              
~      

重写后的增量数据,在incr.aof文件中存储。aof如何存储命令的呢,以下面作为示例。
刚进入redis-cli时,默认为0号库,也就是select 0. 2表示这个命令由2个部分(字符串)组成,$6表示下面的字符串有6个字符,即SELECT;$1表示下面的字符串有1个字符,即0.
再遇到下面的
3,表示一个命令开始,由3个部分组成。同理,可知命令为set k1 v1。
appendonly.aof.3.incr.aof

*2
$6
SELECT
$1
0
*3
$3
set
$2
k1
$2
v1
*3
$3
set
$2
k1
$2
v2
*3
$3
set
$2
k2
$2
v2
*3

appendonly.aof.manifest

file appendonly.aof.3.base.rdb seq 3 type b
file appendonly.aof.3.incr.aof seq 3 type i
~                                                                                                                              
~                                                                                                                              
~                                                                                                                              
~                                                                                                                              
~                                                                                                                              
~                                                                                                                              
~                                                                                                                              
~        

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

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

相关文章

HTML快速学习

目录 一、网页元素属性 1.全局属性 2.标签 2.1其他标签 2.2表单标签 2.3图像标签 2.4列表标签 2.5表格标签 2.6文本标签 二、编码 1.字符的数字表示法 2.字符的实体表示法 三、实践一下 一、网页元素属性 1.全局属性 id属性是元素在网页内的唯一标识符。 class…

207. 课程表 Python

文章目录 一、题目描述示例 1示例 2 二、代码三、解题思路 一、题目描述 你这个学期必须选修 numCourses 门课程&#xff0c;记为 0 到 numCourses - 1 。 在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出&#xff0c;其中 prerequisites[i] [ai, …

入门Linux基本指令(2)

这篇文章主要提供一些对文件操作的Linux基本指令&#xff0c;希望对大家有所帮助&#xff0c;三连支持&#xff01; 目录 cp指令(复制) mv指令(剪切) nano指令 cat指令(打印文件内容) > 输出重定向 >> 追加重定向 < 输入重定向 more指令 less指令(推荐) …

影刀下载,插件安装

1、下载 在影刀官网下载&#xff1a;www.yingdao.com 2、谷歌插件安装 参考&#xff1a; 影刀插件安装各种方式 浏览器安装插件说明 - 影刀帮助中心 安装说明&#xff1a;驱动外置 Chrome 需要安装插件&#xff0c;并且保证此插件处于开启状态 方式一&#xff1a;用户头…

利用sklearn 实现线性回归、非线性回归

代码&#xff1a; import pandas as pd import numpy as np import matplotlib import random from matplotlib import pyplot as plt from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression# 创建虚拟数据 x np.array(r…

nacos安装与基础配置

源码 https://github.com/alibaba/nacos https://gitee.com/mirrors/Nacos 编译 git clone https://github.com/alibaba/nacos.git cd nacos/ mvn -Prelease-nacos -Dmaven.test.skiptrue clean install -U ls -al distribution/target/// change the $version to your ac…

24考研数据结构-队列1

目录 3.2队列&#xff08;Queue&#xff09;3.2.1队列的基本概念3.2.2队列的顺序存储结构3.2.2.1 队列存储的基本操作3.2.2.2 循环队列 基本操作和判空方式 \color{Red}{基本操作和判空方式} 基本操作和判空方式3.2.2.3 知识回顾 3.2队列&#xff08;Queue&#xff09; 3.2.1队…

解读RSAC 2021丨灵魂拷问:你的网络够“皮实”吗?

美国时间5月20日&#xff0c;RSA大会落下帷幕。大会虽已结束&#xff0c;讨论还在继续。对于大会的主题“Resilience”&#xff0c;每个厂商、每个人都有自己的解读。 山石网科新技术研究院全程关注RSA大会&#xff0c;对于“Resilience”&#xff0c;他们的解读简单易懂接地气…

Java面向对象 - 常用类——Object类

什么是Object类 Java中有一个比较特殊的类&#xff0c;就是 Object类&#xff0c;它是所有类的父类&#xff0c;如果一个类没有使用extends关键字明确标识继承另外一个类&#xff0c;那么这个类就默认继承 Object类。因此&#xff0c;Object 类是 Java 类层中的最高层类&#x…

【C语言进阶篇】指针都学完了吧!那回调函数的应用我不允许还有人不会!

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏:《C语言初阶篇》 《C语言进阶篇》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 文章目录 &#x1f4cb; 前言&#x1f4ac; 函数指针数组&#x1f4ad; 函数指针数组的定义&#x1f4ad; 函数指针数组的…

Android性能优化之游戏引擎初始化ANR

近期&#xff0c;着手对bugly上的anr 处理&#xff0c;记录下优化的方向。 借用网上的一张图&#xff1a; 这里的anr 问题是属于主线程的call 耗时操作。需要使用trace 来获取发生anr前一些列的耗时方法调用时间&#xff0c;再次梳理业务&#xff0c;才可能解决。 问题1 ja…

Java Servlet实现下拉选择查询(双表)和单列模式

0目录 1.Servlet实现下拉选择查询&#xff08;双表&#xff09; 2.单列模式 1.Servlet实现下拉选择查询&#xff08;双表&#xff09; 新建数据库和表 实体类 接口方法 实现接方法 Servlet类 Web.xml List.jsp 页面效果 加入功能 2.单列模…

批发零售进销存哪个好?盘点5款主流批发零售进销存软件!

在我看来&#xff0c;几乎没有批发零售行业不需要做进销存管理&#xff0c;哪怕是路边一个小摊贩&#xff0c;也需要做进销存管理&#xff0c;但是传统的进销存过程中存在很多问题&#xff1a; 前后方协作困难&#xff1a;采购/销售/财务工作相互独立&#xff0c;工作入口不一…

机器学习深度学习——多层感知机

&#x1f468;‍&#x1f393;作者简介&#xff1a;一位即将上大四&#xff0c;正专攻机器学习的保研er &#x1f30c;上期文章&#xff1a;机器学习&&深度学习——感知机 &#x1f4da;订阅专栏&#xff1a;机器学习&&深度学习 希望文章对你们有所帮助 上一节…

Java阶段五Day14

Java阶段五Day14 文章目录 Java阶段五Day14分布式事务整合demo案例中架构&#xff0c;代码关系发送半消息本地事务完成检查补偿购物车消费 鲁班周边环境调整前端启动介绍启动前端 直接启动的项目gateway&#xff08;网关&#xff09;login&#xff08;登录注册&#xff09;atta…

DSA之图(4):图的应用

文章目录 0 图的应用1 生成树1.1 无向图的生成树1.2 最小生成树1.2.1 构造最小生成树1.2.2 Prim算法构造最小生成树1.2.3 Kruskal算法构造最小生成树1.2.4 两种算法的比较 1.3 最短路径1.3.1 两点间最短路径1.3.2 某源点到其他各点最短路径1.3.3 Dijkstra1.3.4 Floyd 1.4 拓扑排…

数据库表结构导出成文档

1.需求说明 在系统交付的过程中&#xff0c;有时候需要交付数据库的表结构&#xff0c;如果系统做的比较大&#xff0c;比如几百张表的时候&#xff0c;靠人力一张表一张的写&#xff0c;那就是一个奔溃啊。所以今天特意找了一下工具&#xff0c;小巧安装。比较好用。 2.安装…

新型恶意软件DecoyDog正大规模入侵DNS

安全厂商 Infoblox 的调查研究显示&#xff0c;一个名为 DecoyDog&#xff08;诱饵狗&#xff09;的复杂恶意工具包通过域名系统&#xff08;DNS&#xff09;&#xff0c;从事网络间谍活动已达1年以上。 目前尚不清楚该恶意软件的幕后黑手是谁&#xff0c;但 Infoblox 的研究人…

通过REST API接口上传Nexus仓库

一、Nexus API文档 API文档链接&#xff1a;Components API 二、上传API接口说明 在Nexus中可以直接调试api接口&#xff0c;url参考&#xff1a;http://localhost:8081/#admin/system/api 三、上传请求案例 $ curl -X POST "http://localhost:8081/service/rest/v1/c…

营销系统积分数据库设计

营销系统总体数据-业务功能模型 在当今日益竞争的市场中&#xff0c;如何提高客户留存率和忠诚度&#xff0c;已成为各大企业迫切需要解决的问题。而积分商城/系统作为一种新型的营销方式&#xff0c;受到青睐。 积分商城/系统是指将用户在使用产品或服务时产生的积分&#xf…