Redis backup

RDB (snapshot backup)

RDB snapshot process:

RDB文件内容紧凑,文件小(比AOF文件小)非常适合于灾难恢复
  • 1. Redis uses the fork function to copy a current parent process as a child process
  • 2. The parent process continues to process user requests, and the child process begins to persist the data in memory to disk
  • 3. When the child process writes the data in the memory to the temporary file, it will replace the old RDB file with the temporary file.
功能核心函数rdbSave(生成RDB文件)和rdbLoad(从文件加载内存)两个函数

Insert picture description here

Snapshot

允许你每隔一段时间对内存数据做一次快照然后存储到硬盘中。

Directory and file name

The default path of the RDB persistent file to the disk is in the current directory, and the file name is dump.rdb. You can specify the file directory and file name through the configuration file dir and dbfilename. Parameters to compress

advantage:

Suitable for large-scale data recovery, low data integrity

Disadvantages:

During the interval of downtime, data is lost; the fork process will occupy a certain amount of memory space.

Trigger mechanism:

  • 1. Save rule meets will trigger RDB rule
  • 2. Execute flushall command
  • 3. Exit redis also generates dump.rdb file

AOF (Command Backup)

保存所有的写命令到文件

Synchronization process:

  • 1. Redis executes fork () and now has both parent and child processes.
  • 2. The child process starts writing the contents of the new AOF file to a temporary file.
  • 3. For all newly executedWriteCommand, the parent process accumulates them in a memory cache while appending these changes to the end of the existing AOF file: this way, even if there is a stop in the middle of rewriting, the existing AOF file is still safe.
  • 4. When the child process finishes rewriting, it sends a signal to the parent process. After receiving the signal, the parent process appends all the data in the memory cache to the end of the new AOF file.
  • 5. Get it done! Now Redis replaces the old file atomically with the new file, after which all commands will be appended directly to the end of the new AOF file
每当执行服务器(定时)任务或者函数时flushAppendOnlyFile 函数都会被调用, 这个函数执行以下两个工作
aof写入保存:
WRITE:根据条件,将 aof_buf 中的缓存写入到 AOF 文件
SAVE:根据条件,调用 fsync 或 fdatasync 函数,将 AOF 文件保存到磁盘中。

Insert picture description here

Directory and file name

By recording the write operation command sent to the server to form an AOF file, the default name of the file is appendonly.aof, you can specify the file name through appendfilename

When AOF synchronizes the memory data to the disk, it does not immediately write the file to the disk, but first caches the file to the system, and then writes the file to the disk every 30 seconds.

Repair file

If the aof file has errors and redis cannot start, you can use
redis-check-aof --fix appendonly.aof in the bin directory to repair the backup file.

advantage:

1. Every modification is synchronized, the integrity is better
2. Synchronized once per second, may lose data
3. Never synchronized, then the operating system synchronizes the data by itself, the fastest and most efficient.

Disadvantages:

1. The aof data file is larger than rdb, and the repair speed is slow.
2. The operating efficiency of
AOF is much lower than that of RDB 3. One data and one record for RDB, AOF may have multiple data

How to turn it on (not turned on by default):

Modify appendonly yes in the configuration file

Published 193 original articles · Like 13 · Visitors 40,000+

Guess you like

Origin blog.csdn.net/u013919153/article/details/105602136