redis-2,redis持久化
持久化
rdb:snapshot快照,持久化快照
aof:append only file 写命令操作全部记录下来
RDB
rdb持久性以指定的时间间隔,执行数据集的时间点快照,全量快照
rbd保存到磁盘的文件就是dump.rdb
案例
配置文件redis.conf
在配置文件中找到snapshotting
################################ 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.
快照默认触发方式有以下三种
900秒之内修改过一次
300秒之内修改过10次
60秒之内修改过10000次
这些情况下触发保存快照
# 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:
在这里禁用所有rdb
# 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.
yes代表一致性最高,bgsave保存出错马上停止写入。
配置成no,备份和写入各自干各自的,bgsave出错了也不停止redis服务
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.
压缩,lzf压缩算法,消耗cpu,但是消耗的不多
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.
文件校验
rdbchecksum yes
# The filename where to dump the DB
这里修改保存的名字
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 /www/server/redis/
rdb的操作分为自动触发和手动触发。
当写一个提交命令的时候,比如flushdb,flushall,redis也会自动保存一份快照
shutdown命令模拟宕机的时候,也会保存最后一份快照
修改dump文件的保存路径
修改dump文件的名称
如何恢复
将备份文件dump.rdb移动到redis的安装目录并启动服务即可
rdb手动触发
save和bgsave
redis提供了两个命令来手动生成rbd文件,save和bgsave,相当于后台开了一个子进程,后台备份rbd文件
save
生产上用bgsave不用save,在主程序中执行save会阻塞redis服务器,直到持久化工作完成,执行save期间不能执行其他操作
bgsave
redis会在后台异步进行快照操作,不阻塞快照同时还会响应客户端的请求,该触发方式会fork一个子进程,由子进程复制持久化过程
lastsave
获取上次备份的时间
获取到时间戳,通过linux转换
linux转换时间戳
date -d @1681110403
rdb的优点和缺点
优点:单文件的时间点表示,非常适合备份
不影响性能,可以使用子进程进行io操作,方便大数据集的恢复
缺点:对断电的适应性并不好
fork的时候内存所有数据都被复制了一份,对性能也有要求,备份的时候也会有io操作,影响一部分性能
rbd文件修复
redis-check-rdb修复文件
redis-check-rdb [rdbfilepath]
会产生rdb文件的操作
- 配置文件中默认的kyaizhaopeizhi
- 手动save/bgsave
- 执行flushall/flushdb命令也会自动产生,但产生的是空的rdb文件
- 执行shutdown且没有设置开启aof持久化
- 主从复制时,主节点自动触发
禁用rdb
动态停止所有rdb保存的方法,
redis-cli config set save ""
也可以在配置文件中直接禁用
rdb的优化
aof (append only file)
将redis执行过的所有写指令记录下来,之追加文件但不改写文件,redis启动之初会读取该文件重新构建数据。
默认情况下没有开启。
要激活需要在配置文件中开启。appendonly yes
aof保存的是appendonly.aof文件
aof配置文件
############################## 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"
# 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".
三种写回策略
# 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. 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.
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-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-use-rdb-preamble yes
写会策略
always 同步写回,每个写命令执行完like同步的写回到磁盘
everysec 每秒写回,每个写命令执行完,等下一秒再写回
no 操作系统控制的写回,每个写命令执行完,只是先把命令写到aof文件的内存缓冲区,由操作系统决定何时写回磁盘
mp-aof
redis7,底层改写了,性能影响更小
redis7之前aof只有一个,7之后使用mp-aof
mp-aof
mp-aof是将原来的单个aof文件拆分成多个aof文件,分为三种类型
base:基础aof,他一般由子进程通过重写产生,该文件只有一个
incr:表示增量aof,他一般会在aofrw开始执行时创建,该文件可能存在多个
一般是这个进行写记录
history:表示历史aof,他由base和incr变化而来,每次aofrw成功完成时,本次aofrw之前对应的base和incr aof都会变成history,history类型的会被redis自动删除
为了管理这些aof,引入了一个manifest文件来管理。
如果

浙公网安备 33010602011771号