Redis 持久化

redis数据持久化 在实际使用中需要根据Redis作为主存还是缓存、数据完整性和缺失性的要求、CPU和内存情况等诸多因素来确定适合自己的持久化方案,一般来说稳妥的做法包括:RDB与AOF同时使用:最安全的做法,即使AOF损坏无法修复,还可以用RDB来恢复数据,当然在持久化时对性能也会有影响。只使用RDB:在Redis当简单缓存,没有缓存也不会造成缓存雪崩的情况下可只使用RDB即可。单独使用AOF:不推荐,因为AOF对于数据的恢复载入比RDB慢,所以使用AOF的时候,最好还是有RDB作为备份。 阅读详情

声明: 本系列博文都是总结于《Redis开发与运维》这本书中相关内容;

Redis 支持RDB 和 AOF 两种类型的持久化机制,

1. RDB:

《1》触发机制:

手动触发:

  • save 命令:阻塞当前Redis执行,直到RDB过程完成为止,对于内存比较大的实例,会造成长时间阻塞; 已废弃
  • bgsave 命令:Redis 进行执行 fork 操作创建子进程, RDB持久化过程由子进程负责完成,完成后子进程自动结束,阻塞只发生在 fork 阶段,bgsave 对save命令做了优化,

自动触发:

  • save m n :表示 m 秒内数据集 存在 n 次 修改时,自动触发 bgsave 操作;
  • save 900 1 : 表示 900 秒内 数据集存在 1 次 修改时,触发bgsave操作;

《2》文件保存位置:

  • RDB 文件 以 ${dbfilename} 保存在 ${dir} 目录下;

dbfilename 和 dir 在配置文件 redis.conf 中自行配置即可或者使用 config set dir {newDir} ;config set dbfilename {newFilename} 运行期进行动态执行;

《3》压缩算法:

RDB 文件默认采用 LZF 算法 对生成的RDB文件做压缩处理,默认开启自动压缩,config set rdbcompression {yes|no} 动态修改;

《4》RDB文件正确性检测:

  • redis-check-rdb dbfilename.rdb

《5》优缺点

优点:

  • RDB是一个紧压缩的二进制文件,代表 Redis 在某个时间点上的数据快照,
  • RDB恢复数据时速度比较快,远超AOF的方式;

缺点:

  • RDB是在某个时间点上fork一个子进程对当前redis内存中数据快照进行备份,所以 没有办法做到实时持久化或者说秒级持久化;
  • 存在老版本Redis无法兼容新版本Redis的rdb文件;
################################ 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 behaviour 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

# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
# 当bgsave出错时,是否停止writes
stop-writes-on-bgsave-error yes

# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
# rdb文件是否进行压缩
rdbcompression yes

# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
# rdb文件checksum
rdbchecksum yes

# The filename where to dump the DB
# rdb文件名字
dbfilename 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 ./

2.AOF

Append Only File 持久化:以独立日志的方式记录每次写命令,重启时,重写执行AOF文件中的命令达到回复数据的目的,主要用于解决数据持久化时的实时性,目前已经是 Redis 持久化的主流方式;

开关配置:

appendonly yes : 默认不开启;

文件名称:

appendfilename : 默认文件名:appendonly.aof

保存路径:

dir : 默认当前目录 ./

《1》建议工作流程:

  1. Redis 所有的命令会追加到 aof_buf (缓冲区)中; 之所以先追加到缓冲区是因为,Redis是单进程的,如果每次都追加到硬盘,那么性能完全取决于当前硬盘的负载。
  2. AOF 缓冲区根据对应的策略向硬盘做同步操作;
  3. 随着 AOF 文件越来越大,需要定时对 AOF 文件进行重写,到达压缩目的。
  4. 当 Redis服务器重启时,可以加载AOF文件进行数据恢复;

