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

How to persist redis

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

Share

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

The editor will share with you how to persist redis. I hope you will get something after reading this article. Let's discuss it together.

1. Introduction to redis

REmote DIctionary Server (Redis) is a key-value storage system written by Salvatore Sanfilippo.

Redis is an open source log database written in ANSI C language, complies with BSD protocol, supports network, can be memory-based and persistent, Key-Value database, and provides API in multiple languages.

It is often called a data structure server because the value can be of types such as String, Hash, list, sets, and sorted sets.

It is similar to memcached, except that data can be persisted and supports a wide range of data types. There are strings, linked lists, sets and ordered collections. Support in the server-side calculation of the collection union, intersection and complement set (difference), etc., but also supports a variety of sorting functions. So Redis can also be seen as a data structure server.

All data in Redis is stored in memory and then saved to disk asynchronously from time to time (this is called "semi-persistent mode"); each data change can also be written to an append only file (aof) (this is called "full persistence mode").

Since the data of Redis is stored in memory, if persistence is not configured, all data will be lost after redis restart, so you need to enable the persistence feature of redis to save the data to disk. When redis restarts, you can recover the data from disk. Redis provides two ways for persistence, one is RDB persistence (the principle is to dump the database records of Reids in memory regularly to RDB persistence on disk), and the other is AOF (append only file) persistence (the principle is to write the operation log of Reids to a file in an appended way). So what's the difference between these two persistence methods, and how to choose them? Most of what I read on the Internet are about how to configure and how to use the two methods, but they do not introduce the difference between them and what application scenarios they are used in.

2. The difference between the two

RDB persistence means that the snapshot of the dataset in memory is written to disk within a specified time interval. The actual operation is a child process of fork, which first writes the dataset to a temporary file, and then replaces the previous file and stores it with binary compression.

3. Advantages and disadvantages of both.

What are the advantages of RDB?

1)。 Once you use this approach, your entire Redis database will contain only one file, which is perfect for file backup. For example, you might plan to archive data for the last 24 hours every hour, as well as data for the last 30 days once a day. Through this backup strategy, in the event of a catastrophic failure of the system, we can recover very easily.

2)。 RDB is a very good choice for disaster recovery. Because we can easily compress a single file and then transfer it to other storage media.

3)。 Maximize performance. For the Redis service process, at the beginning of persistence, the only thing it needs to do is to fork the child process, and then the child process completes the persistence work, which can greatly prevent the service process from performing IO operations.

4)。 Compared with the AOF mechanism, if the dataset is large, the RDB startup efficiency will be higher.

What are the disadvantages of RDB?

1)。 If you want to ensure high availability of data, that is, to avoid data loss as much as possible, then RDB will not be a good choice. Because once the system goes down before timing persistence, data that has not been written to disk before will be lost.

2)。 Because RDB assists in data persistence through the help child process, if the dataset is large, it may cause the entire server to go out of service for hundreds of milliseconds, or even 1 second.

What are the advantages of AOF?

1)。 This mechanism can bring higher data security, that is, data persistence. Three synchronization strategies are provided in Redis, that is, synchronization per second, synchronization per modification, and async. In fact, synchronization per second is also done asynchronously, and its efficiency is also very high. The only difference is that once the system downtime occurs, then the modified data within this second will be lost. Every time we modify synchronization, we can think of it as synchronous persistence, that is, every data change that occurs is immediately recorded to disk. It can be predicted that this approach is the least efficient. As for non-synchronization, needless to say, I think everyone can understand it correctly.

2)。 Because this mechanism uses append mode to write log files, even if there is a downtime in the writing process, it will not destroy the content that already exists in the log file. However, if we only write half the data this time and the system crashes, don't worry, we can use the redis-check-aof tool to help us solve the data consistency problem before the next startup of Redis.

3)。 If the log is too large, Redis can automatically enable the rewrite mechanism. That is, Redis constantly writes the modified data to the old disk file in append mode, and Redis also creates a new file to record which modification commands were executed during this period. Therefore, data security can be better guaranteed when rewrite switching is carried out.

4)。 AOF contains a clearly formatted, easy-to-understand log file for recording all changes. In fact, we can also complete the reconstruction of the data through this file.

What are the disadvantages of AOF?

1)。 For the same number of datasets, AOF files are usually larger than RDB files. RDB recovers large datasets faster than AOF does.

2)。 Depending on the synchronization strategy, AOF tends to be slower than RDB in terms of efficiency. In short, the efficiency of synchronization per second policy is relatively high, and the efficiency of synchronization disable policy is as efficient as that of RDB.

The standard of choice between the two is to see whether the system is willing to sacrifice some performance for higher cache consistency (aof), or is willing to write frequently, do not enable backup in exchange for higher performance, and then do backup (rdb) when save is run manually. Rdb is even more eventually consistent.

4. Common configuration

RDB persistent configuration

Redis dump a snapshot of the dataset to the dump.rdb file. In addition, we can also modify the frequency of dump snapshots of the Redis server through the configuration file. After opening the 6379.conf file, we search save and see the following configuration information:

Save 9001 # after 900s (15 minutes), if at least one key changes, the dump memory snapshot.

Save 30010 # after 300 seconds (5 minutes), if at least 10 key changes, the dump memory snapshot.

Save 60 10000 # after 60 seconds (1 minute), if at least 10000 key changes, the dump memory snapshot.

AOF persistent configuration

There are three synchronization methods in the configuration file of Redis, which are:

Appendfsync always # is written to the AOF file every time a data modification occurs.

Appendfsync everysec # synchronizes once per second, which is the default policy for AOF.

Appendfsync no # is never synchronized. Efficient but the data is not persisted.

After reading this article, I believe you have a certain understanding of "how to persist redis". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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