当前位置: 首页 > news >正文

redis持久化-RDB

redis持久化-RDB

文档

  1. redis单机安装
  2. redis常用的五种数据类型
  3. redis数据类型-位图bitmap
  4. redis数据类型-基数统计HyperLogLog
  5. redis数据类型-地理空间GEO
  6. redis数据类型-流Stream
  7. redis数据类型-位域bitfield

官方文档

  1. 官网操作命令指南页面:https://redis.io/docs/latest/commands/?name=get&group=string
  2. Redis persistence
  3. SAVE
  4. BGSAVE

RDB

说明
  1. redis版本:7.0.0
  2. RDB:redis database,redis数据库
  3. 根据配置的规则,每隔一定的时间,将内存中的数据集快照,也就是snapshot内存快照,写入磁盘,恢复时,再将硬盘中的快照文件直接读回到内存中
  4. 快照文件被称为RDB文件,全称dump.rdb
持久化配置
redis-6.0.19的相关配置
  1. 解压目录下redis.conf文件中,相关的配置

    ################################ SNAPSHOTTING  ################################
    #
    # Save the DB on disk:
    #
    #   save <seconds> <changes>
    #
    #   Will save the DB if both the given number of seconds and the given
    #   number of write operations against the DB occurred.
    #
    #   In the example below the behavior will be to save:
    #   after 900 sec (15 min) if at least 1 key changed
    #   after 300 sec (5 min) if at least 10 keys changed
    #   after 60 sec if at least 10000 keys changed
    #
    #   Note: you can disable saving completely by commenting out all "save" lines.
    #
    #   It is also possible to remove all the previously configured save
    #   points by adding a save directive with a single empty string argument
    #   like in the following example:
    #
    #   save ""save 900 1
    save 300 10
    save 60 10000
    
  2. 配置表示,满足以下条件,将触发RDB持久化

    • 上次保存后,如果有1个键发生变化,900秒后保存

    • 上次保存后,如果有10个键发生变化,300秒后保存

    • 上次保存后,如果有10000个键发生变化,60秒后保存

redis-7.0.0的相关配置
  1. 解压目录下redis.conf文件中,相关的配置

    ################################ 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
    
Snapshotting官方文档说明

Snapshotting

By default Redis saves snapshots of the dataset on disk, in a binary file called dump.rdb. You can configure Redis to have it save the dataset every N seconds if there are at least M changes in the dataset, or you can manually call the SAVE or BGSAVE commands.

For example, this configuration will make Redis automatically dump the dataset to disk every 60 seconds if at least 1000 keys changed:

save 60 1000

This strategy is known as snapshotting.

  • 保存快照,可自动触发,或手动调用命令
自动触发保存快照
  1. 找到启动服务使用的配置文件,通常将安装目录下的redis.conf复制到指定路径做为启动文件。修改配置文件,设置保存快照触发条件

    # save 3600 1 300 100 60 10000
    save 300 5 60 10 
    
    • 上次保存后,如果有5次变更,300秒后保存
    • 上次保存后,如果有10次变更,60秒后保存
  2. 修改后重启redis

修改dump文件的保存路径
  1. 找到启动服务使用的配置文件,通常将安装目录下的redis.conf复制到指定路径做为启动文件。修改配置文件,设置自定义保存路径,需提前创建好指定路径,默认路径:./

    # 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 ./
    dir /opt/module/redis/myredis/dumpfiles
    
  2. 修改后重启redis

修改dump文件名称
  1. 找到启动服务使用的配置文件,通常将安装目录下的redis.conf复制到指定路径做为启动文件。修改配置文件,设置自定义文件名称,默认名称:dump.rdb

    # The filename where to dump the DB
    # dbfilename dump.rdb
    dbfilename dump6379.rdb
    
    • 可以标记服务器IP、服务端口号等
  2. 修改后重启redis

获取配置信息命令
  1. config get requirepass:获取密码
  2. config get port:获取端口号
  3. config get dir:获取RDB文件保存路径
恢复快照记录

每次重启服务都是一次恢复的过程,下面模拟恢复指定快照文件dump6379.rdb

  1. 启动redis,在redis中添加几条数据
  2. 查询当前存在的keykeys *
  3. 关闭redis,shutdown
  4. dump6379.rdb文件重命名为dump6379.rdb.bak,模拟已将快照文件保存到其它位置,此时该路径下无dump6379.rdb文件
  5. 启动redis,此时查询当前存在的key,无数据。因为没有快照文件,所以数据不存在
  6. 关闭redis
  7. 此时会生成新的dump6379.rdb文件,删除此文件。将dump6379.rdb.bak文件重命名为dump6379.rdb,模拟将备份的快照文件放回指定的目录下
  8. 启动redis,此时查询当前存在的key,有值,说明数据已经恢复
手动触发保存快照
官方说明

The SAVE commands performs a synchronous save of the dataset producing a point in time snapshot of all the data inside the Redis instance, in the form of an RDB file.

You almost never want to call SAVE in production environments where it will block all the other clients. Instead usually BGSAVE is used. However in case of issues preventing Redis to create the background saving child (for instance errors in the fork(2) system call), the SAVE command can be a good last resort to perform the dump of the latest dataset.

相关命令
  1. save:save命令会阻塞其它客户端,生产环境一般不使用
  2. bgsave:Save the DB in background。子进程进行保存快照的操作,主进程继续处理客户端的请求
  3. lastsave:查看最后保存快照的时间,返回时间戳,linux系统可以使用date命令对时间戳进行格式化:date -d @1746104405
RDB的优点
官方说明

