Redis Master-slave replication

Source: Internet
Author: User

Redis 's master-slave replication is very powerful, a master can have multiple slave, and a slave can have multiple slave, so go on, Form a powerful multi-level server cluster architecture .

The implementation steps are as follows :

1. Create two directories on a Windows disk , for example ; Masterredis (The Master service is stored ) Slaveredis ( the Slave service is stored ). 2.the configuration file in the Master service redis.conf modified : Bind 127.0.0.1. 3. configuration files in the Slave service redis.conf modified:

Port 6381(service port number to separate)

Bind 127.0.0.1

slaveof 127.0.0.1 6379 (set Host for master and Port)

4.start the Master service and the Slave service separately .

Notice that when I start Master andthen start a slave , you can find slave on:

A SYNC request is sent , corresponding from Master , and it supports automatic re-connection,

That is , when Master is dropped, it is in the state of waiting for the request.

and on Master:

First timeSlaveToMasterThe implementation of synchronization is:SlaveToMasterMake a synchronization request,MasterFirstDumpOutRdbFile, and then theRdbThe full amount of the file is transmitted toSlaveAnd thenMasterForward the cached command to theSlave, the initial synchronization is complete. The second and subsequent synchronization implementations are:MasterThe snapshot of the variable is sent directly to eachslave and master The above process is repeated when you disconnect. slave * claims that master-slave replication is non-blocking, but because redis use a single-threaded service if mastermaster

Redis Data Snapshot

The principle of a data snapshot is that all data in the entire Redis memory is traversed and stored in a data file with an RDB extension , with the save command

This procedure can be called. The data snapshot configuration is as follows :

Save 900 1

Save 300 10

Save 60 10000

Above in redis.conf" shows how long it takes How many times the update operation, the data is synchronized to the data file, this can be combined with multiple conditions, the above meaning is 900 key save,300 seconds later 10 keysave,60 seconds have 10000 key save.

The disadvantage of a data snapshot is that after persistence, if a system outage occurs, a piece of data is lost, thus adding an additional type of operation logging, called Append only file , whose log file ends with AoF , which we call aof File, to enable logging of aof logs, you need to configure the configuration file as follows : appendonly Yes

AppendOnly configuration does not turn on and may cause data loss for a period of time when a power outage occurs because the Redis itself synchronizes the data files by the save condition, so some data will only exist in memory for a period of time.

Appendfsync no/always/everysec

No: indicates that the data cache of the operating system is synchronized to disk. Performance is best and persistence is not guaranteed.

Always: represents a manual call to Fsync () to write data to disk after each update operation . Each time a write command is received, it is forced to write to disk immediately, the slowest, but guaranteed full persistence.

Everysec: indicates synchronization once per second . Force writes to disk once per second, making a good tradeoff between performance and persistence.

In order to reduce timingAOFThe size of the file,Redis2.4Added automatic in the future.BgrewriteaofThe function,RedisChooses a self-perceived low-load scenario to executeBgrewriteaof,This rewriteAOFThe process of a file is very performance-impacting. Solution:MasterShut down function, close aof log function to achieve the best performance. open saveaof log function, and turn on bgrewriteaofslave The load is generally higher than master load, but master

Bgrewriterof Internal implementation:

1.Redis iterates through the data, writes a new temporary file by fork a child process

2. The parent process continues to process the client request, and the child process continues to write the temporary file.

3. The parent process writes the newly written AOF in the buffer.

4. when the child process finishes writing out, the parent process receives the exit message and writes the buffer AOF to the temporary file.

5. The temporary file is renamed to Appendonly.aof, the original file is overwritten and the entire process is completed.

Redis Data Recovery

When the Redis server is hung up, the data is restored to memory at the following priority level when restarted:

1. If only AOF is configured, the AOF file recovery data is loaded when the reboot is restarted.

2. if RBD and AOF are configured at the same time , only the AOF file recovery data is loaded at startup.

3. If only an RDB is configured , the dump file recovery data will be loaded at startup.

Redis Master-slave replication

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.