《2》文件同步策略

  • always : 命令写入到 aof_buf 后调用系统 fsync 操作同步到aop文件,fsync 完成后线程返回;
  • everysec : 命令写入 aof_buf后调用系统write操作,write操作完成线程返回。fsync 同步文件操作由专门的线程 每秒调用一次;
  • no : 命令写入 aof_buf后调用系统write操作,fsync 同步文件操作由系统负责;数据安全性较差;

《3》aop文件重写触发机制

手动触发:

  • 直接调用命令: bgrewriteaof

自动触发:

根据 auto-aof-rewrite-min-size 和 auto-aof-rewrite-percentage 参数来自动触发;

  • auto-aof-rewrite-min-size : 表示运行 AOF 重写时,文件最小最小体积,默认 64mb;
  • auto-aof-rewrite-percentage : 当前 AOF 文件空间(aof_current_size)和上次重写后AOF文件空间(aof_base_size)的比值;

自动触发时机:aof_current_size > auto-auto-rewrite-min-size && (aof_current_size - aof_base_size)/ aof_base_size >= auto_aof_rewrite_percentage;

############################## 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 http://redis.io/topics/persistence for more information.
# 开启aof持久化
appendonly yes

# The name of the append only file (default: "appendonly.aof")
# aof 持久化文件名称
appendfilename "appendonly.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".
#
# aof的输出缓冲区数据刷入磁盘策略;
# 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(Redis可能阻塞很长时间当调用fsync时).
# 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 none". 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.
# 如果你存在延迟问题,可以将其设置为yes, 否则让其保持为no,这样更安全
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文件保存时,突然遇到断电,最后一行没保存全时候,是继续还是退出;
aof-load-truncated yes

# When rewriting the AOF file, Redis is able to use an RDB preamble(序文) in the
# AOF file for faster rewrites and recoveries. When this option is turned
# on the rewritten AOF file is composed of two different stanzas:
#
#   [RDB file][AOF tail]
#
# When loading Redis recognizes that the AOF file starts with the "REDIS"
# string and loads the prefixed RDB file, and continues loading the AOF
# tail.
# 
# 对aof文件重写或者回复时候,可以使用rdb序文,快,默认yes就好
aof-use-rdb-preamble yes

3.AOF RDB

《1》rdb 和 aof 同时存在时,先使用 aof 因为其安全和数据完整性交高;

《2》文件校验:

对于错误的AOF文件,可以使用 redis-check-aof --fix 进行人工修复;然后使用diff -u 对比数据的差异,进行人工修复;

 

 

 

 

 

 

 

 

 

 

 

 

redis desktop manager Redis Desktop Manager Cross-platform open source Redis DB management tool 立即下载

相关推荐

redismanage可视化管理工具

1. 多样化管理服务器,支持服务器的密码认证服务. 2. 支持所有redis的数据类型. 3. 支持单个key的重命名功能. 4. 支持单个key的删除功能. 5. 支持查看key数据的大小,引用等辅助属性. 6. 列表类的数据可分页,加快查看速度,不卡死. 7. 支持生命周期的修改以及移除功能 8. 对key包增加了支持, 查看更直观,更清爽.

Redis可视化工具 Redis Desktop Manager

官网下载:https://redisdesktop.com/download github地址:https://github.com/uglide/RedisDesktopManager/releases 一、下载 Redis Desktop Manager 官网的现在下载需要收费了,我在网上找了个放到了github上 https://raw.githubusercontent.com/wangc...

嘻哈王大头 3408

SAP-HR 模块 PPOSE 的详细信息标签页增强

步骤1:T-CODE : SE11 创建一个名为 HRI9106 的结构 命名:HRI + 91 + 两位数字。人事管理 - 组织管理 - 集成新信息类型 - 将信息类型添加为明细范围中的标签页。步骤6:SE51 查看 7000屏幕,修改屏幕属性 让其更美观。步骤3:T-CODE : SM30维护 T777I 表。步骤5:PPCI 新建 7000屏幕用于显示。标签页 IT 开头 + 信息类型 9106。步骤2:T-CODE : PPCI。步骤4:T-CODE : SPRO。