RDB advantages

  • RDB is a very compact single-file point-in-time representation of your Redis data. RDB files are perfect for backups. For instance you may want to archive your RDB files every hour for the latest 24 hours, and to save an RDB snapshot every day for 30 days. This allows you to easily restore different versions of the data set in case of disasters.
  • RDB is very good for disaster recovery, being a single compact file that can be transferred to far data centers, or onto Amazon S3 (possibly encrypted).
  • RDB maximizes Redis performances since the only work the Redis parent process needs to do in order to persist is forking a child that will do all the rest. The parent process will never perform disk I/O or alike.
  • RDB allows faster restarts with big datasets compared to AOF.
  • On replicas, RDB supports partial resynchronizations after restarts and failovers.
总结
  1. 适合大规模的数据恢复
  2. 按照业务定时备份。定时保存RDB文件,一般应保存到其它服务器上,可形成不同的数据版本,需要恢复时,可以按需选择对应的版本
  3. 对数据完整性和一致性要求不高
  4. RDB文件在内存中的加载速度要比AOF快得多
RDB的缺点
官方说明

RDB disadvantages

  • RDB is NOT good if you need to minimize the chance of data loss in case Redis stops working (for example after a power outage). You can configure different save points where an RDB is produced (for instance after at least five minutes and 100 writes against the data set, you can have multiple save points). However you’ll usually create an RDB snapshot every five minutes or more, so in case of Redis stopping working without a correct shutdown for any reason you should be prepared to lose the latest minutes of data.
  • RDB needs to fork() often in order to persist on disk using a child process. fork() can be time consuming if the dataset is big, and may result in Redis stopping serving clients for some milliseconds or even for one second if the dataset is very big and the CPU performance is not great. AOF also needs to fork() but less frequently and you can tune how often you want to rewrite your logs without any trade-off on durability.
总结
  1. 如果redis不是正确的关闭,则未达到保存点的数据可能会丢失
  2. RDB需要经常fork()子进程用来保存快照,如果数据集很大fork()可能会很耗时,并且如果数据集非常大并且CPU性能不好,则可能导致Redis停止为客户端服务几毫秒甚至一秒钟
检查并修复RDB文件
  1. 进入redis安装目录下的bin目录/opt/module/redis/bin

    cd /opt/module/redis/bin
    
  2. 操作redis-check-rdb,检查并修复RDB文件

    ./redis-check-rdb /opt/module/redis/myredis/dumpfiles/dump6379.rdb
    
触发保存快照的场景
  1. 达到配置文件中设置的保存快照触发条件
  2. 手动执行savebgsave命令
  3. 执行flushall命令
  4. 正确执行shutdown命令,且没有设置开启AOF持久化
  5. 主从复制时,主节点自动触发
禁用快照
  • 客户端执行命令禁用快照

    config set save ""
    
  • 修改redis.conf配置文件禁用快照

    # Snapshotting can be completely disabled with a single empty string argument
    # as in following example:
    #
    # save ""
    save ""# 注释掉其它的save配置
    # save 3600 1 300 100 60 10000
    
其它RDB配置

找到启动服务使用的配置文件,通常将安装目录下的redis.conf复制到指定路径做为启动文件,修改配置文件

  1. stop-writes-on-bgsave-error,默认yes,表示,如果后台保存执行失败,将停止接受写入操作

    stop-writes-on-bgsave-error yes
    
  2. rdbcompression,默认yes,表示,使用LZF算法进行压缩存储

    rdbcompression yes
    
  3. rdbchecksum,默认yes,表示,使用CRC64算法进行数据校验

    rdbchecksum yes
    
  4. rdb-del-sync-files,默认noyes表示,在未启用持久化的情况下删除复制使用的RDB文件

    rdb-del-sync-files no
    

相关文章:

  • 提示词版本化管理:AI开发中被忽视的关键环节
  • 数字智慧方案6197丨智慧用电一体化服务运营解决方案(34页PPT)(文末有下载方式)
  • Linux 常用命令合集
  • 我的日记杂文
  • 截图软件、画图软件、左右分屏插件、快捷键
  • 【大模型面试每日一题】Day 6:分布式训练中 loss 出现 NaN,可能原因及排查方法?
  • 实战交易策略 篇二十二:情绪流龙头交易策略
  • 学习笔记:Qlib 量化投资平台框架 — OTHER COMPONENTS/FEATURES/TOPICS
  • 仿腾讯会议——主界面设计创建房间加入房间客户端实现
  • Linux管道识
  • Qt 中基于 QTableView + QSqlTableModel 的分页搜索与数据管理实现
  • 双向链表详解
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(14):かもしれません (~た・~ない)ほうがいいです
  • 兰亭妙微分享:B 端设计如何实现体验跃迁
  • 依赖倒置原则(DIP)
  • DeepSeek-R1模型蒸馏
  • Demo02_基于寄存器+标准库开发的项目
  • vulkanscenegraph显示倾斜模型(6.2)-记录与提交
  • LLMs Tokenizer Byte-Pair Encoding(BPE)
  • 上位机知识篇---粗细颗粒度
  • 印尼巴厘岛多地停电,疑似海底电缆发生故障
  • 华尔兹转岗与鲁比奥集权:特朗普政府人事震荡背后的深层危机
  • AI世界的年轻人|他用影像大模型解决看病难题,“要做的研究还有很多”
  • 韩国代总统、国务总理韩德洙宣布辞职
  • 马上评|启动最高层级医政调查,维护医学一方净土
  • 中国空间站多项太空实验已取得成果,未来将陆续开展千余项研究