当前位置:网站首页>Redis Persistence - RDB and AOF
Redis Persistence - RDB and AOF
2022-08-02 01:58:00 【Chon-Wang】
Redis的数据是存储在内存当中, because the server is down、停电等情况,RedisThe data in it is easy to lose.这时RedisThe role of persistence is reflected.例: Can be used to complete data recovery、failover data, etc.
RedisPersistence 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, 整个过程只有在forkThere 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 优点
RDBThe persistent file is binary data, 占用内存更小.RDBBecause the backup is a snapshot of the database, So it is very suitable for cold backup, 比如 凌晨1点的时候, 进行持久化操作.- in the face of big data,
RDBBackup to restore speed ratioAOF快.
1.2.2 RDB 缺点
RDBBackups 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)
AOFRecord in log, 将执行完的命令、命令参数、Information such as the number of parameters is passedwriteThe function appends to the end of the log file.
2.1 同步流程
Command synchronization process:
- Send commands or parameters, etc. to
AOF程序AOFAfter 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
AOFWhen the buffer receives data, 立即调用fsyncThe command is forced to goAOF文件写数据.everysec
AOFWhen the buffer receives data, 调用write命令往io缓冲区写数据, Also called once per secondfsync命令, 强制将io缓冲区的数据写入AOF文件.no
AOFWhen the buffer receives data, 调用write命令往io缓冲区写数据, **当ioWhen 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 命令合并 方式, 创建一个新的AOFfile 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
RDBThe 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 -uto 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 技术
边栏推荐
- When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
- Redis 底层的数据结构
- Moonbeam与Project Galaxy集成,为社区带来全新的用户体验
- Constructor of typescript35-class
- 软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言
- 有效进行自动化测试,这几个软件测试工具一定要收藏好!!!
- Named parameter implementation of JDBC PreparedStatement
- Pcie the inbound and outbound
- JDBC PreparedStatement 的命名参数实现
- PHP 使用 PHPRedis 与 Predis
猜你喜欢
随机推荐
YGG Guild Development Plan Season 1 Summary
LeetCode刷题日记:LCP 03.机器人大冒险
【刷题篇】打家劫舍
【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
哈希表
【服务器数据恢复】服务器Raid5阵列mdisk磁盘离线的数据恢复案例
MySQL优化策略
3 Month Tester Readme: 4 Important Skills That Impacted My Career
飞桨开源社区季度报告来啦,你想知道的都在这里
Effects of Scraping and Aggregation
记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
字节给我狠狠上了一课:危机来的时候你连准备时间都没有...
Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
手写一个博客平台~第三天
有效进行自动化测试,这几个软件测试工具一定要收藏好!!!
华为5年女测试工程师离职:多么痛的领悟...
Day115.尚医通:后台用户管理:用户锁定解锁、详情、认证列表审批
LeetCode刷题日记:74. 搜索二维矩阵
"NetEase Internship" Weekly Diary (1)
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here









