Home > Database > Redis > How to persist redis

How to persist redis

(*-*)浩
Release: 2019-11-26 09:59:51
Original
4324 people have browsed it

Since Redis data is stored in memory, if persistence is not configured, all data will be lost after redis restarts. Therefore, you need to enable the persistence function of redis and save the data to the disk. After redis restarts, you can Recover data from disk.

How to persist redis

redis provides two ways for persistence, one is RDB persistence (the principle is to record Reids in the database in memory Regular dump to RDB persistence on disk), the other is AOF (append only file) persistence (the principle is to write the Reids operation log to the file in an appended manner).

RDB: RDB persistence is the process of generating a snapshot of the current process data and saving it to the hard disk. The process of triggering RDB persistence is divided into manual triggering and automatic triggering. (Recommended learning: Redis video tutorial)

Trigger mechanism

Manual triggering corresponds to the save and bgsave commands respectively

save command: Block the current Redis server until the RDB process is completed. For instances with relatively large memory, it will cause long-term blocking and is not recommended for use online.

DB saved on disk
Copy after login

bgseve command: The Redis process performs a fork operation to create a child process. The RDB persistence process is responsible for the child process and ends automatically after completion. Blocking only occurs in the fork phase and is generally very short-lived.

* Background saving started by pid 3151
* DB saved on disk
* RDB: 0 MB of memory used by copy-on-write
* Background saving terminated with success
Copy after login

Automatic trigger

It will be triggered in the following scenarios

1) Use save related configuration , such as "save m n". Indicates that bgsave is automatically triggered when the data set is modified n times within m seconds.

2) If the slave node performs a full copy operation, the master node automatically executes bgsave to generate an RDB file and sends it to the slave node.

3) When executing the debug reload command to reload Redis, the save operation will also be automatically triggered.

4) By default, when executing the shutdown command, if the AOF persistence function is not enabled, bgsave will be automatically executed.

AOF: Record each written command in an independent log, and re-execute the command in the AOF file during restart to restore data. Main function: Solve the problem of real-time data persistence.

Using AOF

To enable the AOF function, you need to set the configuration: appendonly yes, which is not enabled by default. The file name is set through the appendfilename configuration, and the default is appendonly.aof.

How to persist redis

#1) All write commands will be appended to aof_buf (buffer).

2) The AOF buffer performs synchronization operations to the hard disk according to the corresponding policy.

3) As AOF files become larger and larger, AOF files need to be rewritten regularly to achieve compression.

4) When the Redis server is restarted, the AOF file can be loaded for data recovery.

For more Redis-related technical articles, please visit the Redis Getting Started Tutorial column to learn!

The above is the detailed content of How to persist redis. 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