Home >Database >Redis >AOF method of Redis persistence

AOF method of Redis persistence

齐天大圣
齐天大圣Original
2020-05-22 09:49:051948browse

The RDB method of Redis cannot achieve persistence, but the AOF method can. If the data is important and loss will cause serious consequences, then the RDB method is obviously inappropriate and the AOF method should be used. The aof method is somewhat similar to mysql's binlog log, which only records new, modified, and deleted operations. The difference is that redis will rewrite the aof file every once in a while to reduce the size of the aof file.

AOF workflow

Here, why does the command need to be written to aof_buf first? Because if the aof file is written directly, then the performance depends entirely on Remove the IO performance of the hard disk. Writing aof_buf is to improve writing performance.

Configuration

appendonly: Whether to enable aof persistence mode, the default is no. If you want to enable it, change it to yes.

dir: aof file storage directory

appendfilename: aof file name

appendfsync: aof synchronization mode, there are three values, as follows:

  • always: synchronize every time a command is written, the data security is the highest, but the performance is poor

  • everysec: synchronize every second, the default method, high performance, safe The performance is okay

  • #no: The synchronization operation is left to the operating system, and the data security is the worst.

auto-aof-rewrite-percentage and auto-aof-rewrite-min-size These two configurations are related to the aof rewrite mechanism. Only when these two conditions are met at the same time can Will trigger the rewrite mechanism.

auto-aof-rewrite-min-size means that when rewriting, the file size must be larger than this value. The default value is 64mb

auto-aof-rewrite-percentage means the current file The size must be this much larger than the file size after the last rewrite.

AOF rewriting

The AOF rewriting mechanism of redis has two methods: manual triggering and automatic triggering. To trigger manually, enter the bgrewriteof command. Automatic triggering satisfies all two conditions mentioned above.

Why rewriting can reduce the file size? There are several situations:

  • Expired keys and deleted keys will no longer be recorded

  • Many single operations can be completed by one operation, such as lpush a, lpush b. After rewriting, it is lpush a b.

Let’s take a look at the aof rewriting process

AOF method of Redis persistence

  1. Execute the bgrewriteof command

  2. The main process forks a child process

  3. The original aof mechanism continues to run, and at the same time, new commands are also written into aof_rewrite_buf

  4. The child process generates a new aof file

  5. Notify the parent process that the new aof file has been generated successfully; append the commands in aof_rewrite_buf to the new aof file; replace the old aof file with the new aof file.

  6. After completing the above steps, the aof rewriting is completed.

Note that if there are multiple redis services on a server, it is best to separate their rewriting time to prevent excessive io and cpu competition.

The above is the detailed content of AOF method of Redis persistence. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn