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 is Redis persistence

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what is Redis persistence". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

RDB: the snapshot form is to save the data in memory directly to a dump file, and to restore the snapshot file is to read the snapshot file directly into memory.

AOF: save all commands that make changes to Redis's server in a file, a collection of commands.

Redis is the persistence mode of snapshot RDB by default

RDB

There are two trigger modes for RDB, which are automatic trigger and manual trigger.

Automatic trigger

In the redis.conf configuration file, under SNAPSHOTTING, add the following configuration:

Save 9001: if there is a change in the value of at least one key in 900s, save save 30010: if there is a change in the value of at least 10 key in 300s, save save 60 10000: it means that if there is a change in the value of at least 10000 key in 60 seconds, save it

Of course, if you only use Redis's caching function without persistence, you can comment out all save lines to disable the save function, or you can use the `save "command

Manual trigger

There are two kinds of commands that manually trigger Redis for RDB persistence:

Save

This command blocks the current Redis server, and during the execution of the save command, Redis cannot process other commands until the RDB process is complete

Bgsave

When executing this command, Redis performs snapshot operations asynchronously in the background, and snapshots can also respond to client requests. The specific operation is that the Redis process executes the fork operation to create the child process, and the RDB persistence process is responsible for the child process, which ends automatically after completion. Blocking occurs only in the fork phase, usually for a short time.

Basically, all RDB operations within Redis use the bgsave command.

Recover data

Just move the backup file (dump.rdb) to the redis installation directory and start the service, and redis will automatically load the file data into memory. The Redis server will remain blocked during the loading of the RDB file until the loading is complete.

To get the installation directory of redis, you can use the config get dir command

Advantages and disadvantages of RDB

Advantages:

RDB is a very compact file that holds the dataset of redis at some point in time. This kind of file is very suitable for backup and disaster recovery

When the RDB file is generated, the fork main process will fork () a child process to handle all the save work, and the main process does not need to do any disk IO operation.

RDB is faster than AOF in recovering large datasets.

Disadvantages:

There is no way to achieve real-time persistence / second persistence of RDB data. Because every time bgsave runs, it has to perform the fork operation to create a child process, which is a heavyweight operation (a copy of the data in memory is cloned, approximately 2 times the expansibility needs to be considered), and the cost of frequent execution is too high (affecting performance).

RDB files are saved in a specific binary format. During the evolution of Redis versions, there are multiple RDB versions of formats. There is a problem that the old version of Redis service is not compatible with the new version of RDB format (the version is not compatible).

Make a backup at regular intervals, so if redis accidentally down, you will lose all changes since the last snapshot (data is lost)

AOF

AOF configuration

In the redis.conf configuration file, under APPEND ONLY MODE:

Default configuration of appendonly no, which means that AOF persistence is not enabled

Appendfilename "appendonly.aof" AOF log file name

Appendfsync: configuration of aof persistence policy

No means that the fsync is not executed, and the operating system ensures that the data is synchronized to disk, which is the fastest, but not very secure.

Always means that fsync is executed every time a write is written to ensure that the data is synchronized to disk, which is very inefficient

Everysec indicates that fsync is executed every second, which may result in the loss of the 1s of data. Everysec is usually chosen, taking into account both security and efficiency

AOF file recovery

After restarting Redis, the AOF file will be loaded.

AOF rewriting

The AOF file is getting bigger and bigger. In order to solve this situation, Redis rewrites the AOF file after the file size reaches a certain threshold, compresses the AOF file, and retains only the minimum instruction set that can recover the data.

For example:

Sadd key "A"B"C"

If you do not rewrite, you will retain three sadd instructions, but rewriting will retain only one.

When performing an AOF rewrite, all key-value pairs in the server memory are read directly, instead of sorting out the previous AOF file

During the AOF rewrite of the child process, the server process (parent process) can continue to process other commands

The child process has a copy of the data of the parent process, and using the child process instead of the thread can ensure the security of the data without using locks.

Data inconsistencies may occur before the main process and child processes, the solution:

The Redis server sets an AOF rewrite buffer that is used after the child process is created, and when the Redis server executes a write command, the write command is also sent to the AOF rewrite buffer. When the child process completes the AOF rewrite, it sends a signal to the parent process. After the parent process receives this signal, it calls a function to write the contents of the AOF rewrite buffer to the new AOF file.

Advantages and disadvantages of AOF

Advantages:

The method of AOF persistence provides a variety of synchronization frequencies, and even if you use the default synchronization frequency to synchronize once per second, Redis will only lose data for a second at most.

AOF files are constructed in the form of Redis command appends, so even if Redis can only write command fragments to the AOF file, it is easy to modify the AOF file using the redis-check-aof tool

The format of AOF file is readable, which also provides users with a more flexible way to deal with it. For example, if we accidentally misuse the FLUSHALL command, we can manually remove the last FLUSHALL command before the rewrite, and then use AOF to recover the data.

Disadvantages:

Redis,AOF files with the same data are usually larger than RDF files.

When the load of Redis is high, RDB has better performance guarantee than AOF.

There may be bug in AOF

That's all for "what is Redis persistence"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report