Redis master-slave replication for building high-performance database caches

Source: Internet
Author: User

First,What isRedismaster-slave replication?

master-slave replication, when users go to When the Master side writes data, the data file is sent to Slave via the Redis Sync mechanism,and Slave performs the same operation to ensure the data is consistent; The master-slave replication is very simple.

Second,Redismaster-slave replication features

1, the same Master can have more than one slaves.

2,Master under the Slave can also accept the same schema other Slave link and synchronization requests, to achieve cascade replication of data, that is master->slave-> Slave mode;

3. Master synchronizes data to slave in a non-blocking manner , which will mean that master will continue to process one or more slave read and write requests;

4,Slave -side synchronization data can also be modified to non-blocking is the way, when the Slave in performing a new synchronization, it can still use the old data information to provide queries; otherwise, when Slave and when Master loses contact,slave returns an error to the client;

5, master-slave replication has scalability, that is, multiple slave dedicated to provide read-only query and data redundancy,Master dedicated to provide write operations;

6, through the configuration disables the master data persistence mechanism, the data persistence operation to the slaves completes, avoids in the master to have the independent process to complete this operation.

Third,RedisMaster-slave replication principle

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/38/6A/wKiom1Ozz5OThc6NAAGUIzDDlQs366.jpg "title=" 13.png "alt=" Wkiom1ozz5othc6naaguizddlqs366.jpg "/>when you start aSlaveprocess, it willMasterSend aSYNC Command, request a synchronous connection. Whether it's the first connection or the reconnection,Masterstarts a background process, saves a snapshot of the data to the data file, andMasterAll commands that modify the data are logged and cached in the data file. After the background process completes the cache operation,Mastersend a data file toSlave,SlaveSave the data file to your hard disk, then load it into memory, and thenMasterall actions that modify the data are sent toSlaveend. IfSlavefailure causes downtime and automatically re-connects when it returns to normal.MasterreceivedSlaveconnection, send its full data file toSlave, ifMateralso receive multipleSlavethe synchronization request that was sentMasteronly a process is started in the background to save the data file and then send it to allSlaveto ensureSlaveNormal.


Iv. List of server resources

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/38/6A/wKiom1Ozz7PilaRGAAD2EbEmDy4850.jpg "title=" 14.png "alt=" Wkiom1ozz7pilargaad2ebemdy4850.jpg "/>


Five, the configuration process

about the Redis Installation and configuration It's not working here. READ: Redis for high performance Database Cache (i)http://cfwlxf.blog.51cto.com/3966339/1423106


3,1 MasterThe end operation is as follows:RunRedisService
[Email protected]_master sh]# redis-server/etc/redis/redis.conf
EnquiryRedisRun Log

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/38/6A/wKioL1Ozz6bw05VBAAJdwyDX2eg504.jpg "title=" 15.png "alt=" Wkiol1ozz6bw05vbaajdwydx2eg504.jpg "/>

## by reading some of the information in the log file output, you can see Master with the Slave session mechanism to be performed when a connection is established: Load the data file to the hard disk, when 0.012 seconds, imagine the speed is how fast, of course, according to the size of the data to evaluate; services connected to 6379 Port, received Slave Sync connection request, open "BGSAVE" synchronization, etc.;


ClearMasterin the End database, allKey
[[Email protected]_master sh]# redis-cli127.0.0.1:6379> flushallok127.0.0.1:6379> keys * (empty list or set)
3,2 SlaveThe end operation is as follows:

[Email protected]_slave ~]# vim/etc/redis/redis.conf

# Add the IP and Port on the Master side

# slaveof <masterip><masterport>slaveof 192.168.8.8 6379


RunRedis
[Email protected]_slave ~]# redis-server/etc/redis/redis.conf
EnquirySlaveRun Log

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/38/6A/wKioL1Ozz7vRolYwAAOCbyhb-MU029.jpg "title=" 16.png "alt=" Wkiol1ozz7vrolywaaocbyhb-mu029.jpg "/>

##AnalysisRedislog, you can seeSlavewith theMasterthe process of establishing a connection, data synchronization, such as: SendingSYNCcommand, withMasterEnd192.168.8.8:6379establish the connection, and thenSlave Sync started; thenMasterSendPINGcommand CheckSlavethe surviving state, replication is continued ....

querying all the databases in theKey
[[Email Protected]_slave ~]# redis-cli 127.0.0.1:6379> keys * (empty list or set)
3,3 Slave2The end operation is as follows:

[Email protected]_slave2 ~]# vim/etc/redis/redis.conf

# Add IP and Port of Slave Port , realize cascade replication;

# slaveof <masterip><masterport>slaveof 192.168.8.10 6379

#RunRedisService
[Email protected]_slave2 ~]# redis-server/etc/redis/redis.conf

EnquiryRedisRun Log

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/38/6B/wKiom1Oz0AGRPpzyAAOAB3zsl1Q226.jpg "title=" 17.png "alt=" Wkiom1oz0agrppzyaaoab3zsl1q226.jpg "/>

## Results and Slave1 similar, but Slave2 with the Slave1 ( 192.168.8.10:6379 ) Establish a connection and synchronize the data; MySQL cascade replication is like this, master->slave1->slave2;

#querying all of the databaseKey

[Email protected]_slave2 ~]# REDIS-CLI

127.0.0.1:6379> keys *

(empty list or set)

3,4 MasterThe end operation is as follows:
[[Email protected]_master sh]# redis-cli127.0.0.1:6379> MSET ID 1005 namemariadb City beijingok127.0.0.1:6379> MGET ID name City1) "1005" 2) "MariaDB" 3) "Beijing" 127.0.0.1:6379> Keys *) "NAME" 2) "ID" 3) "City"

3,5client-side validation synchronization resultsSlave1-Side Verification
[Email protected]_slave ~]# redis-cli127.0.0.1:6379> auth [email protected] #aedf127 .0.0.1:6379> keys *) "City" 2 "Name" 3) "id" 127.0.0.1:6379> MGET ID NAME City1) "1005" 2) "MariaDB" 3) "Beijing"
Slave2-Side Verification
[[Email protected]_slave2 ~]# redis-cli127.0.0.1:6379> keys *] "id" 2) "name" 3) "City" 127.0.0.1:6379> MGET ID NAME C ity1) "1005" 2) "MariaDB" 3) "Beijing"

Four,Master Write,Slave Readmechanism

Redis Master-slave replication, through the program to achieve the separation of data read and write, the Master is responsible for processing the writing request,Slave is responsible for processing read requests, through the expansion of Slave processing more concurrent requests, reduce the load on the Master side, such as:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/38/6B/wKioL1Ozz-3yEjbBAAEZvvETcp8136.jpg "title=" 18.png "alt=" Wkiol1ozz-3yejbbaaezvvetcp8136.jpg "/>

This drawing is relatively simple, showing the realization Redis Read-write separation process, by judging the user read-write request, the write request sent to Redis Master processing,Read request sent to Redis Slave Treatment, the shortcomings of the article, welcome to the guidance.


This article is from the "Step by foot, from Ops to DBA" blog, be sure to keep this source http://cfwlxf.blog.51cto.com/3966339/1433637

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.