Home > Database > Redis > body text

RDB for redis data persistence

齐天大圣
Release: 2020-05-22 09:03:10
Original
1601 people have browsed it

Different from Memcache, Redis can persist data to the hard disk. Redis currently provides three persistence methods, RDB, AOF, and RDB-AOF hybrid persistence. Data security and backup are the focus of operation and maintenance work. Let’s take a look at the introduction and application scenarios of RDB persistence.

The default persistence method used by Redis is RDB. RDB files take up very little space, so the file generation and loading speed are very fast.

Generate RDB file

Generating RDB file is divided into manual method and automatic method.

First look at the manual method. There are two commands that can trigger the generation of RDB files. The difference between save and bgsave is that the save operation blocks redis until the RDB file generation is completed. But bgsave will not block redis. It will fork out a child process and complete the generation of rdb file in the child process.

There are several situations in the automatic mode, as follows:

  • The modification operation of the current key meets the configuration requirements of Rdb in redis

  • When the master-slave node performs full replication

  • When restarting or shutting down redis (Redis persistence mode is RDB)

Here, we focus on the related configuration of rdb.

The directory where the rdb file is saved is determined by the dir configuration item

# rdb文件保存目录
dir "/usr/local/redis/var"
Copy after login

The file name is determined by the dbfilename

dbfilename "dump.rdb"
Copy after login

The triggering mechanism is determined by the save item

save 900 1
save 300 10
save 60 10000
Copy after login

The meaning of the above configuration is that it will be triggered when there is one modification operation within 900 seconds, it will be triggered when there are 10 modification operations within 300 seconds, and it will be triggered when there are 10,000 modification operations within 60 seconds.

In addition, the rdbcompression configuration item determines whether to compress the rdb file. The default is yes, which means compression, which is also the recommended method.

RDB file generation process

RDB for redis data persistence

Because save has almost been abandoned, redis automatically triggers using bgsave operation, so here we only introduce the process of bgsave.

  1. When executing bgsave, if there is currently a child process, redis will exit directly without performing the following operations. If not, continue execution.

  2. The redis main process will fork out a child process. Redis will be blocked during fork, but the time is very short.

  3. After the fork is successful, the redis main process continues to do what it should do.

  4. The subprocess generates a new RDB file and replaces the old rdb file.

  5. When the replacement operation is completed, the child process will notify the parent process, and the parent process will save the relevant information of the operation.

Application scenarios

RDB files are small in size and fast to generate and load, but the rdb persistence method cannot achieve this Real-time persistence, which can easily lead to data loss under abnormal circumstances. In addition, different versions of rdb files may be incompatible.

Through the above introduction, you can know that RDB files are very suitable for disaster recovery backup, such as generating RDB files every morning. In addition, if the data stored in redis is not too important, such as using redis for caching and losing some data has no impact, using RDB is usually a better way.

Let’s introduce a solution to a common problem. When the partition where redis data is stored is almost full, how to write data to another partition without stopping redis. We can use config set dir 'new partition directory' to modify the directory where the rdb file is stored. Then execute bgsave to generate a new RDB file in the new directory.

The above is the detailed content of RDB for redis data persistence. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template