RDB persistence (redis server papers)

RDB endurance of

Storing the data to the hard disk in the form of a binary file which

Endurance of

Because the Redis server inside the data stored in memory, and once the server is shut down, or run the server host itself is turned off, the data stored in the memory inside will disappear.

If we just will Redis as a cache, then the problem that data loss is not very large, we only need to restart the machine, and then the data into the cache and again on it; but if we as a Redis database, then that data loss can not be accepted.

To Redis server when closed, retains data in a database, RDB and AOF Redis provides two kinds of persistence function, these two functions can be stored in the database data memory which is stored as a file to the hard drive inside, so even if the server is down, has been saved to the hard disk inside the data will not be lost.

In addition, the server can restart to restore the database server data before closing the file by loading persistence. Or use the persistent file for data backup, data migration work.

RDB endurance of

RDB persistence function allows all database servers containing the data saved to the hard drive inside in the form of binary files.
Here Insert Picture Description
And by loading the RDB file at server startup, the server can restore the original database server RDB data based on the contents of the file.
Here Insert Picture Description

Create a file RDB

So Redis server when will create RDB file it?
In the case of Redis server to create RDB file, the following three are the most common:

  1. SAVE server executes the command sent by the client;
  2. BGSAVE server executes commands sent by the client;
  3. Use the save configuration option settings are automatically saved condition is satisfied, the server automatically performs BGSAVE.

In all three cases RDB file is created, the first two cases need to be performed manually, and the third case is performed automatically by the Redis server.

The following describes the contents of the same and different from the three cases in detail.

RDB files manually create

The client sends a SAVE or BGSAVE

SAVE command

By sending a SAVE command to the server using the client, you can command the server to create a new RDB file:
Redis> SAVE
the OK
Here Insert Picture Description
during the execution of the SAVE command in (that is, the process of creating RDB file), Redis server will be blocked, command can not process the request sent by the client only after SAVE command is complete (that is, after the file is created RDB) command, the server will start to reprocess the client sends a request.

If the RDB file already exists, then the server will automatically use the new RDB RDB file to replace the old file.

SAVE command to the complexity of O (N), N is the server, the database contains all the key number of the sum.

BGSAVE command

BGSAVE command execution can also create a new RDB file, this distinction is that the command and SAVE command, BGSAVE Redis server will not cause obstruction : during the execution of the command BGSAVE, Redis server can still handle other commands sent by the client normally request.

The reason BGSAVE command does not cause the server to block that:

  1. When the server receives BGSAVE Redis commands, it does not own to create the RDB file, but () to generate a child process by fork, then responsible for the child of a RDB file, while he continues to deal with the client command requests;

  2. When a file is created RDB child process and exit, it will be the parent process (that is, Redis server responsible for handling the request command) to send a signal telling it to RDB file has been created;

  3. RDB file last Redis server (parent) receives the child process is created, BGSAVE finished.

BGSAVE command execution (1/2)

Here Insert Picture Description

BGSAVE command execution (2/2)

Here Insert Picture Description

BGSAVE command

BGSAVE 是一个异步命令,发送命令的客户端会立即得到回复,而 实际的操作在回复之后才开始:
redis> BGSAVE
Background saving started

BGSAVE 命令的复杂度和 SAVE 一样,都是 O(N) ,N 为所有数据库包含的键值对数量总和。

SAVE 和 BGSAVE 的区别

Here Insert Picture Description
SAVE 和 BGSAVE 没有孰好孰坏之分,需要考 虑的是哪个更适合你。

自动创建 RDB 文件

save 配置选项

自动 RDB 持久化

以上提到的 SAVE 和 BGSAVE 都是手动操作,它们都需要用户介入。

为了让 Redis 服务器可以自动进行 RDB 持久化操作,Redis 提供了 save 配置选项,通过这个选项,用户可以设置任意多个保存条件,每当保存条件中的任意一个被 满足时,服务器就会自动执行 BGSAVE 命令。

save 选项的格式为:
save

这个选项的效果为:如果距离上一次创建 RDB 文件已经过去了 seconds 秒,并且服务器的所有数据库总共已经发生了不少于 changes 次修改(包括添加、删除和更新),那么执行 BGSAVE 。

save 选项示例

举个例子,设置:
save 300 10
表示“如果距离上一次创建 RDB 文件已经过去了 300 秒,并且服务器的所有数据库总共已经发生了不少于 10 次修改,那么执行 BGSAVE 命令”。 而设置:
save 60 10000
则表示“如果距离上一次创建 RDB 文件已经过去了 60 秒,并且服务器的所有数据库总共已经发生了不少于 10000 次修改,那么执行 BGSAVE 命令”。

save 多选项示例

Further, the user can also set a plurality of conditions by providing a plurality of automatically save save option, when any one condition is satisfied, the server will automatically execute BGSAVE command.
For example, it is provided for the following:
Save 900. 1
Save 300 10
Save 10000 60
whenever any of the three conditions is met, the server executes BGSAVE.

After each RDB file is created, a time counter server to automatically set the persistence and frequency counter will be cleared and resume counting, so the effect of multiple storage conditions are not superimposed.

RDB file Overview

Uncover the truth RDB file
Here Insert Picture Description

review

Redis provides RDB AOF persistence and persistence of these two persistent functions for database data stored in memory is saved to the hard disk inside the inside in the form of a file, so no data is lost because the server is shut down.

RDB file is a binary file, it saves the file when creating RDB, data for all database Redis server.

The most common file created RDB three methods are: 1, the implementation of SAVE command; 2, execute BGSAVE command; 3, using the save option to save the condition, let the server automatically performs BGSAVE.

Server will be blocked in the implementation of SAVE command, resulting in not processing commands sent by the client request.

BGSAVE in the implementation will not block the server, because the creation RDB file is executed by the child, but it also makes the execution BGSAVE will consume more memory than the implementation of SAVE, and create ㏿ of RDB file will be slower than some of SAVE .

Users can set up multiple save options, when any one of the storage conditions are met, BGSAVE will be executed.

Published 252 original articles · won praise 151 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_39885372/article/details/104272892