当前位置:网站首页>Redis Persistence - RDB and AOF
Redis Persistence - RDB and AOF
2022-08-02 01:58:00 【Chon-Wang】
Redis
的数据是存储在内存当中, because the server is down、停电等情况,Redis
The data in it is easy to lose.这时Redis
The role of persistence is reflected.例: Can be used to complete data recovery、failover data, etc.
Redis
Persistence is divided into three policy mechanisms:RDB(Redis DataBase)
、AOF(Append Only File)
、Mixed persistence in two ways.This article briefly describes both methods. For in-depth knowledge points, Congratulations, everyone, please enter the advanced area to check.
点我查看 - Redis 订阅与 Redis Stream 技术
一、RDB(Redis DataBase)
RDB
是指周期性Writes an in-memory snapshot of the dataset to a disk file.In this way, the data in memory is written to the binary file in the form of a snapshot, 默认的文件名为
dump.rdb
, 在Redis
重启时, 通过加载dump.rdb
文件来恢复数据.
1.1 RDB 的触发方式
1.1.1 自动触发
顾名思义, 自动触发是
Redis
在某个时间内, Automate persistence operations, 将数据进行备份.
Redis
配置文件是/usr/local/redis/bin/redis.conf
.
这是我的
redis.conf
所在位置, Find the file according to your own situation打开后, 我的在 412 The line starts with the configuration for the snapshot
我这里默认
RDB
持久化功能是关闭的,# save ""
, 我的在 424 行
dbfilename dump.rdb
文件名配置, 我的在 481 行
dir ./
The backup file holds the directory configuration, 我的在 504 行By default it is annotated, 如果要启用, will start with the line
#
去掉
# 配置格式: save m n [m n ...]
# 解释: 在 m 秒内, 如果有 n 个键发生改变, The persistence operation is automatically triggered, Multiple conditions can be configured
# 例: save 3600 1 300 100 60 10000
# 例解: 3600 秒内, 如果有 1 个键发生改变, The persistence operation is automatically triggered
# 例解: 300 秒内, 如果有 100 个键发生改变, The persistence operation is automatically triggered
# 例解: 60 秒内, 如果有 10000 个键发生改变, The persistence operation is automatically triggered
1.1.2 手动触发
顾名思义, Manual trigger is pass 手动执行命令 来完成持久化操作.
There are two commands that are triggered manually:
save()
、bgsave()
, The main difference between them is whether or not they block the thread.
SAVE 命令:
save()
命令触发Redis
同步持久化.- 缺点: 会使
Redis
处于阻塞状态, 不能执行其他命令, 直到RDB
持久化完成.- 优点: Doesn't consume extra memory though,但生产环境慎用.
BGSAVE 命令:
bgsave()
会执行fork
操作创建子进程, The persistent work is done by the child process, 整个过程只有在fork
There is a brief block when the child process, When the child process is created, It can respond to requests from other clients.
bgsave()
命令触发Redis
异步持久化.- 缺点: 创建子进程, 需要消耗更多内存.
- 优点: 创建子进程后, Other clients can also interact, Used in most cases
bgsave()
.
1.2 RDB 的优缺点
1.2.1 RDB 优点
RDB
The persistent file is binary data, 占用内存更小.RDB
Because the backup is a snapshot of the database, So it is very suitable for cold backup, 比如 凌晨1点的时候, 进行持久化操作.- in the face of big data,
RDB
Backup to restore speed ratioAOF
快.
1.2.2 RDB 缺点
RDB
Backups are performed periodically, 如果发生意外情况, And there is data interaction during this time, Data for this period will be lost.- 无法实现 实时持久化.
- Because it is a full backup, And also often create subprocesses, 数据量大的话, Will consume the server for a long timeCPU与内存.
二、AOF(Append Only File)
AOF
Record in log, 将执行完的命令、命令参数、Information such as the number of parameters is passedwrite
The function appends to the end of the log file.
2.1 同步流程
Command synchronization process:
- Send commands or parameters, etc. to
AOF
程序AOF
After the program is received, 因为Redis
是单线程, 通过resp
协议, Staging data toAOF
缓冲区- By setting the synchronization policy, Synchronously appends commands from the staging area to the end of the file
2.2 同步策略
# Synchronization strategy by appendfsync 参数决定, 我的在 1435 行开始
# appendfsync always
appendfsync everysec # 默认
# appendfsync no
always
AOF
When the buffer receives data, 立即调用fsync
The command is forced to goAOF
文件写数据.everysec
AOF
When the buffer receives data, 调用write
命令往io
缓冲区写数据, Also called once per secondfsync
命令, 强制将io
缓冲区的数据写入AOF
文件.no
AOF
When the buffer receives data, 调用write
命令往io
缓冲区写数据, **当io
When the buffer fills up or periodically the system willio
缓冲区的数据写入AOF
文件 **.
2.3 AOF 重写
As more and more commands are executed,
AOF
文件会越来越大, 可采用 AOF 重写机制 解决这一问题.Overriding enables a background child process
bgrewriteaof
.
2.3.1 配置
AOF 重写 是去除
AOF
文件中 无效的命令、超时的数据 wait or adopt 命令合并 方式, 创建一个新的AOF
file to hold the rewritten commands.no-appendfsync-on-rewrite no # The rewrite mechanism is disabled by default, 如需开启, 请改为 yes , 我的在 1460 行
2.3.2 触发重写
手动触发
AOF
重写可以由用户通过调用BGREWRITEAOF
命令手动触发.自动触发
根据
auto-aof-rewrite-percentage
与auto-aof-rewrite-min-size 64mb
确定触发时机.# 我的在 1479 行 auto-aof-rewrite-percentage 100 # 当前 AOF 文件大小(aof_current_size) 比 The size after the previous rewrite(aof_base_size) 至少大了 100% auto-aof-rewrite-min-size 64mb # AOF 体积大于 64mb 会触发重写
2.4 开启 AOF
默认
AOF
持久化功能是关闭的.
Redis
配置文件是/usr/local/redis/bin/redis.conf
.打开后, 我的在 1359 line is the start of the configuration.appendonly yes # 开启, 改为 yes , 我的在 1379 行 appendfilename "appendonly.aof" # Snapshot base filename appenddirname "appendonlydir" # Storage dedicated directory
2.5 AOF 的优缺点
2.5.1 AOF 的优点
- Data integrity can be better protected, 比
RDB
更靠谱- Even if the file is too large, 也可以采用 重写 方式, 不影响客户端
2.5.2 AOF 的缺点
- Because of the synchronization strategy, 一般情况下
AOF
的速度慢于RDB
- Even if it's been rewritten all the time, But the file size is still better
RDB
The binary file is largeAOF
在恢复时, Each command will be repeated, 速度也比RDB
慢- Due to its large size, 恢复时, There may be write corruption, You can compare the differences between the two files, 使用
redis-check-aof --fix AOF文件名
修复,diff -u
to compare the differences in the files, And then confirm and then restore.
三、两者比较
方式 | 存储速度 | 恢复速度 | 数据完整性 | 文件大小 | 资源消耗 |
---|---|---|---|---|---|
RDB | 慢 | 快 | 不可抗拒因素, 可能会丢失数据 | 小 | 高 |
AOF | 快 | 慢 | According to the synchronization strategy,视情况而定, May be badly written | 大 | 低 |
Choose the appropriate persistence method according to the business scenario, 默认
RDB
.
- If the business scenario mainly acts as a cache function, Failed to revisit data acquisition, Less reliance on persistence, 则选择
RDB
.- 如果数据需要持久保存, 确保数据的完整性, 则建议
RDB
与AOF
同时启用.
点我查看 - Redis 订阅与 Redis Stream 技术
边栏推荐
猜你喜欢
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
喜报 | AR 开启纺织产业新模式,ALVA Systems 再获殊荣!
Constructor instance method inheritance of typescript38-class (implement)
【服务器数据恢复】服务器Raid5阵列mdisk磁盘离线的数据恢复案例
Hiring a WordPress Developer: 4 Practical Ways
Use baidu EasyDL implement factory workers smoking behavior recognition
Image fusion based on weighted 】 and pyramid image fusion with matlab code
外包干了三年,废了...
Rust P2P网络应用实战-1 P2P网络核心概念及Ping程序
Navicat数据显示不完全的解决方法
随机推荐
LeetCode刷题日记: 33、搜索旋转排序数组
Redis 持久化 - RDB 与 AOF
手写一个博客平台~第一天
Shell Beginners Final Chapter
ofstream,ifstream,fstream读写文件
Typescript31 - any type
"NetEase Internship" Weekly Diary (3)
Golang分布式应用之定时任务
Golang分布式应用之Redis
Newton's theorem and related corollaries
6-24漏洞利用-vnc密码破解
超大规模的产业实用语义分割数据集PSSL与预训练模型开源啦!
密码学的基础:X.690和对应的BER CER DER编码
力扣 1374. 生成每种字符都是奇数个的字符串
HSDC is related to Independent Spanning Tree
YGG 公会发展计划第 1 季总结
Named parameter implementation of JDBC PreparedStatement
typescript35-class的构造函数
【轮式里程计】
搜罗汇总的效应