The installation and use of Redis has been summarized earlier today to talk about the persistence of Redis.
Redis, like Memcached, is a memory database, but Redis supports data persistence, which means that Redis can synchronize in-memory data to disk to persist to ensure redis data is secure.
Redis two ways to persist
Redis provides two ways to persist, namely, RDB (Redis DataBase) and aof (Append only File).
An RDB, in short, is a way to store a snapshot of a data stored on disk,
AOF is a record of all the write instructions performed by Redis, appended to the end of the AOF file via the Write function. The next time the Redis restarts, the data can be recovered by simply repeating the write commands from the front to the back.
In fact, the RDB and aof two ways can also be used simultaneously, in this case, if the Redis restart, it will take precedence in the AOF way for data recovery, because the AOF mode of data recovery is more complete.
If you don't have the need for data persistence, you can also turn off the RDB and aof mode, so Redis will become a pure memory database, just like memcache.
Rdb
An RDB (Redis DataBase) is a snapshot-persistence method that persists data from a redis moment to disk. The default file name is Dump.rdb.
In the process of data persistence, Redis writes data to a temporary file before the persistence process is finished, replacing the last persisted file with this temporary file to ensure that the data is fully available.
Save #300秒内容如超过10个key被修改, the snapshot save is initiated
However, because snapshots are done at intervals of time, if you are sensitive to the integrity of your data, the RDB approach is not suitable for you, because even if you persist every 2 minutes, you will still have nearly 2 minutes of data loss when Redis fails. So, Redis also offers another way to persist, which is aof.
AOF
AOF (Append only file), which is only allowed to append files that are not allowed to overwrite.
Redis appends each of the received writes, such as set, to the end of the aof file via the Write function. The default AOF persistence policy is fsync once per second (writes the write instruction in the cache to disk).
When Redis restarts, the contents of the entire database are rebuilt in memory by re-executing the write commands saved in the file.
appendonly Yes #启用aof持久化方式 # Appendfsync always #每次收到写命令就立即强制写入磁盘, the slowest, but guaranteed full persistence, is not recommended to use Appendfsync everysec< c5/> #每秒钟强制写入磁盘一次, a good compromise in performance and durability, recommended # Appendfsync no #完全依赖os, best performance, no guarantee of durability
But the AoF method is to record all the commands, so the aof file is larger than the size of the Rdb file. Also, the recovery speed is slower than the Rdb method.
Redis provides the bgrewriteaof command, which regenerates a completely new aof file that includes a minimal set of commands that can restore existing data.
Note that the operation to rewrite the aof file does not read the old aof file, but instead overwrites the entire in-memory database content with a new aof file in the form of a command, which is a bit similar to a snapshot.
How to choose an Rdb and aof
For us to choose an RDB or AOF, depending on the application scenario, the official recommendation is to use both simultaneously. This can provide a more robust solution for persistence.
Redis Summary (iv) The persistence of Redis