常回来看看呀!! 1638

详解Redis之持久化

详解Redis持久化之RDB持久化和AOF持久化。

. 9438

吃透 AM32 无人机电调:从源码架构到工作原理的全方位解析(附实践指南)(上)

本文深入解析了AM32电调固件的源码架构与核心模块实现。文章首先介绍了AM32电调作为开源无人机;动力心脏的重要地位,随后从硬件基础、源码目录、模块划分三个维度构建解析框架。重点剖析了系统初始化模块的时钟配置、GPIO初始化和定时器设置,以及FOC控制模块的坐标变换算法、角度估算和SVPWM生成等关键技术实现。通过逐行代码解析,揭示了电调如何将飞控指令转换为电机驱动的完整过程,为开发者理解AM32电调工作原理提供了系统性指导。

bother3000的博客 6656

ADS交叉耦合振荡器VCO相位噪声优化仿真实践

本文详细介绍了在ADS软件中搭建和优化交叉耦合VCO相位噪声的完整仿真实践。通过谐波平衡与瞬态仿真分析电路性能,并重点阐述了通过提升谐振腔Q值、优化晶体管工作点及改善尾电流源来有效降低相位噪声的具体方法,为射频电路设计提供了实用指南。

weixin_29235923的博客 647

[Redis] Redis数据持久化

我们之前在学习MySQL数据库的时候,我们学习过MySQL事务的四个比较核心的特性:分别是原子性(把对多个表的操作打包为一个操作),一致性(事务前后的数据都是合法数据,比如同一个操作中同时减少和增加一个值,要不同时成功,要不同时失败,不能出现中间状态),持久性(把数据存储在硬盘上),隔离性(脏读,不可重复读,幻读,串行化).

无需多言 2698

UE5 Compute Shader实战:从基础到高级应用全解析

本文全面解析了在UE5中应用Compute Shader的完整流程,从基础概念、环境搭建到核心机制与实战应用。详细介绍了如何通过参数传递、线程派遣与数据读写实现GPU并行计算,并深入探讨了GPU粒子模拟、性能优化及结合光线追踪等高级应用场景,为开发者掌握UE5高性能计算提供系统指导。

yhn45678901的博客 509

Redis两种持久化方式

Redis两种持久化方式

水w的博客 1978

AIStarter避坑指南:Wan2.2 Animate整合包从下载到出片的完整流程(附常见错误修复)

本文提供了Wan2.2 Animate整合包从下载部署到生成高清视频的完整避坑指南。详细解析了通过AIStarter启动、环境配置、核心参数调优以及七大常见错误(如CUDA内存不足、节点缺失、视频模糊等)的根因与修复方案,帮助用户高效利用ComfyUI工作流,稳定生成720P高质量AI动画视频。

weixin_29052717的博客 718

新能源汽车整车控制器(VCU)深度全解:硬件原理、工作流程、接口设计与高压安全实战

整车控制器(VCU,Vehicle Control Unit)是新能源纯电动、混动汽车的核心决策大脑,相当于汽车的“中枢神经系统”。它不直接驱动电机,却统筹电机控制器(MCU)、电池管理系统(BMS)、充电机(OBC)、DC/DC、空调、热管理、底盘等全部部件,负责驾驶意图解析、动力输出控制、高压上下电管理、故障安全保护、能量优化等关键任务。VCU所有设计均围绕车规级环境适应性、功能安全(ISO 26262)、实时性、高压安全性四大核心目标,与通用控制器完全不同。

CSDN-新能源汽车研发测试专业博主 2617

【Redis】持久化机制

Redis持久化

chaodddddd的博客 2584

[数学建模][2012年A国赛模拟] 3. 葡萄酒的评价

