Redis persistence (transfer)

The original URL is taken from http://www.javaseo.cn/article/73/

 

 

1. There are two ways to persist Redis :

1. RDB: Snapshots of in-memory database state

2.AOF: Write each write command to a file, similar to mysql's binlog log

 

RDB method: Save the database state of Redis in memory to disk. The RDB file is a compressed binary file, through which the database state when the RDB file is generated can be restored

 

How RDB is generated:

1) Execute the command to generate manually

There are two Redis commands that can be used to generate RDB files, one is SAVE, the other is BGSAVE The SAVE command will block the Redis server process until the RDB file is created. During the blocking period of the server process, the server cannot process any command requests. The BGSAVE command A child process will be forked, and then the child process will be responsible for creating the RDB file. The server process (parent process) continues to process the command request. Before the creation of the RDB file ends, the BGSAVE and SAVE commands sent by the client will be rejected by the server.

2) Automatically generated by configuration

You can set the save option of the server configuration to let the server automatically execute the BGSAVE command every once in a while. You can set multiple save conditions through the save option, but as long as any one of the conditions is satisfied, the server will execute the BGSAVE command.

E.g:

save 900 1

save 300 10

save 60 10000

Then as long as any of the following three conditions are met, the BGSAVE command will be executed

The server has made at least 1 modification to the database within 900 seconds 

The server has made at least 10 changes to the database within 300 seconds 

The server has made at least 10,000 changes to the database within 60 seconds

 

AOF mode: It is a way to refresh the AOF file that records the database status by saving the write command executed by the Redis server. There are three ways:

  • appendfsync always - call fsync to refresh the AOF file every time a modification command is submitted, very very slow, but also very safe
  • appendfsync everysec - call fsync every second to flush to AOF file, very fast, but may lose data less than a second
  • appendfsync no - Rely on OS to refresh, redis does not actively refresh AOF, this is the fastest, but the security is poor

By default, it is recommended to refresh every second, so that both speed and security are taken into account.

 

2. Data recovery

1. RDB method

The loading of the RDB file is automatically executed when the server starts. There is no special command for loading the RDB file. As long as the Redis server detects the existence of the RDB file at startup, it will automatically load the RDB file. During the loading of the RDB file, it will be blocked until the loading work is completed.

2. AOF method

When the server starts, it restores the database state before the server shuts down by loading and executing the commands saved in the AOF file. The specific process is as follows:

Load AOF file

Create a mock client

Read a command from an AOF file

Execute a command using an impersonated client

Read and execute commands in a loop until all is complete

If RDB and AOF are enabled at the same time, AOF takes priority, and only AOF files are loaded to restore data at startup

 

Summary

RDB: Save all the data in the memory to the dump file and save it regularly

AOF: Save all the commands modified to the Redis server into a file, which is a collection of commands

The two methods can be turned on at the same time, but when data recovery, use AOF for data recovery

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326150881&siteId=291194637