[Redis Notes] Article 3rd: Persistence configuration in redis. conf

Source: Internet
Author: User
Redis supports two persistence policies: snapshot and commandlog. The former is implemented by periodically writing the current Memory Data snapshot into the RDB file; the latter is implemented by recording the write operations received by the Redis process in the log. When Redis restarts next time, the commandlog is played back to restore the data status. Depending on your needs, you can choose to completely disable persistence

Redis supports two persistence policies: snapshot and commandlog. The former is implemented by periodically writing the current Memory Data snapshot into the RDB file; the latter is implemented by recording the write operations received by the Redis process in the log. When Redis restarts next time, the commandlog is played back to restore the data status. Depending on your needs, you can choose to completely disable persistence

Redis supports two persistence policies: snapshot and commandlog. The former is implemented by periodically writing the current Memory Data snapshot into the RDB file; the latter is implemented by recording the write operations received by the Redis process in the log. When Redis restarts next time, the commandlog is played back to restore the data status.

Depending on your needs, you can choose to completely disable persistence, or enable RDB and AOF in the same Redis instance at the same time.

Note: If the deployment mode is master slave, it is best to stagger the persistence time of different instances! Avoid the persistence of the master and slaves in the background at the same time, which may reduce the system performance.

1. RDB Configuration

1) databases

Configure the number of db files. You can use select Specifies the db file for subsequent persistence for each connection. db 0 is used by default for new connections.

2) save

SNAPSHOTTING supports multiple save policies for persistence, and supports mixing. For example:

Save 900 1

Save 300 100

Save 60 10000

The effect of the above configuration is that snapshotting will be triggered when any of the three conditions is met:. at least one key in S has changed; B. at least 100 keys are changed within S; c. at least 10000 keys change within 60 s

When the save condition is triggered, Redis uses the fork sub-process to Implement Asynchronous dump disk in the background by the sub-process. According to the fork write-time replication policy, if many write requests occur during the persistence process, in the worst case, the memory required is twice the memory occupied by the current dataset.

NOTE 1: The three trigger conditions in the preceding configuration are actually step-by-step enhanced. The save policy is triggered when the conditions are met first.

NOTE 2: If the business does not require persistence or RDB persistence, you can comment out the save configuration item.
3) stop-writes-on-bgsave-error

Specifies the behavior of Redis when a dump disk error occurs in the background. The default value is yes, indicating that if a dump error occurs in the background, the RedisServer rejects new write requests. This method can be used to alert users, avoid Major Accidents Caused by no exceptions discovered by users.
4) rdbcompression

Whether the RDB file is compressed or not. If yes, it will consume a little CPU while compressing, but saves disk space.

5) rdbchecksum

Whether the RDB file requires CRC64 verification. If yes, the CRC64 is calculated after the RDB file is generated and the result is appended to the end of the file. Similarly, when Redis starts Load RDB, it will also calculate the CRC64 of the file and compare it with the calculation result of dump.

Benefits: strict RDB integrity and security

Cost: 10% of performance will be lost during dump or load. To maximize Redis performance, use no to disable this configuration item.

6) dbfilename

Specifies the RDB file name. The default value is dump. rdb.

7) dir

Specifies the path of the RDB file storage directory. If a multi-level path is included, the parent path must be mkdir in advance. Otherwise, the startup will fail.

2. AOF Configuration

By default, Redis stores data persistently as RDB files (unless you manually disable RDB persistence ). If the Redis process fails or the machine loses power, new data will be lost during the period from the last save completion time to the time of failure. The introduction of AOF can reduce the data loss to one second or one write command.

1) appendonly

Configure whether to enable AOF persistence. The default value is no.

2) appendfilename

Specifies the aof file name. The default value is appendonly. aof.

3) appendfsync

Configure the aof file synchronization mode. Redis supports the following three methods:

A. no => redis does not actively call fsync, And the OS will schedule disk flushing;

B. always => redis will actively call fsync to fl the disk for each write command;

C. everysec => call fsync to fl the disk once per second.

You can select an appropriate synchronization policy based on the data sensitivity of your business.

4) no-appendfsync-on-rewrite

Specifies whether to call fsync during the rewrite of the aof file in the background. The default value is no, indicating that fsync is to be called (whether or not a sub-process is flushing the disk in the background ). Note: Redis will have a large number of disk IO during RDB file writing or afo file rewriting in the background. In some linux systems, calling fsync may be blocked.

5) auto-aof-rewrite-percentage

Specify the condition for Redis to override the aof file. The default value is 100, indicating that when the current aof file size exceeds 100% of the size of the last afo file, this will trigger the background rewrite. If it is set to 0, automatic rewrite is disabled.

6) auto-aof-rewrite-min-size

Specifies the size of the aof file that triggers rewrite. If the aof file is smaller than this value, automatic rewrite is not triggered even if the Incremental Ratio of the current file reaches the configured value of auto-aof-rewrite-percentage. That is, when both configuration items are met, the rewrite will be triggered.

3. issues that need to be clarified

1) if both the RDB and AFO persistence modes are enabled, the AOF file will be loaded when the Redis Server is started to recreate the dataset, because AOF ensures that the data is relatively complete.

2) for details about the advantages and disadvantages of RDB and AOF and how to choose a suitable persistence policy, refer to here.

3) In short, if you can tolerate data loss, you can only enable RDB. If you are sensitive to data, you can enable both RDB and AOF; we do not recommend that you only enable AOF (annotate the save configuration item of the configuration file or run save "" Through redis-cli), because if the AOF file is damaged or the AOF parsing engine has a bug, the entire dataset cannot be rebuilt.

[References]

1. Redis Persistence

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.