Redis Summary (4) Redis persistence, redis Summary
I have summarized the installation and use of Redis. Today I will talk about Redis persistence.
Similar to memcached, redis is a memory database. However, redis supports data persistence. That is to say, redis can synchronize data in the memory to the disk for persistence to ensure redis data security.
RedisTwo persistence Methods
Redis provides two persistence Methods: RDB (Redis DataBase) and AOF (Append Only File ).
RDB, in short, stores data snapshots on disks,
AOF is to record all write commands executed by redis and append them to the end of the AOF file through the write function. During the next redis restart, you only need to re-execute these write commands from the past to the next time to implement data recovery.
In fact, the RDB and AOF methods can also be used at the same time. In this case, if redis is restarted, The AOF method will be used for data recovery first, this is because the AOF method has a higher degree of data recovery integrity.
If you do not need data persistence, you can completely disable the RDB and AOF methods. In this way, redis will become a pure memory database, just like memcache.
RDB
RDB (Redis DataBase) is a snapshot-type persistence method that persists data in redis to a disk at a certain time point. The default file name is dump. rdb.
In the process of data persistence, redis will first write the data into a temporary file. After the persistence process ends, redis will replace the last persistent file with this temporary file, to ensure full data availability.
Save 300 10 #300 seconds if more than 10 keys are modified, the snapshot is saved.
However, because the snapshot method is performed at a certain interval, if you are sensitive to data integrity, the RDB method is not suitable for you, because even if you persist every two minutes, when redis fails, data will still be lost for nearly two minutes. Therefore, redis also provides another persistence method, namely AOF.
AOF
AOF (Append Only File), that is, Only files that cannot be rewritten can be appended.
Redis will append each write operation (such as SET) received to the end of the AOF file through the write function. The default AOF persistence policy is fsync every second (records write commands in the cache to the disk ).
When Redis is restarted, it re-executes the write commands saved in the file to recreate the entire database content in the memory.
Appendonly yes # enable the aof persistence method # appendfsync always # force write to the disk immediately upon receiving the write command, which is the slowest, but ensures full persistence, it is not recommended to use appendfsync everysec # force write to disk once per second, which makes a good compromise in terms of performance and persistence. It is recommended that # appendfsync no # fully dependent on OS. The best performance is that persistence is not guaranteed.
However, the AOF method records all commands, so the AOF file is larger than the RDB file. In addition, the recovery speed is slower than the RDB method.
Redis provides the bgrewriteaof command to generate a new AOF file, which includes the minimal command set that can restore existing data.
You need to note that the aof file rewriting operation does not read the old aof file, but overwrites a new aof file in the database content in the entire memory using commands, this is similar to snapshot.
How to Choose RDB and AOF
Whether we should choose RDB or AOF depends on the specific application scenario. The official recommendation is to use both. This provides a more reliable Persistence Solution.