Redis - 持久化
Redis RDB
在进行RDB的过程中 Redis会进行fork一个子进来进行持久化
RDB 默认开启
➜ redis ./redis-5.0.4/src/redis-cli -h 127.0.0.1
127.0.0.1:6379> DBSIZE
(integer) 10000002
127.0.0.1:6379> bgsave
Background saving started
127.0.0.1:6379>
➜ redis ps -ef|grep redis
cmm 4796 1556 0 21:34 ? 00:00:06 /usr/share/code/code redis.conf
cmm 5730 1556 39 21:46 ? 00:03:18 ./redis-5.0.4/src/redis-server 127.0.0.1:6379
cmm 6099 4473 0 21:53 pts/0 00:00:00 ./redis-5.0.4/src/redis-cli -h 127.0.0.1
cmm 6136 5730 75 21:54 ? 00:00:02 redis-rdb-bgsave 127.0.0.1:6379
cmm 6138 6105 0 21:54 pts/2 00:00:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn redis
➜ redis ps -ef|grep redis
cmm 4796 1556 0 21:34 ? 00:00:06 /usr/share/code/code redis.conf
cmm 5730 1556 38 21:46 ? 00:03:18 ./redis-5.0.4/src/redis-server 127.0.0.1:6379
cmm 6099 4473 0 21:53 pts/0 00:00:00 ./redis-5.0.4/src/redis-cli -h 127.0.0.1
cmm 6148 6105 0 21:54 pts/2 00:00:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn redis
➜ redis
其中这里有一个 cmm 6136 5730 75 21:54 ? 00:00:02 redis-rdb-bgsave 127.0.0.1:6379 这样的进程
当RDB完成之后这个字进程就销毁了
在进行RDB过程中会使用临时文件
➜ redis ./redis-5.0.4/src/redis-cli -h 127.0.0.1
127.0.0.1:6379> bgsave
Background saving started
127.0.0.1:6379>
➜ redis ll
总用量 157M
-rw-r--r-- 1 cmm cmm 105M 2月 7 21:54 dump.rdb
drwxr-xr-x 6 cmm cmm 4.0K 3月 19 2019 redis-5.0.4
-rw-rw-r-- 1 cmm cmm 1.9M 3月 23 2019 redis-5.0.4.tar.gz
-rw-r--r-- 1 cmm cmm 61K 2月 7 21:35 redis.conf
-rw-r--r-- 1 cmm cmm 51M 2月 7 21:58 temp-6232.rdb
➜ redis ll
总用量 107M
-rw-r--r-- 1 cmm cmm 105M 2月 7 21:58 dump.rdb
drwxr-xr-x 6 cmm cmm 4.0K 3月 19 2019 redis-5.0.4
-rw-rw-r-- 1 cmm cmm 1.9M 3月 23 2019 redis-5.0.4.tar.gz
-rw-r--r-- 1 cmm cmm 61K 2月 7 21:35 redis.conf
➜ redis
这里会存在一个 temp-6232.rdb 这样一个临时文件
当RDB完成之后这个临时文件就被删除了
RDB触发机制
- 当shutdown时,若没有开启aof,会触发 (shutdown属于正常关机 kill -9 属于意外关机 不会触发RDB)
- 配置文件中的默认快照配置 如下图节选自 redis.conf 的默认配置
################################ 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
- 执行bgsave,save bgsave 是后台处理, save是会阻塞的
当把
save 900 1
save 300 10
save 60 10000
这个三个save配置去除掉后,就关闭了RDB功能
RDB 在主从复制模式的话 是无法关闭
Redis AOF
AOF 保存的是redis执行的命令 (协议) 默认配置 AOF 1秒1次
配置
no : 表示等操作系统进行数据缓存同步到磁盘 (块,持久化没有保证)
always: 同步持久化,每次发生数据变革时,立刻同步到磁盘 (性能损耗大,比较慢,相对安全)
everysec : 每秒进行一次同步操作 (默认配置,很快,但是有可能会丢失一秒内的数据)
如下面代码片段所示 默认未开启AOF 默认文件名:“appendonly.aof” 默认执行模式 appendfsync everysec
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
AOF 保存的是redis执行的命令 (协议)
下面shell 代码片段展示的就是AOF文件的内容
➜ redis ./redis-5.0.4/src/redis-cli -h 127.0.0.1
127.0.0.1:6379> set name NewshiJ
OK
127.0.0.1:6379>
➜ redis cat appendonly.aof
*2
$6
SELECT
$1
0
*3
$3
set
$4
name
$7
NewshiJ
*2 代表有两个部分组成 $6 表示有6个字符 $1 表示一个1字符 所以 推算出来结果是 “SELECT 0”
*3 表示3个部分 所以 “set name NewshiJ”
这就是Redis的协议 jedis也就是这么做的
AOF重写机制
当AOF文件增长到一定大小的时候Redis能够调用bgrewriteaof对日志进行重写 (对文件进行瘦身,无效命令进行合并)
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
auto-aof-rewrite-percentage 100 --> 当AOF文件大小的增长率大于该配置项时自动重写(这里指的是超过原大小的100%)
auto-aof-rewrite-min-size 64mb --> 当AOF文件大小达到一定大小时,Redis能够调用bgrewriteaof对日志文件进行重写,当AOF文件大小达到该配置项是自动开启重写
(这个配置生产环境需要修改,一般改成N个G大小,重写AOF文件也是很消耗性能)
redis 4.0 后混合持久化
开启混合持久化后,aof rewrite 时候会将rdb文件直接放到aof文件的前面,后面再存放aof记录的命令(协议格式)
- 优点: 这样做的好处是可以结合rdb和aof的有点,可以快速加载并且避免同时丢失过多的数据.
- 缺点: aof中rdb部分是压缩的二进制格式,不在是aof格式可读性差,并且4.0之前版本的Redis无法识别这个aof文件
下面代码为redis-5.0.4的配置文件,可以看到在5.0.4版本中是默认开启的 (但是aof没有默认开启,需要先开启aof)
# 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 1AOF 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
下面是实战演示的shell片段
➜ redis ./redis-5.0.4/src/redis-cli -h 127.0.0.1
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> set readlName ChenMM
OK
127.0.0.1:6379> BGREWRITEAOF
Background append only file rewriting started
127.0.0.1:6379> set age 24
OK
127.0.0.1:6379>
➜ redis cat appendonly.aof
REDIS0009� redis-ver5.0.4�
redis-bits�@�ctime¼{=^used-mem°�
㼌/�*2 aof-preamble���nameNewshiJ readlNameChenMM���
$6
SELECT
$1
0
*3
$3
set
$3
age
$2
24
➜ redis
集群模式
集群模式 : 哨兵(小型公司),cluster(大厂)
单机问题
- 单价故障
- 容量瓶颈
- qps瓶颈
主从复制
- 读写分离
- 容灾备份
主从可以处理qps瓶颈问题
哨兵可以处理单机故障问题(单纯的主从复制,无法做到自动的故障转移) 在故障转移期间,Redis集群是不可用的
Redis 四大问题
- 缓存穿透
- 使用布隆过滤器
- 往redis里面添加这个不存在的key
- 缓存击穿
- 当访问获取不到目标数据时,使用互斥所限制数据库访问
- 缓存雪崩
- 使用cluster 分布式模式
- 设置随机的过期时间
- 缓存与数据库数据一致性问题
- 延迟双删 先删除缓存,再修改数据库,最后再删除一次缓存(需要一定时间的延迟)
- 串行化 当修改数据库和线程Redis查询为null时,加入到访问队列中 修改数据库前先进行一次删除Redis的操作