redis数据持久化
1、说明
在使用Redis
时候,我们会存在重启Redis
情况,而Redis
数据都保存在内存中,重启之后会存在丢失情况,此时我们想要Redis
可以持久化,在重启之后Redis
可以从持久化层获取数据,重新放入内存。redis提供两种方式持久化:RDB
方式 与AOF
方式。
2、RDB 持久化方式
2.1 RDB持久化快照方式介绍
机制:根据规则“定时”将内存中的数据存储的硬盘上。是使用内存快照方式。
将内存中的数据以快照的方式写入二进制文件中,默认的文件名是dump.rdb
,存在当前进程目录。内容如下:
以下几种情况会触发快照:
- 根据设定的规则自动触发;
- 用户主动执行
save
或者bgsave
命令; - 执行
FLUSHALL
命令; - 执行复制(
replication
)时;
(1) save
与bgsave
区别
save
命令操作会阻塞客户端的所有请求,将数据进行快照,如果Redis内存数据过大时,Redis会出现较长时间不响应,所以不适用于生产。
bgsave
命令操作是后台异步执行快照操作,服务器同时可继续响应来自客户端的请求
- 查看最后一次快照时间命令:
lastsave
, 返回unix
时间戳(秒级)
127.0.0.1:6379[1]> lastsave
(integer) 1625462617
(2)flushall
命令清除数据时执行快照
只要自动快照条件不为空,在执行flushall
时都会执行一次快照,与配置的自定义快照条件是否满足无关 。
(3)执行复制时触发快照
redis主从模式下,在复制初始化的时候会触发快照操作。
2.2、RDB中快照原理
(1)redis使用fork函数复制一份当前进程(父进程)的副本(子进程)。
(2)父进程继续接收并处理客户端发来的命令,而子进程开始将内存中的数据写入硬盘中的临时文件
(3)当子进程写入完所有数据后会用该临时文件替换旧的RDB文件完成快照。
注
:fork的副本内存小于redis使用内存,如,总共内存4G, redis 已使用3G, 做fork时候不会是占用6G。
2.3、持久化配置方式
修改redis配置文件:/usr/local/redis-5.0.5/redis.conf
中
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
配置含义:
900秒内,如果超过1个key被修改,则发起快照保存
300秒内,如果超过10个key被修改,则发起快照保存
60秒内,如果1万个key被修改,则发起快照保存
3、AOF( appedn only file
)持久化方式
机制:每执行一条
更改Redis内存数据的命令
都会将命令以纯文本形式保存在硬盘上。
3.1 配置AOF持久方式
aof
修改redis.conf文件,默认appendonly
为 no
,把no
改为yes
重启redis即可。
############################## 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.
appendonly no
# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"