Redis persistence [Switch]

Source: Internet
Author: User

Redis is an advanced key-value database. It is similar to memcached, but data can be persistent and supports a wide range of data types. There are string, linked list, set and ordered set. Allows you to compute, merge, and merge sets (difference) on the server side. It also supports multiple sorting functions. So redis can also be seen as a data structure server.
All redis data is stored in the memory and then asynchronously stored to the disk (this is called the "semi-persistent mode "); you can also write every data change to an append only file (AOF) (this is called "Full persistence mode ").

Method 1Filesnapshotting: By default, redis will persist data to the disk in the form of snapshots (a binary file, dump. RDB, the file name can be specified). The format in the configuration file is: Save n m, which indicates that within n seconds, redis will take snapshots to the disk if at least m changes occur in redis. Of course, you can also manually save or bgsave (asynchronous) to take snapshots.

A Brief Introduction to the working principle: When redis requires persistence, redis will fork a sub-process; the sub-process will write data to a temporary RDB file on the disk; after a child process writes a temporary file, replace the original RDB. This is advantageous becauseCopy-on-write

Another persistence method isAppend-only: When the filesnapshoshomethod is abnormal in redis, the recent data will be lost (the amount of lost data depends on the configuration of your save policy), so this is its biggest drawback. When the business volume is very large, there are a lot of lost data. The append-only method can ensure that all data is not lost, but the performance of redis is worse. Aof can achieve full persistence. You only need to enable it in the configuration file (No by default). After appendonly Yes enables aof, Every time redis executes a command to modify data, it will be added to the aof file. When redis is restarted, it will read the aof file for "replay" to restore to the last moment before redis is closed.

Log rewriting will increase with the execution of aof files for data modification, many of which record the changes of a key. Therefore, redis has an interesting feature: rebuild the aof file in the background without affecting client operations. Execute at any timeBgrewriteaofCommands write the most short sequence of commands in the current memory to the disk. These commands can fully construct the current data, without any additional changes (such as status changes, the size of the aof file.Therefore, when using aof, redis recommends that you use bgrewriteaof at the same time..

There are three methods to refresh aof files. Refer to the configuration parameters.Appendfsync:Appendfsync alwaysEvery time a modification command is submitted, fsync is called to refresh the aof file, which is very slow but safe;Appendfsync everysecEvery second, fsync is called to refresh to the aof file, which is very fast, but data within one second may be lost;Appendfsync NoRefresh by OS, redis does not actively refresh aof, this is the fastest, but the security is poor. By default, refresh is recommended per second, so both speed and security are ensured.

The aof file may be damaged due to system reasons, and redis cannot load this aof file. You can follow the steps below to fix it: first, back up an aof file and copy it to other places; fix the original aof file, run:$ Redis-check-Aof-fix; you can use the diff-u command to view the locations where the files are inconsistent before and after the repair. Restart the redis service.

How log rewrite works: Copy-on-write is also used: redis will first fork a sub-process; the sub-process will write the latest aof into a temporary file; the parent process incrementally writes the latest modifications in the memory (the old aof is still written, and the rewrite is safe if the rewrite fails). After the child process completes the temporary rewrite file, the parent process receives a signal and writes the incremental changes in the memory to the end of the temporary file. In this case, redis renames the old aof file, the temporary file, and starts writing to the new aof file.

Finally, in case (the machine breaks down or the disk breaks down), remember to regularly back up the * RDB *. aof file generated using filesnapsholoud or append-only to a remote machine. I use crontab for SCP every half an hour. I didn't use the redis Master/Slave function, because it should be enough to back up data once in half an hour, and I think it would be a waste of machines to make a decision. It depends on the application.

Redis persistence [Switch]

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.