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

Example Analysis of Redis persistence of memory Database

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly shows you the "memory database Redis persistence example analysis", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn the "memory database Redis persistence example analysis" this article.

Because Redis is an in-memory database, in order to prevent data loss due to system crash and other reasons, Redis provides two different persistence methods to store data on the hard disk. One method is RDB, which can write all the data that exists at a certain time to the hard disk, and the other is to append only the file (AOF), which will execute the write command. Write all the write commands executed to the hard disk.

Snapshot persistence

Redis can create snapshots to get a copy of the data in memory at a certain point in time. After creating a snapshot, users can back up the snapshot, copy it to another server to create a server copy with the same data, and leave the snapshot in place for use when restarting the server.

There are two commands that can be used to generate RDB files, one is SAVE, and the other is BGSAVE.

When only snapshot persistence is used to save data, if the system does crash, the user will lose all data that has changed since the last snapshot was generated. Therefore, snapshot persistence applies only to applications that do not cause problems even if part of the data is lost.

SAVE

Features: the SAVE command blocks the Redis server process until the RDB file is created, and the server cannot process any command requests during the server process blocking.

Cons: the server cannot accept other requests during persistence.

BGSAVE

Features: the BGSAVE command will derive a child process, and then the child process will be responsible for creating the RDB file, and the server process will continue to process command requests.

Cons: the time it takes to create a child process increases with the memory consumed by Redis.

AOF persistence

AOF persistence records changes in the data by writing the executed write commands to the end of the AOF file, so Redis can recover the dataset recorded in the AOF file by re-executing all the write commands contained in the AOF file from beginning to end.

When setting the synchronization frequency, there are three options:

Option synchronization frequency always every Redis write command is synchronously written to the hard disk, but doing so will occupy the memory owned by Redis and seriously reduce the speed of Redis. Everysec performs synchronization once per second, explicitly synchronizing multiple write commands to the hard disk no to let the operating system decide when to synchronize.

It is best to use everysec, which can not only avoid the performance impact caused by every write, but also avoid the possible loss of uncertain data caused by operating system crash. even if the system crashes, users will only lose data generated within a second at most. When the hard disk is busy performing write operations, Redis will gracefully slow down its speed in order to adapt to the maximum write speed of the hard disk.

Disadvantages: because Redis will constantly record the executed write commands in AOF files, as Redis continues to execute, the size of AOF files will continue to grow, and in extreme cases, AOF may even use up all the available space on the hard drive.

To address the above shortcomings, Redis provides the BGREWRITEAOF command, which rewrites the AOF file by removing redundant commands from the AOF file, making the AOF file as small as possible. Its principle is similar to the BGSAVE command, Redis will create a child process, and then the child process will be responsible for rewriting the AOF file, because the AOF file rewriting also needs the child process, so there are also performance problems and memory consumption problems caused by the snapshot persistence caused by the creation of the child process.

These are all the contents of the article "sample Analysis of Redis persistence in memory databases". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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

Database

Wechat

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

12
Report