save配置表示调用bgsave 。save 60 10000表示如果在60秒内,超过10000个key被修改了,就调用一次bgsave 。同理save 300 10表示300秒内,超过10个key被修改了,就调用一次bgsave 。多个save不是互斥的,如果配置多个save,只要满足其中一个就会执行bgsave,配置多个是为了适应不同的场景 。
配置save ""或者注释所有的save表示不开启RDB 。
从配置文件配置的save参数来看,如果每60秒执行一次bgsave,而在59秒的时候服务宕机了,这样就丢失了59秒内修改的数据,因为还没来得及生成快照 。数据丢失量这么大,肯定是不被允许的 。为此,redis还提供了另一种持久化方式,那就是AOF
AOFAOF(Append Only File)是把对redis的修改命令以特定的格式记录在指定文件中 。也就是说RDB记录的是数据快照,而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"
如果开启了AOF,相应的命令会记录在appendonly.aof文件中 。
appendonly.aof这个文件的内容本身也需要写到磁盘中,如果appendonly.aof还未来得及写入磁盘,服务就宕机了,也会造成appendonly.aof文件内容丢失,而丢失redis的修改命令,进而丢失redis的修改数据 。
为此redis为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 alwaysappendfsync everysec# appendfsync no
这三种方式都是通过参数appendfsync来指定 。
- no:并不是不持久化,只将数据写到OS buffer,由操作系统决定何时将数据写到磁盘,这种方式速度最快
- always:每次在appendonly.aof中追加内容,都调用fsync()将数据写入磁盘,这种方式最慢,但是最安全
- everysec:默认配置,表示每秒调用一次fsync(),将数据写入磁盘,是一种折中的方式
除此之外,appendonly.aof文件是以追加的方式写入命令,对于长时间运行的服务,必定会导致该文件过大 。万一服务宕机需要根据appendonly.aof文件恢复数据,将会消耗相当长的时间来执行appendonly.aof中记录的命令 。
为了解决appendonly.aof文件过大的问题redis提供了一种机制,叫bgrewriteaof 。
bgrewriteaofbgrewriteaof命令描述如下
127.0.0.1:6379> help bgrewriteaofBGREWRITEAOF -summary: Asynchronously rewrite the append-only filesince: 1.0.0group: server
这个命令的作用就是fork()出一个子进程来对appendonly.aof文件进行重写 。这个重写操作在redis4.0以前和4.0以后有不同的实现方式 。
推荐阅读
- redis集群搭建
- Redis命令大全,满足你的日常工作,看这一篇就够了
- Redis这么讲你还不能入门么
- 在SpingBoot中使用Redis对接口进行限流
- 实战基于Redis实现阻塞队列
- redis5.0.7 版本集群liunx部署简易流程
- redis内存碎片
- 一文读懂Redis的dict字典数据结构
- Go语言 连接池相关总结:HTTP、RPC、Redis 和数据库等
- 谈谈 Redis 的过期策略