Redis Persistent Configuration

Source: Internet
Author: User
Tags flush redis

It is stored in the cache for Redis, so the problem of cache data loss has always been a topic of considerable concern to programmers, so the need to persist the data in the cache is quite prominent, and the following are the related configurations for Redis persistence:


1 First type: RDB persistence mode
1.1 Overview

The default Redis is a snapshot of the data persisted to the disk (a binary file, Dump.rdb, this file name can be specified), the format in the configuration file is: Save N m means that within N seconds, redis occurs at least m modified Redis snapshot to disk. Of course we can also manually perform save or Bgsave (asynchronous) to take snapshots.

1.2 Implementation Mechanism

When Redis needs to be persisted, Redis fork a child process, and the child process writes the data to a temporary RDB file on disk, and when the child process finishes writing the temporary file, the original RDB is replaced, so the benefit is that it can be copy-on-write

1.3 Related Configurations

redis.conf configuration file:

1)

# save ""

Save 900 1

Save 300 10

Save 60 10000

2)

# The filename where to dump the DB

Dbfilename Dump.rdb

3)

# Note that you must specify a directoryhere, not a file name.

Dir./


2 Second type: aof persistence mode
2.1 Overview

There is also a persistence method is the Append-only:filesnapshotting method when Redis abnormal die, the recent data will be lost (how much data loss depends on the configuration of your save policy), so this is its biggest disadvantage, when the volume of business is very large, the loss of data is many. The Append-only method can do all the data without loss, but the performance of Redis will be worse. AOF will be able to achieve full-length persistence, only need to open in the configuration file (default is no), appendonly Yes on aof, Redis every time you execute a modified data command, will add it to the AoF file, when the Redis restart, will read the aof file to "replay "To revert to the last moment before Redis was closed.

Log rewriting as the execution of the modified data aof file becomes larger, many of which record the change of a key. So Redis has a more interesting feature: rebuilding the aof file in the background without affecting the client side operation. Executing the bgrewriteaof command at any time will write commands for the shortest sequence in the current memory to disk, which can completely build the current data situation without any extraneous changes (such as state changes, counter changes, etc.) and the size of the aof file. So when using AOF, Redis recommends using bgrewriteaof at the same time.

AoF file Refresh way, there are three kinds, reference configuration parameters Appendfsync:appendfsync always each commit a modification command is called Fsync flush to the aof file, very very slow, but also very safe ; Appendfsynceverysec calls Fsync flush to aof file every second, soon, but may lose less than one second of data, Appendfsync no depends on OS refresh, Redis does not actively refresh aof, so the fastest, but the security is poor. The default and recommended refresh per second, so that both speed and security are done.

AOF may be damaged due to system reasons, Redis can no longer load this aof, you may follow the steps below to fix: First to do a aof file backup, copy to another place; fix the original aof file, execute: $ redis-check-aof–fix; can be diff– u command to see where the files are inconsistent before and after the repair, and restart the Redis service.

2.2 Implementation Mechanism

1) Redis will take a snapshot of the database, traverse all the databases, restore the data in the database to the protocol format of the instructions sent by the client,

2) Then Redis creates a new temporary file to save the snapshot data, the temporary file name is modified to the normal AOF file name after the end of the snapshot program, and the original files are discarded automatically.

Since there may be new commands in the process of the snapshot that modify the data in the database, the newly modified data needs to be appended to the AoF file at the end of the snapshot program, and subsequent commands from the client continue to be written to the disk according to different security levels. This supports real-time persistence, but it is possible to have data loss for a short period of time, which can be tolerated for general systems.

2.3 Related Configurations

Redis 127.0.0.1:6380> config get*append*
1) "AppendOnly"
2) "Yes"
3) "No-appendfsync-on-rewrite"
4) "No"
5) "Appendfsync"
6) "Everysec"

Redis 127.0.0.1:6380> config get*aof*
1) "Auto-aof-rewrite-percentage"
2) "100"
3) "Auto-aof-rewrite-min-size"
4) "67108864"


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.