Redis is an in-memory database that supports persistence, which means that Redis often needs to synchronize the in-memory data to the hard disk to ensure persistence.
Redis supports two persistence modes:
1. Snapshot (snapshot), is also the default mode;
This is done by writing the in-memory data to the binary file as a snapshot, with the default file name Dump.rdb. You can automate snapshot persistence by configuring settings.
Save 1 #900秒内如果超过1个key被修改, the snapshot is initiated
Save #300秒内如果超过10个key被修改, the snapshot is initiated
Save 10000 #60秒内如果超过10000个key被修改, the snapshot is initiated
As we can see, there is no Dump.rdb file when the Redis is finished, the system automatically generates DUMP.RDB when the snapshot requirements are met, and the file type is a binary file.
2.append-only file (aof) mode;
Because the snapshot is done at a certain interval, if Redis accidentally falls down, all modifications after the last snapshot are lost.
AOF is more persistent than snapshot mode, because Redis appends each command received to the file via the Write function when using AOF, and when Redis restarts, it is re-executed by the
Write commands to rebuild the entire contents of the database in memory.
Of course, because the OS caches write modifications in the kernel, it may not be written to disk immediately. This way, the persistence of the AoF method is also likely to lose some modifications
The configuration file tells Redis the time we want to force the OS to write to disk through the Fsync function.
Appendfsync always #收到命令立即写入磁盘, the slowest, but guaranteed full persistence
Appendfsync everysec #每秒钟写入磁盘一次, a good compromise in performance and persistence
Appendfsync no #完全依赖os, best performance, no guarantee of durability
Let's take a look at the shell:
Edit redis.conf file, modify AppendOnly No to appendonly Yes
Start Redis again and add a key
We can see that the system automatically generated a appendonly.aof file, and non-binary, we use cat to view, in fact, the store is the new command we just added.
Redis persistence mechanism