Home >Database >Redis >How does redis persist data?

How does redis persist data?

(*-*)浩
(*-*)浩Original
2019-11-21 10:03:122898browse

Redis is called an in-memory database because it stores all its data in memory, so Redis has strong speed performance. However, precisely because the data is stored in memory, when Redis is restarted, all Data stored in memory will be lost. In order to make data persistent, Redis provides two methods: RDB method and AOF method.

How does redis persist data?

1. RDB method

RDB method persistence is completed through snapshotting. Under certain conditions, Redis will automatically generate a copy of all the data in the memory and store it on the hard disk. This process is called a "snapshot". "Snapshot" is similar to taking a photo. The moment you press the shutter, the photo you freeze is called a "snapshot".

# Redis There are 4 cases of snapshots on the data: (Recommended Learning: Redis Video Tutorial )

## automatically;


When the user executes the SAVE or BGSAVE command;

executes the FLUSHALL command;

when executing replication.

2. AOF method

Persistence is achieved through RDB. Once Redis exits abnormally, all data changed after the last snapshot will be lost. In order to reduce the risk of data loss caused by process termination, the AOF method can be used to achieve data persistence.


AOF persistence records every write and delete operation processed by the server in the form of a log. Query operations will not be recorded, but will be recorded in the form of text. Details can be seen in the file. operation records.

Her appearance is to make up for the shortcomings of RDB (data inconsistency), so it uses the form of a log to record each write operation and append it to the file. When Redis is restarted, the write instructions will be executed from front to back based on the contents of the log file to complete the data recovery work.

By default, Redis does not enable AOF persistence. It can be started through the appendonly parameter: appendonly yes


After AOF persistence is enabled, each execution will change Redis. Data command, Redis will write the command to an AOF format file on the hard disk, that is, the .aof format file stores some redis instructions.

It should be noted at this time that when data in Redis is frequently manipulated, the memory occupied by the AOF format file will become larger and larger, and there will be many useless instructions, such as continuously executing set foo 1, set foo 2, set foo 3.

Then the first two instructions are meaningless. Redis is very powerful. It will automatically delete meaningless instructions. That is, whenever a certain condition is reached, Redis will automatically rewrite the AOF file, and this condition Can be set in the configuration file. The rewriting process is only related to memory data and has nothing to do with the previous AOF file. This is similar to RDB.


When synchronizing hard disk data, due to the cache mechanism of the operating system, the data is not actually written to the hard disk, but enters the system's hard disk cache. By default, the system will perform a synchronization operation every 30 seconds. During these 30 seconds, if the system exits abnormally, the data in the hard disk memory will be lost.

Redis allows RDB and AOF modes to be turned on at the same time, which not only ensures data security, but also makes backup operations very easy. AOF can effectively reduce the possibility of data loss, and RDB can make data backup easy to achieve.

The above is the detailed content of How does redis persist data?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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

Related articles

See more