Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of Redis persistence

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points of Redis persistence method, which are detailed in content and clear in logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Why do you need persistence?

Usually, all the data of redis is stored in memory, and all data will be lost when the database is restarted in the event of a failure. Even in redis cluster or redis sentinel mode, it still takes some time to recover the master-slave synchronization data.

The persistence function is that it can effectively avoid the problem of data loss caused by the exit of the process, and the data recovery can be achieved by using the previously persisted files in the next restart.

When Redis persistence is enabled, the data will be stored to disk, and the database will take much less time to perform incremental synchronization than full synchronization. Fault data recovery plays a very important role in the production environment.

There are two schemes for Redis data persistence

There are two scenarios for Redis persistence:

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

AOF is an add-on storage method that records Redis writes to disk in real time.

What is the difference between the two schemes? Let's let the editor come one by one.

1. RDB persistence

When the write of Redis triggers the RDB persistence condition (it can also be triggered by manually executing the dgsave command), the Redis main process fork a child process to create a temporary RDB storage file, and rename replaces the original RDB file to the temporary file after the file is created. RDB file is a single file which is very suitable for disaster recovery backup and recovery of data. it takes a short time to restore database through RDB file. Usually, it only takes about 20 seconds for 1G snapshot file to be loaded into memory.

Disadvantages:

RDB persistence will only save Redis data periodically. When Redis goes down before the next storage is triggered, all data in memory will be lost.

In addition, when the amount of data is large, the operation of the cpu child process consumes a lot of cpu. The monitoring chart below shows that every 1800s triggered RDB persistence, the cpu consumption of the Redis will soar. Blocking conditions up to a second can occur during the fork child process.

Parameters:

If the save option is configured with empty save "", RDB persistence is disabled. The trigger condition for enabling RDB persistence can be configured. For example, one write trigger snapshot within 900s / 10 write trigger snapshots within 300s can be configured freely to balance performance and data security according to the Redis write situation.

It is recommended to enable stop-writes-on-bgsave-error to reject the client's request when an error occurs in redis bgsave. The failure of bgsave is usually due to insufficient disk or memory space, which needs to be monitored to improve data security.

2. AOF persistence

AOF achieves persistence by saving commands for Redis write operations. Using AOF to persist, the security of Redis data will be greatly improved. In the case of abnormal downtime, a maximum of 1 second of data will be lost. AOF file records the write operation of redis, which is clear in format, easy to understand and modify, and conducive to data reconstruction.

Disadvantages:

With the increase of redis writes, AOF storage files will become larger and larger, which will affect the recovery time and disk space of database data, so we need to configure AOF rewriting to reduce the size of AOF files. Here we can use the default two trigger conditions configuration or we can manually call the BGREWRITEAOF command to trigger.

Parameters:

Appendonly sets whether AOF persistence is enabled.

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

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

The two methods have their own advantages. Let's compare the two methods of redis data persistence:

4. Choice

When Redis recovers data, it first checks whether the AOF file exists, and attempts to load the RDB file if it does not exist.

In the actual production environment, there will be a variety of persistence strategies according to the amount of data, application security requirements for data, budget constraints and other conditions. Such as not using any persistence at all, using one of RDB or AOF, or turning on RDB and AOF persistence at the same time.

PS: the choice of persistence must be considered 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 scheme.

These are all the contents of the article "what are the methods of Redis persistence?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report