暑期学校数学建模培训第三次模拟:2012年国赛A题,一道完全统计方面的题目。据老师反映当年有位老师所带领小组取得“全国10佳优秀论文奖”,我们大组搞统计的老师建模能力不行,但其统计理论非常扎实,在此期间给予我们非常大的帮助,十分感谢老师的倾囊相授。我们小组的定位也不在统计方向,主攻物理建模及优化模型,故本次模拟就用SPSS软件简单做了下,还是学到了多元统计的许多知识,达到了本次训练的目的。 ...

Y-puyu 的博客 3万+

Redis的持久化

1.MySQL可以保证事务中的持久性,即只要事务提交,之后无论MySQL挂了还是服务器宕机,数据都不会丢失。 2.而Redis虽然也有持久化策略,但是它缺无法保证持久性,当Redis挂了,往往都会丢失一定的数据。 造成这两种的区别就是它们的持久化策略不同

tazj666的博客 1003

Docker/Hyper-V/Wsl2等虚拟化组件一键切换工具 (附源码, Docker Desktop开发模式与三角洲游戏模式一键切换工具)

开启 Docker、Hyper-V 或 WSL2 后游戏无法启动?🎮💻 本文教你用一键批处理工具⚡快速切换“开发/游戏模式”,开发娱乐两不误!🔄✨

XiaoqiangClub的博客 717

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

redis的持久化

Redis 的持久化机制主要是通过内部的方式来实现的,而不是直接将数据写入到外部数据库(如 MySQL)。下面将详细介绍 Redis 的默认持久化机制,以及如何通过编程的方式将 Redis 数据手动同步到 MySQL。

2301_76723322的博客 1547

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【redis】redis持久化

两种持久化方式(1)RDB(Redis DataBase):内存中数据直接写入文件中(2)AOF(Append Of File):以追加的行为把内容写入文件中。

rita_0567的博客 1635

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Redis 之持久化

Redis提供了两个持久化数据的能力,RDB Snapshot 和 AOF(Append Only FIle)日志,RDB快照能够在指定的时间间隔内生成数据快照,而AOF日志则记录了所有的写操作命令。RDB做镜像全量持久化,AOF做增量持久化。 RDB全称Redis Database Backup file (Redis数据备份⽂件),也被叫做Redis数据快照。简单来说就是把内存中的所有数据都记录到磁盘中。 当Redis实例故障重启后,从磁盘读取快照⽂件,恢复数据。这样一来即使

gorgor在码农 1386

Redis持久化

无论是AOF持久化还是快照持久化,将数据持久化到硬盘上都是非常有必要的,但除了进行持久化外,用户还必须对持久化得到的文件进行备份(最好是备份到不同的地方),这样才能尽量避免数据丢失事故发生。开启AOF持久化后每执行一条会更改Redis中的数据的命令,Redis就会将该命令写入硬盘中的AOF文件。选项一般不推荐,这种方案会使Redis丢失不定量的数据而且如果用户的硬盘处理写入操作的速度不够的话,那么当缓冲区被等待写入的数据填满时,Redis的写入操作将被阻塞,这会导致Redis的请求速度变慢。

2401_89317354的博客 850

Redis --- Redis如何做持久化

Redis如何做持久化 一共三种 一、RDB(快照)持久化:保存某个时间点的全量数据快照 redis.conf save 60 1 代表Redis在60秒内写入一条数据那么进行快照 stop-writes-on-basave-error yes 代表当备份进程出错的时候,主进程就停止写入新的操作了。这样是为了保护持久化数据一致性的问题。 rdbcompression yes 表示...

xinbumi的博客 2万+

Redis持久化的方式

本文主要讲述了redis的rdb,aof持久化的原理流程和配置使用

sjhhshsh的博客 1954

Redis:持久化

讲解Redis的持久化,包括AOF,RDB,混合持久化

博客网站 https://async-area.com 1970
上一篇: TXT 数据文件批量导入DB
下一篇: 代码复习遇到知识点回顾(一)
Coder-Qian
博客等级 码龄11年 63粉丝 73原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址