Redis Master and Standby replication

Source: Internet
Author: User

Redis supports Master-slave (master-slave) mode, where Redis server can be set up as a host (slave) for another Redis server, from which the data is periodically taken from the host computer. Specifically, a slave can also be set up as a Redis server host, so that the master-slave distribution looks like a forward-free graph, a Redis server cluster, whether the host or slave is a Redis server, can provide services.

After configuration, the host master can be responsible for reading and writing services , from the machine slave only read . Redis improves this configuration so that it supports weak consistency of data, that is, eventual consistency. Redis replication is non-blocking at the end of the master, meaning that master can still execute the client's operation commands without affecting the slave when synchronizing with the data.

Second, Redis master-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 request, 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 is performing a new synchronization, it can still use the old data information to provide the query; otherwise, when slave loses contact with master, 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, the principle of Redis master-slave replication

1. When a slave process is started, it sends a sync Command to master to request a synchronous connection. Whether it is the first connection or the reconnection, master initiates a background process, saves the data snapshot to the data file, and master logs all commands that modify the data and caches it in the data file;

2, after the background process completes the cache operation, Master sends the data file (DUMP.RDB) to the Slave,slave side to save the data file to the hard disk, and then loads it into memory, then master will all modify the data operation, send it to the slave side.

3, if the slave failure results in downtime, return to normal will automatically reconnect, master received slave connection, the full data file sent to slave, if Mater at the same time received multiple slave sent to the synchronization request, Master will only start a process in the background to save the data file and then send it to all slave to make sure the slave is normal.

First: The slave to master synchronization is implemented as follows:

Slave sends the sync request (Sync command) to master, the master dumps the Rdb file, then transfers the Rdb file to slave, then the master forwards the cached write command to Slave, and the first synchronization is completed;

Second: The synchronous implementation principle is as follows: Master sends a snapshot of a variable directly to each slave in real time, but for whatever reason the slave and master disconnects will repeat the above two steps. The master-slave replication of Redis is based on the persistence of memory snapshots, as long as there is slave there will be memory snapshots.

RDB Replication Disadvantages See:http://my.oschina.net/hanruikai/blog/308007?fromerr=VJsCzCcq

Four, Redis master-slave configuration

Implementing master-slave replication requires only modifying the following parameters in the slave corresponding redis.conf file, which does not need to be modified in master:

Slaveof <master ip> <port>

Slaveof 192.168.126.137 6379

IP and Port-master IPs and ports

Replication configuration options in redis.conf:

     #slaveof [Masterip] [masterport]  set the IP and port #masterauth of master [ master-password]     If master needs auth, password
     # in this setting Slave-serve-stale-data yes       If the slave connection to master is broken, this option determines whether slave continues to serve
      #slave-read-only yes               Slave is a read-only #repl-ping-slave-period 10        the time interval of the slave end of the master end, Effective
     #repl-timeout 60         to detect slave connections at all times           replication Connection time-out
     # Slave-priority 100               The weight of Slave, For Redis Sentinel mode, if Master is down, a slave with a large weight replaces the master

Replication is a replication feature provided by Redis for data synchronization that master provides to slave. Slave after connecting the master, the master side will start a process in the background to establish the Rdb file, when the file is established, sent to the slave side, after the slave side receives, will complete the copy to master through the Rdb file.

Specific examples are as follows:

Open three terminals first, then three instances and connect them with three clients respectively:

A:SRC $./redis-server--port 10000--daemonize Yes

A:SRC $./redis-cli-p 10000

Port 10000 does the master.

Slave 01:

A:SRC $./redis-server--port 10001--daemonize Yes

A:SRC $./redis-cli-p 10001

Slave 02:

A:SRC $./redis-server--port 10002--daemonize Yes

A:SRC $./redis-cli-p 10002

The above just lets their instance start up and connect it with the client, and does not set the master-slave relationship. Execute the following command on slave 01 and slave 02:

127.0.0.1:10001> slaveof 127.0.0.1 10000

Ok

127.0.0.1:10001>

This will set up the master-slave relationship. Let's try and see if it works.

127.0.0.1:10001> Get testkey001

(nil)

127.0.0.1:10001>

There is no value at this time.

Execute on Master:

127.0.0.1:10000> Set testkey001 testvalue001

Ok

127.0.0.1:10000>

And see if there are any slave on it:

127.0.0.1:10001> Get testkey001

"Testvalue001"

127.0.0.1:10001>

127.0.0.1:10002> Get testkey001

"Testvalue001"

127.0.0.1:10002>

When you set up a master-slave relationship, the slave will send a sync command to master the first time it connects or re-connects master slave;

After master receives the instruction, start the background save process to save the data, and then collect all the data modification instructions. The background is saved, Master sent this data to Slave,slave first save the data to disk, and then load it into memory, master then the collected data modification instruction line to send Slave,slave received after the re-execution of the instruction, In this way, data synchronization is achieved.

Slave automatically reconnect after losing contact with master. If master receives multiple slave synchronization requests, it performs a single background save for all slave services.

Redis Master and Standby 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.