Redis持久化方式——RDB|AOF

redis是一种内存数据库,为了保证数据的持久性,提供了三种持久化方案:RDB、AOF、混合方式。
在这里插入图片描述

1. RDB方式

Redis Database。
RDB是通过快照方式完成持久化,在某种条件下将reids数据从内存快照到磁盘。
Redis默认采用RDB作为持久化方式。

当Redis重启时,直接通过这个快照文件将数据恢复到内存即可。

原理

(1)Redis主进程 fork 一个子进程

(2)父进程继续接收并处理客户端发来的命令,而子进程开始将内存中的数据写入硬盘中的临时文件(不会修改原来的RDB文件)。

(3)当子进程写入完所有数据后用该临时文件替换旧的RDB文件,通过这三个步骤完成一次快照操作。

RDB配置

参数:save <seconds> <changes>

################################ 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 "" # 该配置表示不使用配置  

# 满足如下任何一个规则就会触发RDB持久化
save 900 1     # 表示15分钟(900秒钟)内至少1个键被更改则进行快照。
save 300 10    # 表示5分钟(300秒)内至少10个键被更改则进行快照。 
save 60 10000  # 表示1分钟内至少10000个键被更改则进行快照。

优缺点

缺点

会丢失数据,如果Redis异常退出,则会丢失最后一次快照之后的所有数据。如果要保证数据完整的持久化可以采用AOF方式。不过如果Redis只是用来做缓存,某些场景下丢失部分缓存也可以容忍,大不了缓存不能命中就去关系型数据库重新获取。如果是一些关键数据,比如分布式锁、通过Redis生成的全局自增主键,这种数据是万万不能丢的,因此需要保证持久化时数据不丢失。

优点

RDB性能比AOF要好,RDB 文件是 经过压缩的二进制文件 ,占用的空间会小于内存中的数据,更加利于传输。


2. AOF方式

原理

Append Only File

以Redis命令的形式来持久化,Redis每执行一条命令,就会将该命令记录到磁盘的AOF文件中。

Redis重启之后,根据AOF文件重新执行一遍所有命令即可恢复数据。

注意:如果同时存在RDB和AOF文件,则会优先从AOF文件恢复。

AOF配置

AOF默认是关闭的,通过配置打开。

# AOF文件的保存位置和RDB文件的位置相同,都是通过dir参数设置的。 
dir ./

############################## 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.

# yes表示开启、no表示关闭,默认是关闭的。   
appendonly yes 

# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"

缓存同步配置

Redis 每次有数据变更的时, AOF 方式并没有直接把数据写入aof文件(磁盘),而是先写入缓存(操作系统的缓存机制),什么时候将缓存同步到磁盘,通过如下参数来配置:

# 每次执行写入都会进行同步写入磁盘, 这个是最安全但是是效率比较低的方式 
appendfsync always
# 每一秒执行(默认) ,先写缓存,每表执行一次将缓存写入磁盘,存在丢失的风险    
appendfsync everysec
# 不主动进行同步磁盘的操作,什么时候将缓存同步到磁盘交给操作系统去执行控制,这个是最快但是最不安全的方式   
appendfsync no

AOF重写

Redis每次执行变更数据的命令都会记录到AOF文件,这样会导致AOF文件过大,这时需要重写AOF文件(压缩),重写实际上就是精简命令(保留命令的最小集合)。

如多次执行set操作设置一个键值会产生多个set命令,精简之后的命令只记录最后一次set的即可;

如多次执行lpush集合的操作也产生了多条命令,精简之后只需要记录一条lpush所有元素的命令即可。

#如执行三次 set name 
set name 曹操 
set name 孙权
set name 刘备 
#AOF重写优化后变成如下一条指令  
set name 刘备

#如执行两次数组的push操作  
lpush arr 1 2 3
lpush arr 4 5 6
#优化后同样编程一条指令  
lpush arr 1 2 3 4 5 6

注意:重写AOF不需要读取原来的AOF文件,而是直接读取Redis内存数据然后转换成命令,将该这些命令保存到临时文件,最后用这个临时文件覆盖原来的AOF文件。

优缺点

缺点:性能差,每执行一条命令,都需要记录AOF,导致IO频繁影响性能,重启时Redis恢复也慢,因为需要从AOF文件执行所有命令。

有点:数据有保证。

混合方式

Redis4.0 之后新增的方式,混合持久化是结合了 RDB 和 AOF 的优点,最开始先把当前的数据以 RDB 的形式写入文件的开头,再将后续的操作命令以 AOF 的格式存入文件(这种方式将两种数据混合写到一个文件中),这样既能保证 Redis 重启时的速度,又能减低数据丢失的风险。

RDB 和 AOF 持久化各有利弊,RDB 可能会导致一定时间内的数据丢失,而 AOF 由于文件较大则会影响 Redis 的启动速度,为了能同时拥有 RDB 和 AOF 的优点,Redis 4.0 之后新增了混合持久化的方式,因此我们在必须要进行持久化操作时,应该选择混合持久化的方式。

#查看是否开启了混合方式  
> config get aof-use-rdb-preamble
1) "aof-use-rdb-preamble"
2) "yes"  #其中 yes 表示已经开启混合持久化,no 表示关闭,Redis 5.0 默认值为 yes。

通过命令行开启混合方式

# 重启Redis之后会失效  
config set aof-use-rdb-preamble yes

通过修改 Redis 配置文件开启

# 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, then continues loading the AOF
# tail.
aof-use-rdb-preamble yes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值