Redis RDB与AOF持久化机制

Redis 持久化

RDB持久化机制

过程

RDB持久化把当前进程数据生成快照(.rdb)文件保存到硬盘的过程
bgsave 会创建fork操作创建子线程,子线程来做响应的持久化操作

  • save 阻塞当前Redis,线上不建议使用
  • bgsave 执行fork子线程,阻塞时间短

设置保存路径

redis-cli 命令窗口

# 查询保存的路径
127.0.0.1:6379> config get dir
1) "dir"
2) "C:\\software\\Redis-x64-3.2.100\\bat"
# 设置保存路径
127.0.0.1:6379>  config set dir C:\\software\\Redis-x64-3.2.100
OK
# 再查询一下
127.0.0.1:6379> config get dir
1) "dir"
2) "C:\\software\\Redis-x64-3.2.100"
# 将修改配置持久化到redis.conf文件(我用的windows版本对应文件redis.windows.conf)
127.0.0.1:6379> config rewrite
OK

直接修改配置文件
windows版本对应文件redis.windows.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 ./

恢复

生成的dump.rdb文件与redis.windows.conf配置文件同级目录,重启Redis即可

AOF持久化机制

过程

相当于增量持久化

1.所有的写操作,都会先追加到aof_buf缓冲区
2.缓冲区向硬盘做sync同步
3.随着AOF文件不断增大会定期对aof文件做重写操作,达到压缩

# 默认不开启AOF持久化机制
127.0.0.1:6379> config get appendonly
1) "appendonly"
2) "no"
# 默认每秒强制写入磁盘一次,
127.0.0.1:6379> config get appendfsync
1) "appendfsync"
2) "everysec"
# 其他值可选值always、no

# 正在保存RDB时要不要停止同步,默认不停止
127.0.0.1:6379> config get no-appendfsync-on-rewrite
1) "no-appendfsync-on-rewrite"
2) "no"

# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.
# 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.
# 根据文件大小比上次的增长率对比来判定是否重写
127.0.0.1:6379> config get auto-aof-rewrite-percentage
1) "auto-aof-rewrite-percentage"
2) "100"
# 根据文件大小来判定是否重写(67108864/1024/1024=64M)
127.0.0.1:6379> config get auto-aof-rewrite-min-size
1) "auto-aof-rewrite-min-size"
2) "67108864"

恢复

首先需要开启AOF持久化机制(appendonly 为yes)
持久化的文件appendfilename在dir参数指定的目录,默认为appendonly.aof
重启Redis即可自动加载

对比

持久化机制 持久化时间 持久化文件 恢复
RDB 1.手动,2.shutdown且未开启AOF持久化自动执行bgsave 3.非实时 1.dump.rdb 2.二进制文件类型 恢复数据速度快
AOF 开启AOF持久化设置,可实时持久化 1.默认为appendonly.aof 2.可读的懂的文件 速度较慢,但若开启AOF持久化设置,rdb与aof文件同时存在,优先加载aof文件
posted @ 2020-05-28 13:22  VVII  阅读(170)  评论(0)    收藏  举报