Redis master-Slave synchronization, read and write separation settings related operations

Source: Internet
Author: User
Tags failover
This article describes the use RedisThe master-Slave synchronization function (master, slave), so that the program to achieve read and write separation, avoid IO bottlenecks, improve data read and write efficiency.

Redis supports a master server to synchronize multiple slave servers, using the Publish/subscribe mechanism synchronously.
1 master to multiple slave, can also be layered, each slave under the slave can be synchronized, expand into a tree-like structure.

Redis Master-Slave synchronization settings

Redis default port is 6379, we use the new port in order not to affect the original Redis

Master configuration redis_master.conf

Port 6300requirepass 123456masterauth 123456daemonize Yes


slave1 configuration redis_slave1.conf set to master slave

Port 6301slaveof 127.0.0.1 6300requirepass 123456masterauth 123456daemonize Yes


slave2 configuration redis_slave2.conf set to master slave

Port 6302slaveof 127.0.0.1 6300requirepass 123456masterauth 123456daemonize Yes

daemonize indicates background startup.
Requirepass is the host authentication password.
The masterauth authenticates the password for the slave access host and needs to be consistent with the requirepass of the host.
Because of the need to demonstrate the master-slave switchover later, the authentication passwords for the three groups of Conf are consistent.

Redis Master-Slave synchronization test

Start Master, slave1, Slave2

Redis-server Redis_master.confredis-server Redis_slave1.confredis-server redis_slave2.conf

Check to see if startup is successful after execution

PS aux|grep redisroot  1858  SS  3:55  0:00.01 redis-server *:6302 root  1849  Ss  3:54  0:00.01 redis-server *:6301 Root  1842  Ss  3:54  0:00.02 redis-server *:6300

Enter master, set key ABC value to 123

Redis-cli-p 6300127.0.0.1:6300> Auth 123456ok127.0.0.1:6300> set ABC 123ok127.0.0.1:6300> get ABC "123"

Enter slave1 Separately, slave2 check if data is synchronized
SLAVE1:

Redis-cli-p 6301127.0.0.1:6301> auth 123456ok127.0.0.1:6301> get abc "123" 127.0.0.1:6301>

Slave2:

Redis-cli-p 6302127.0.0.1:6302> auth 123456ok127.0.0.1:6302> get abc "123" 127.0.0.1:6302>

Enter Master to change the value of key ABC to 456

127.0.0.1:6300> set ABC 456ok127.0.0.1:6300> get ABC "456"

Check slave1, Slave2 is synchronized
SLAVE1:

127.0.0.1:6301> get ABC "456"

Slave2:

127.0.0.1:6302> get ABC "456"

Redis Master-Slave switching

In the process of running, if the master has problems, we can set up another slave machine to automatically set the master to use. The main use of the Redis Sentinel function to achieve master-slave switching.

Sentinel1.conf

Port 26301sentinel Monitor Master 127.0.0.1 6300 2sentinel auth-pass master 123456logfile "/tmp/sentinel.log" daemonize Yes

Sentinel2.conf

Port 26302sentinel Monitor Master 127.0.0.1 6300 2sentinel auth-pass master 123456logfile "/tmp/sentinel.log" daemonize Yes

Sentinel Monitor Master 127.0.0.1 6300 2 2 indicates that more than 2 Sentinel services have detected master failure before performing a master-slave switchover.

Start two Sentinel processes

Redis-server sentinel1.conf--sentinelredis-server sentinel2.conf--sentinelps aux|grep redisroot  2643  Ss  4:28 0:00.02 redis-server *:26302 [Sentinel]  root  2636  Ss  4:28 0:00.02 redis-server *:26301 [ Sentinel

Redis logs can be seen, startup successfully started monitoring

Running Mode=sentinel, Port=26301.sentinel ID is 3a23343948cd7f26662ccba1d01b92955311ef52+monitor Master Master 127.0.0.1 6300 quorum 2+slave slave 127.0.0.1:6301 127.0.0.1 6301 @ Master 127.0.0.1 6300+slave slave 127.0.0.1:6302 127.0 .0.1 6302 @ Master 127.0.0.1 6300Running Mode=sentinel, Port=26302.sentinel ID is Ce0ee2af6b454205a3e475763945f505a10a7d6a+monitor Master Master 127.0.0.1 6300 quorum 2+slave slave 127.0.0.1:6301  127.0.0.1 6301 @ Master 127.0.0.1 6300+slave slave 127.0.0.1:6302 127.0.0.1 6302 @ Master 127.0.0.1 6300+sentinel Sentinel 3a23343948cd7f26662ccba1d01b92955311ef52 127.0.0.1 26301 @ Master 127.0.0.1 6300+sentinel Sentinel ce0ee2af6b454205a3e475763945f505a10a7d6a 127.0.0.1 26302 @ Master 127.0.0.1 6300

Terminating master, testing master-Slave switching

After the kill Master process, Sentinel determines that master fails and performs a master-slave switching process.

The logs are as follows:

+failover-state-reconf-slaves Master master 127.0.0.1 6300+slave-reconf-sent slave 127.0.0.1:6301 127.0.0.1 6301 @ Master 127.0.0.1 6300+config-update-from Sentinel 3a23343948cd7f26662ccba1d01b92955311ef52 127.0.0.1 26301 +  Switch-master Master 127.0.0.1 6300 127.0.0.1 6302+slave slave 127.0.0.1:6301 127.0.0.1 6301 @ Master 127.0.0.1 6302+slave Slave 127.0.0.1:6300 127.0.0.1 6300 @ Master 127.0.0.1 6302-odown Master master 127.0.0.1 6300+slave-reconf-inprog Slave 127.0.0.1:6301 127.0.0.1 6301 @ Master 127.0.0.1 6300+slave-reconf-done slave 127.0.0.1:6301 127.0.0.1 6301 @ Master 127.0 .0.1 6300+failover-end Master Master 127.0.0.1 6300+switch-master master 127.0.0.1 6300 127.0.0.1 6302+convert-to-slave s Lave 127.0.0.1:6300 127.0.0.1 6300 @ Master 127.0.0.1 6302

As can be seen from the log, the master-slave switchover performs the following actions:

1. switch slave2 to the new master, slaveof 127.0.0.1 6300 in redis_slave2.conf is automatically deleted.

2. Automatically update the redis_slave1.conf slaveof 127.0.0.1 6300 to slaveof 127.0.0.1 6302, using Slave2 as the new master.

3. after the original master restarts, it will be used as slave , redis_master.conf will automatically join slaveof 127.0.0.1 6302.


Master-Slave synchronization test after original master reboot

The original master update key ABC is 888 because it is now slave, so the update fails.

127.0.0.1:6300> SET ABC 888 (ERROR) READONLY you can ' t write against a read only slave.

slave2 Update key ABC to 888

127.0.0.1:6302> set ABC 888ok127.0.0.1:6302> get ABC "888"

Original MASTER,SLAVE1 Check whether synchronization

Original master

127.0.0.1:6300> Get ABC "888"

Slave1

127.0.0.1:6301> Get ABC "888"

After checking, the master-slave switch, slave2 as the new master, other servers as slave, can be used normally.

This article explains the Redis master-slave synchronization, read and write separation settings related operations, more relevant content of the pro-English web.

Related recommendations:

Describes how MySQL rebuilds table partitions and preserves data

PHP generates a unique RequestID class of related content

PHP Json_encode does not support object private property resolution

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.