Redis Tutorial (10): Persistence

Source: Internet
Author: User
Tags redis server redis tutorial
I. What persistence mechanisms are provided by Redis:

1). RDB Persistence:
The mechanism is to write a snapshot of the in-memory data set to disk within a specified interval of time.
2). AoF Persistence:
This mechanism will record every write processed by the server as a log, and read the file in the early of the Redis server to rebuild the database to ensure that the data in the database is complete after startup.
3). No persistence:
We can disable the persistence of the Redis server by configuring it so that we can treat Redis as a feature-enhanced version of memcached.
4). Apply both AoF and RDB.

Ii. advantages and disadvantages of the RDB mechanism:

What are the advantages of an RDB?

1). Once this is used, your entire Redis database will contain only one file, which is perfect for file backups. For example, you might want to archive the last 24 hours of data every hour and archive the last 30 days of data every day. With such a backup strategy, we can recover very easily once the system has a catastrophic failure.
2). The RDB is a great choice for disaster recovery. Because we can easily compress a single file and then transfer it to other storage media.
3). Maximum performance. For a Redis service process, the only thing it needs to do when it starts to persist is to fork out the child process and then complete the persistence work by sub-processes, which can greatly prevent the service process from performing IO operations.
4). If the data set is large, the RDB will be more efficient to start than the AOF mechanism.

What are some of the disadvantages of an RDB?

1). An RDB is not a good choice if you want to ensure high availability of data, which minimizes data loss. Since the system is down before the scheduled persistence, the data that has not been written to the disk before is lost.
2). Since the RDB assists with data persistence through the fork process, it can cause the entire server to stop serving hundreds of milliseconds, or even 1 seconds, if the data set is large.

Third, the advantages and disadvantages of the AOF mechanism:

What are the advantages of aof?

1). This mechanism can result in higher data security, i.e. data persistence. There are 3 synchronization policies available in Redis, that is, synchronization per second, synchronization per modification, and unsynchronized. In fact, synchronization per second is also done asynchronously, and its efficiency is very high, the difference is that once the system is down, then the data will be lost in a second. Each time we modify the synchronization, we can treat it as a synchronous persistence, that is, each occurrence of the data changes will be immediately logged to disk. It can be predicted that this is the least efficient way. As for the no-sync, needless to say, I think we can understand it correctly.
2). Because this mechanism writes to log files in append mode, it does not break the content that already exists in the log file even if there is an outage during the write process. However, if we only write half of the data this time there is a system crash problem, do not worry, before the next boot of Redis, we can use the Redis-check-aof tool to help us solve the problem of data consistency.
3). If the log is too large, Redis can automatically enable the rewrite mechanism. That is, Redis continuously writes the modified 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, the data security can be better ensured when the rewrite is switched.
4). AOF contains a well-formed, easy-to-understand log file for recording all modification operations. In fact, we can also complete the reconstruction of the data through this file.

What are the disadvantages of aof?
1). aof files are typically larger than the Rdb file for the same number of datasets.
2). Depending on the synchronization strategy, the AOF is often slower than the RDB in terms of operational efficiency. In summary, the efficiency of the synchronization policy per second is high, and the efficiency of the synchronization disable policy is as efficient as the RDB.

Iv. Other:

1. Snapshotting:

By default, Redis dumps a snapshot of the dataset to the Dump.rdb file. In addition, we can modify the frequency of the Redis server dump snapshot through the configuration file, after we open the 6379.conf file, we search for save, we can see the following configuration information:
Save 1 #在900秒 (15 minutes), dump memory Snapshot if at least 1 key changes occur.
After save #在300秒 (5 minutes), dump memory Snapshot if at least 10 key changes occur.
After save 10000 #在60秒 (1 minutes), dump memory Snapshot if at least 10,000 key changes occur.

2. Dump snapshot mechanism:

1). Redis first fork the child process.
2). The child process writes the snapshot data to the temporary RDB file.
3). After the child process completes the data write operation, replace the old file with the temporary file.

3. aof File:

It has been said many times that the RDB snapshot timing dump mechanism does not guarantee good data persistence. If our application is really concerned about this point, we can consider using the AOF mechanism in Redis. The default mechanism for Redis servers is the RDB, and if you need to use AOF, you need to modify the following entries in the configuration file:
Change AppendOnly No to appendonly Yes
From now on, Redis appends the data modification commands to the AoF file every time it receives a command. The next time Redis restarts, the information in the AoF file needs to be loaded to build the most up-to-date data into memory.

4. Configuration of AOF:

There are three synchronization modes in the Redis configuration file, namely:
Appendfsync always #每次有数据修改发生时都会写入AOF文件.
Appendfsync everysec #每秒钟同步一次, this policy is the default policy for AOF.
Appendfsync no #从不同步. Efficient but data is not persisted.

5. How to repair aof files for bad damage:

1). Make an additional copy of the existing AoF file that has been damaged.
2). Execute the "redis-check-aof--fix <filename>" command to repair the damaged AoF file.
3). Restart the Redis server with the repaired aof file.

6. Redis Data Backup:

In Redis, we can back up the running Redis data files online by copy. This is because the Rdb file will no longer be modified once it is generated. Each time Redis dumps the latest data into a temporary file, it then renames the temporary file to its original data file name using the Rename function atomicity. So we can say that copy data files are safe and consistent at any time. For this reason, we can periodically back up Redis data files by creating cron jobs and copy the backup files to secure disk media.

The above is the Redis tutorial (10): Persistent detailed content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.