Redis learning Manual (persistence)

Source: Internet
Author: User
Tags redis server

I. What persistence mechanisms does redis provide:

1). RDB Persistence:
This mechanism writes data set snapshots in the memory to the disk within a specified interval.
2). aof Persistence:
This mechanism records every write operation processed by the server in the form of logs. When the redis server is started, it reads the file to re-build the database, to ensure that the data in the database is complete after startup.
3). No Persistence:
We can disable the persistence function of the redis server through configuration, so that we can regard redis as a function enhanced version of memcached..
4) apply both aof and RDB.

Ii. Advantages and disadvantages of the RDB mechanism:

what are the advantages of RDB?
1). Once this method is used, your entire redis database will only contain one file, which is perfect for file backup. For example, you may plan to archive data for the last 24 hours every hour, and archive data for the last 30 days every day. With this backup policy, once the system encounters a catastrophic fault, we can easily recover it.
2). RDB is a good choice for disaster recovery. Because we can easily compress a single file and transfer it to other storage media.
3). maximize performance. For redis service processes, the only thing it needs to do at the beginning of persistence is to Fork sub-processes, and then the sub-processes will complete the persistence work, in this way, the service process can be greatly prevented from performing Io operations.
4). Compared with the aof mechanism, RDB can be started more efficiently if the dataset is large.
what are the disadvantages of RDB?
1). If you want to ensure high data availability, that is, to avoid data loss to the maximum extent, RDB is not a good choice. Once the system goes down before scheduled persistence, data that has not been written to the disk will be lost.
2 ). because RDB uses the Fork sub-process to help complete data persistence, when the dataset is large, it may cause the entire server to stop service for several hundred milliseconds or even one second.
3. Advantages and disadvantages of the aof mechanism:

What are the advantages of aof?
1). This mechanism can bring higher data security, that is, data persistence. Redis provides three synchronization policies, namely, synchronization per second, synchronization per modification, and non-synchronization. In fact, synchronization is done asynchronously every second, and the efficiency is very high. The difference is that once the system goes down, the data modified within one second will be lost. For each modification and synchronization, we can regard it as synchronization persistence, that is, each data change will be immediately recorded in the disk. It is foreseeable that this method is the least efficient. As for the absence of synchronization, there is no need to say anything. I think everyone can understand it correctly.
2) because this mechanism writes log files in the append mode, even if the log file goes down during the write process, the existing content in the log file will not be damaged. However, if we write only half of the data in this operation, the system crashes. Don't worry. Before redis starts up next time, we can use the redis-check-Aof tool to solve the data consistency problem.
3) if the log size is too large, redis can automatically enable the rewrite mechanism. That is, redis constantly writes the modification data to the old disk file in append mode, and redis also creates a new file to record which modification commands are executed during this period. Therefore, data security can be better ensured during rewrite switching.
4). aof contains a clear and easy-to-understand log file used to record all modification operations. In fact, we can use this file to rebuild the data.

What are the disadvantages of aof?
1). For the same number of datasets, The aof file is usually larger than the RDB file.
2). According to different synchronization policies, aof is often slower than RDB in terms of operation efficiency. In short, the efficiency of synchronization policies per second is relatively high, and the efficiency of synchronization policies is as efficient as that of RDB.

4. Others:

1. snapshotting:
By default, redis will dump the snapshot of the dataset to the dump. RDB file. In addition, we can also modify the dump snapshot frequency of the redis server through the configuration file. After opening the 6379. conf file, we search for save to see the following configuration information:
Save 900 1 # If at least one key changes after 900 seconds (15 minutes), dump the memory snapshot.
Save 300 10 # If at least 10 keys change after 300 seconds (5 minutes), dump the memory snapshot.
Save 60 10000 # If at least 10000 keys change after 60 seconds (1 minute), dump the memory snapshot.

2. Dump snapshot mechanism:
1). redis Fork sub-process first.
2) The sub-process writes the snapshot data to the temporary RDB file.
3) after the sub-process completes Data Writing, replace the old file with a temporary file.

3. aof file:
As mentioned above many times, the RDB snapshot timed dump mechanism cannot guarantee good data persistence. If our application is very concerned about this point, we can consider using the aof mechanism in redis. For the redis server, the default mechanism is RDB. To use aof, You need to modify the following entries in the configuration file:
Set Appendonly No Change Appendonly Yes
From now on, redis will append the data modification command to the aof file every time it receives it. During the next redis restart, You need to load the information in the aof file to build the latest data into the memory.

4. aof Configuration:
There are three Synchronization Methods in the redis configuration file:
Appendfsync always # The aof file is written every time a data modification occurs.
Appendfsync everysec# Synchronize data every second. This policy is aof's default policy.
Appendfsync No # Never synchronize. Efficient but data is not persistent.

5. How to fix the damage aof file:
1) copy an additional damaged aof file.
2). Execute the "redis-check-Aof -- fix <FILENAME>" command to fix the aof file of the damage.
3) use the repaired aof file to restart the redis server.

6. redis data backup:
In redis, we can back up running redis data files online through copy. This is because once the RDB file is generated, it will not be modified. Redis dumps the latest data to a temporary file every time, and then changes the temporary file to the original data file name atomically using the rename function. Therefore, we can say that copying data files is safe and consistent at any time. In view of this, we can create a cron job to regularly back up redis data files and copy the backup files to a secure disk medium.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.