What persistence mechanisms are provided by Redis :
1). RDB Persistence:
This mechanism is to write a snapshot of an in-memory dataset to disk at a specified time interval.
2). AoF Persistence:
This mechanism will log every write that the server processes, read the file in the Redis server startup be made early, and rebuild the database to make sure that the data in the database is complete after startup.
3). No persistence:
We can disable the persistence of the Redis server in a configurable way so that we can see Redis as a memcached of a feature-enhanced version.
4. Apply AoF and Rdb at the same time.
Ii. Advantages and disadvantages of the RDB mechanism:
What are the advantages of RDB?
1. Once this approach is adopted, your entire Redis database will contain only one file, which is perfect for file backups. For example, you might want to archive the last 24 hours of data every hour, and archive the last 30 days of data every day. With such a backup strategy, we can easily recover from a catastrophic failure of the system.
2. For disaster recovery, RDB is a very good choice. Because we can easily compress a separate file and then transfer it to another storage medium.
3). Maximize performance. For a Redis service process, the only thing it needs to do when it starts to be persistent is to fork the subprocess, and then do the persistent work by the child process, which can greatly prevent the service process from performing IO operations.
4. Compared with the AOF mechanism, if the dataset is large, the RDB will be more efficient to start.
What are the disadvantages of RDB?
1. If you want to ensure the high availability of data, that is, to maximize the avoidance of data loss, then RDB will not be a good choice. Since the system has been down before the timer is persisted, data that has not been written to the disk in time will be lost.
2. Since RDB assists with data persistence through the fork subprocess, it can cause the entire server to stop serving hundreds of milliseconds, or even 1 seconds, if the dataset is large.
Iii. Advantages and disadvantages of the AOF mechanism:
What are the advantages of aof?
1. This mechanism can bring higher data security, namely data persistence. The 3 synchronization strategy is provided in Redis, that is, synchronization per second, synchronization per modification, and Out-of-sync. In fact, synchronization per second is also done asynchronously, and its efficiency is very high, the difference is that once the system occurs, then the modified data will be lost within a second. And every time we modify the synchronization, we can see it as a synchronous persistence, that is, each occurrence of the data changes will be immediately logged to the disk. It can be foreseen that this approach is the lowest in efficiency. As for no synchronization, needless to say, I think we all can understand it correctly.
2. Because this mechanism uses the Append mode for write operations on log files, it does not destroy what already exists in the log file even if there is a downtime during the write process. However, if we only write half of the data to the system crash problem, don't worry, before the next boot Redis, we can use the Redis-check-aof tool to help us solve the problem of data consistency.
3. If the log is too large, Redis can automatically enable the rewrite mechanism. That is, Redis writes the modified data to the old disk file in Append mode, and Redis also creates a new file to record which modification commands were executed during this period. Therefore, the data security can be better ensured when the rewrite switch is made.
4. AOF contains a clear, Easy-to-understand log file that records all of the modification operations. In fact, we can also complete the reconstruction of the data through this document.
What are the disadvantages of aof?
1. For the same number of datasets, AOF files are typically larger than RDB files.
2. Depending on the synchronization strategy, AOF tends to be slower than RDB in its operational efficiency. In summary, synchronization policies are more efficient per second, and synchronous disabling policies are as efficient as RDB.
Iv. Other:
1. Snapshotting:
By default, Redis dumps snapshots of datasets into DUMP.RDB files. In addition, we can modify the frequency of the Redis server dump snapshot through the configuration file, after we open the 6379.conf file, we search for save and we can see the following configuration information:
Save 900 1 #在900秒 (15 minutes), dump memory Snapshot if at least 1 keys have changed.
After save #在300秒 (5 minutes), a dump memory snapshot is taken if at least 10 keys change.
After the Save 10000 #在60秒 (1 minutes), the dump memory snapshot if at least 10,000 keys have changed.
2. Dump snapshot mechanism:
1). Redis first fork the child process.
2. The child process writes the snapshot data to the temporary RDB file.
3. When the child process completes the data write operation, it replaces the old file with the temporary file.
3. AOF Documents:
As has been said many times above, the RDB snapshot timing dump mechanism does not guarantee good data persistence. If our application is really focused on this point, we can consider using the AOF mechanism in Redis. The default mechanism for Redis servers is RDB, and if you need to use AOF, you need to modify the following entries in the configuration file:
Change AppendOnly No to appendonly Yes
From now on, Redis appends the data modification to the AoF file every time it receives a command. The next time Redis restarts, you need to load the information in the AoF file to build the most current data into memory.
4. Configuration of aof:
There are three kinds of synchronization methods in the Redis configuration file, respectively:
Appendfsync always #每次有数据修改发生时都会写入AOF文件.
Appendfsync everysec #每秒钟同步一次, this policy is the AOF default policy.
Appendfsync no #从不同步. Efficient but data will not be persisted.
5. How to repair the damaged aof files:
1. An additional copy of the existing AOF document which has already been damaged.
2. Perform the "Redis-check-aof--fix <filename>" command to fix a damaged aof file.
3. Restart the Redis server with the repaired aof file.
6. Redis Data Backup:
In Redis we can back up the running Redis data files online in copy mode. This is because RDB files will not be modified once they are generated. Redis each time dump the latest data into a temporary file, and then rename the temporary file to the original data file name using the Rename function atomicity. So we can say that copy data files are safe and consistent at any time. With this in mind, we can periodically back up the Redis data files by creating a cron job and copy the backup files to a secure disk media.