AOF persistence means recording each write command in an independent log, and re-executing the commands in the AOF file when Redis restarts to achieve the purpose of data recovery. The main role of AOF is to solve the problem of data persistence. real-time.
Redis is an in-memory database that stores data in memory, and its read and write efficiency is much faster than traditional databases that store data on disk. . But once the process exits, the Redis data will be lost.
In order to solve this problem, Redis provides two persistence solutions, RDB and AOF, to save data in memory to disk to avoid data loss. The introduction of RDB is in this article "Detailed Explanation of Redis RDB Persistence". Today we will take a look at the principles related to AOF.
AOF (append only file) persistence records each write command in an independent log, and re-executes the command in the AOF file when Redis restarts to recover the data. The main function of AOF is to solve the real-time problem of data persistence.
RDB and AOF
antirez described the respective advantages and disadvantages of RDB and AOF in the article "Redis Persistence Decryption":
RDB is a compact, compressed binary file that represents Redis data backup at a certain point in time. Very suitable for backup, full copy and other scenarios. For example, perform bgsave backup every 6 hours and copy the RDB file to the remote machine or file system for disaster recovery.
Redis loads RDB to restore data much faster than the AOF method
RDB method data cannot be persisted in real time, and the AOF method can do.
What are the steps for AOF persistence?
1. Command appending steps
First, the server puts the write operation command to the database into the AOF buffer, and uses a periodic function to check whether the content in the buffer needs to be written to the AOF file ( There are generally three strategies: always, every second, no). These three strategies are a balance between security and efficiency.
2. Data restoration
Restore data by creating a pseudo client without a network connection and executing the commands in AOF in sequence.
3. AOF rewriting
Because AOF records write operation commands, it will cause file redundancy. As long as the current status of the database is re-read, AOF can be streamlined.
The specific process is as follows:
First create a child process (why not use threads? Because threads will cause locks, resulting in low efficiency. And using child processes will not block the parent process)
Then the parent process creates an AOF Rewrite buffer used to record write commands executed during AOF rewrite.
A write command will be sent to both the AOF buffer and the AOF rewrite buffer.
Then when the rewriting is completed, the child process sends a signal to the parent process. The parent process will append the contents of the AOF rewrite buffer to the new AOF file and atomically replace the original AOF file.
Recommended tutorial: "Redis Tutorial"
The above is the detailed content of What does AOF persistence mean?. For more information, please follow other related articles on the PHP Chinese website!