Home > Database > Redis > body text

What are the persistence methods of Redis?

WBOY
Release: 2023-05-27 08:02:59
forward
2732 people have browsed it

Why is persistence needed?

Normally all redis data is stored in memory. Once the database fails and is restarted, all data will be lost, even in redis cluster or redis The recovery of master-slave synchronization data in sentinel mode still takes some time.

What are the persistence methods of Redis?

The persistence function can effectively avoid data loss caused by process exit. Data recovery can be achieved by using the previously persisted files during the next restart.

After using Redis persistence, the data will be stored on the disk. When performing incremental synchronization of the database, the time required is much less than that of performing full synchronization. Data recovery from failures plays a very important role in a production environment!

There are two options for Redis data persistence

There are two options for Redis persistence:

  • RDB is a snapshot data storage that periodically saves all Redis data at the current point in time to disk.

  • AOF is an append-based storage method that records Redis write operations to the disk in real time.

What are the differences between these two solutions? Let me explain them one by one~

1. RDB persistence

When the writing of Redis triggers the RDB persistence condition (it can also be triggered by manually executing the dgsave command), the Redis main process forks a child process to create a temporary RDB storage file. After the file creation is completed, the temporary File rename replaces the original RDB file. The RDB file is a single file that is very suitable for disaster recovery backup and recovery of data. Restoring the database through the RDB file takes less time. Usually, it only takes about 20 seconds to load a 1G snapshot file into the memory.

What are the persistence methods of Redis?

Disadvantages:

  • RDB persistence will only save Redis data periodically, when the next storage has not been triggered yet If Redis crashes, all data in the memory will be lost.

  • In addition, when the amount of data is large, the operation of forking the sub-process consumes a lot of CPU. As shown in the monitoring chart below, the RDB persistence triggered every 1800s will consume a lot of CPU for Redis. Soaring. Second-long blocking may occur during the fork child process.

What are the persistence methods of Redis?

Parameters:

What are the persistence methods of Redis?

save option If configured as empty save "", it will be closed RDB persistence. You can configure multiple trigger conditions for turning on RDB persistence. For example, 1 write triggers a snapshot within 900 seconds/10 writes triggers a snapshot within 300 seconds. This can be freely configured according to your own Redis writing conditions. Balance performance with data security.

It is recommended to enable stop-writes-on-bgsave-error. When an error occurs in redis bgsave, the client's request will be rejected. Bgsave failure is usually caused by insufficient disk or memory space, and monitoring is required to improve data security.

2. AOF persistence

AOF achieves persistence by saving commands for Redis write operations. Using AOF for persistence, the security of Redis data will be greatly improved. Improved, up to 1 second of data loss in case of abnormal downtime. The write operations of Redis are recorded in the AOF file. The format of the file is clear and easy to understand and can be easily modified for convenient data reconstruction.

What are the persistence methods of Redis?

Disadvantages:

  • As redis writes increase, the AOF storage file will become larger and larger, which will affect the database Data recovery time and disk space, etc., so we need to configure AOF rewriting to reduce the size of the AOF file. Here we can use the default two trigger condition configurations or we can manually call the BGREWRITEAOF command to trigger.

Parameters:

What are the persistence methods of Redis?

appendonly sets whether to enable AOF persistence.

appendfsync has three persistence modes: always/everysec/no, which takes into account the speed and security of data storage and is configured as everysec, which synchronizes data to the disk every second.

3. Comparison of the advantages and disadvantages of RDB and AOF persistence

Both methods have their own merits. Let’s compare the two redis data persistence methods:

What are the persistence methods of Redis?

4. When selecting

Redis will first check whether the AOF file exists, and if it does not exist, try to load the RDB file.

Under different circumstances, depending on the amount of data, application requirements for data security, budget constraints, etc., various persistence strategies will be used in actual production environments. This sentence can be rewritten as: You can choose not to use persistence, or you can choose to use separate RDB or AOF persistence, or you can enable RDB and AOF persistence at the same time.

PS: The choice of persistence must be considered together with the master-slave strategy of Redis, because master-slave replication and persistence also have the function of data backup, and the host master and slave slave can independently choose the persistence solution.

The above is the detailed content of What are the persistence methods of Redis?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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