Explain the Redis persistence mechanism in detail

What is the redis persistence mechanism?

In a word: Persistence is to write memory data to disk to prevent memory data loss when the service is down.

What are the redis persistence mechanisms?

RDB endurance

Redis RDB persistence mechanism

AOF persistence

Redis AOF persistence mechanism

The difference between the two Redis persistence mechanisms?

RDB persistence refers to writing a snapshot of a data set in memory to disk within a specified time interval. The actual operation process is to fork a sub-process, first writing the data set to a temporary file, and then replacing the previous file after the write is successful. , Use binary compression storage.

AOF persistence records every write and delete operation processed by the server in the form of a log. The query operation will not be recorded, but it will be recorded in the form of text. You can open the file to see the detailed operation record.

What are the advantages and disadvantages of the two Redis persistence mechanisms?

The advantages and disadvantages of the RDB persistence mechanism:
advantages:

  • There is only one file dump.rdb, which is convenient for persistence
  • Disaster tolerance is good, and a file can be saved to a safe disk.
  • To maximize performance, fork the child process to complete the write operation and let the main process continue to process commands, so IO is maximized. Use a separate child process for persistence, the main process will not perform any IO operations, ensuring the high performance of redis
  • Compared with the large data set, the startup efficiency is higher than that of AOF.

Disadvantages: low data security. RDB is persisted at intervals. If redis fails between persistence, data loss will occur. So this method is more suitable when the data requirements are not rigorous)

The advantages and disadvantages of the AOF persistence mechanism:
advantages:

  • According to security, aof persistence can be configured with appendfsync attribute, there is always, every time a command operation is performed, it will be recorded in the aof file once.
  • Write files in append mode, even if the server is down in the middle, you can use the redis-check-aof tool to solve the data consistency problem
  • The rewrite mode of the AOF mechanism. Before the AOF file is rewrite (when the file is too large, the command will be merged and rewritten), you can delete some of the commands (such as the incorrectly operated flushall)

Disadvantages:

  • The AOF file is larger than the RDB file and the recovery speed is slow
  • When the data set is large, the startup efficiency is lower than that of rdb

How to choose the appropriate persistence mechanism?

  • Generally speaking, if you want to achieve data security comparable to PostgreSQL, you should use both persistence functions. In this case, when Redis restarts, it will load the AOF file first to restore the original data, because under normal circumstances the data set saved by the AOF file is more complete than the data set saved by the RDB file.

  • If you care about your data very much, but can still withstand data loss within a few minutes, then you can just use RDB persistence.

  • Many users only use AOF persistence, but this method is not recommended, because regular RDB snapshot (snapshot) is very convenient for database backup, and the speed of RDB recovery data set is faster than the speed of AOF recovery, except In addition, the use of RDB can also avoid AOF program bugs.

  • If you only want your data to exist while the server is running, you can also not use any persistence methods.

Guess you like

Origin blog.csdn.net/weixin_44533129/article/details/112685989