Replication of Redis (Master/slave)

Source: Internet
Author: User
Tags failover

What is it:

That is what we call the master-slave replication, after the host data update according to the configuration and policy, automatic synchronization to the master/slaver mechanism of the standby machine, master to write-based, slave to read the main

What do you do:

Read-write separation, disaster recovery

How to Play:

1. Match from (library) unworthy Master (library)

2. From library configuration: slaveof Main Library IP main Library port

Every time you disconnect from master, you need to reconnect unless you configure the redis.conf file

Info replication

3. Modify configuration file Details operation

Copy multiple redis.conf files

Turn on Daemonize Yes

PID file name

Specify port

Log file name

Dump.rdb Name

4. Common 3 strokes:

A master and two servants

Legend of Fire

His new

Demo on different ports of the same machine, similar to multiple machines

Copy Multiple configuration files

[[email protected] myredis]# CP redis.conf Redis6379.conf[[email protected] myredis]# CP redis.conf redis6380.conf[[ Email protected] myredis]# CP redis.conf Redis6381.conf[[email protected] myredis]#

Modify the corresponding configuration file separately

Info replication

127.0.0.1:6379> Info replication# replicationrole:masterconnected_slaves:0Master_repl_offset: 0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset: 0Repl_backlog_histlen:

127.0.0.1:6380> Info replication# replicationrole:masterconnected_slaves:0Master_repl_offset: 0repl_backlog_active:0repl_backlog_size:1048576Repl_backlog_first_byte_offset: 0Repl_backlog_histlen:

127.0.0.1:6381> Info replication# replicationrole:masterconnected_slaves:0Master_repl_offset: 0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset: 0Repl_backlog_histlen:

A servant two lord:

127.0.0.1:6379> Set K1 v1
Ok
127.0.0.1:6379> Set K2 v2
Ok
127.0.0.1:6379> Set K3 v3
Ok
127.0.0.1:6379> keys *
1) "K3"
2) "K2"
3) "K1"

127.0.0.1:6380> slaveof 127.0.0.1 6379
Ok
127.0.0.1:6380> get K4
"V4"
127.0.0.1:6380>

127.0.0.1:6381> slaveof 127.0.0.1 6379
Ok
127.0.0.1:6381> Get K1
"V1"
127.0.0.1:6381>

Re-enter Info replication

127.0.0.1:6379> Info Replication
# Replication
Role:master
Connected_slaves:2
Slave0:ip=127.0.0.1,port=6380,state=online,offset=291,lag=1
Slave1:ip=127.0.0.1,port=6381,state=online,offset=291,lag=0
master_repl_offset:291
Repl_backlog_active:1
repl_backlog_size:1048576
Repl_backlog_first_byte_offset:2
repl_backlog_histlen:290
127.0.0.1:6379>

127.0.0.1:6380> Info Replication
# Replication
Role:slave
master_host:127.0.0.1
master_port:6379
Master_link_status:up
Master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:333
slave_priority:100
Slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6380>

Read/write Separation:

Slave node cannot write

127.0.0.1:6380> Set K6 V6 (Error) READONLY you can

When the host hangs out:

127.0.0.1:6379> SHUTDOWN
Not connected> exit
You have new mail in/var/spool/mail/root
[Email protected] ~]#

Slave node State:

127.0.0.1:6380> Info Replication
# Replication
Role:slave
master_host:127.0.0.1
master_port:6379
Master_link_status:down
Master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:753
Master_link_down_since_seconds:17
slave_priority:100
Slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6380>

After the host has been repaired:

[Email protected] ~]# redis-server/myredis/redis6379.conf
[Email protected] ~]# redis-cli-p 6379
127.0.0.1:6379> Set K7 V7
Ok
127.0.0.1:6379>

 

Slave node:

127.0.0.1:6380> Get K7
"V7"
127.0.0.1:6380>

In the present case, the host hangs up and the slave will not change state, will be in place standby

After the slave is down, it will need to be reconnected with the host unless it is written into the Conf profile

127.0.0.1:6380> slaveof 127.0.0.1 6379
Ok
127.0.0.1:6380> Get K7
"V7"
127.0.0.1:6380>

Legend of Fire:

The primary node of configuration 6381 is 6380

127.0.0.1:6381> slaveof 127.0.0.1 6380
Ok
127.0.0.1:6381>

The node state is now

127.0.0.1:6379> Info Replication
# Replication
Role:master
Connected_slaves:1
Slave0:ip=127.0.0.1,port=6380,state=online,offset=1957,lag=1
master_repl_offset:1957
Repl_backlog_active:1
repl_backlog_size:1048576
Repl_backlog_first_byte_offset:2
repl_backlog_histlen:1956
127.0.0.1:6379>

127.0.0.1:6380> Info Replication
# Replication
Role:slave
master_host:127.0.0.1
master_port:6379
Master_link_status:up
Master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:2027
slave_priority:100
Slave_read_only:1
Connected_slaves:1
Slave0:ip=127.0.0.1,port=6381,state=online,offset=71,lag=1
master_repl_offset:71
Repl_backlog_active:1
repl_backlog_size:1048576
Repl_backlog_first_byte_offset:2
Repl_backlog_histlen:70
127.0.0.1:6380>

127.0.0.1:6381> Info Replication
# Replication
Role:slave
master_host:127.0.0.1
master_port:6380
Master_link_status:up
Master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:99
slave_priority:100
Slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381>

His new

Slaveof No one: causes the current database to stop synchronizing with other databases and into the primary database

127.0.0.1:6380> slaveof No One
Ok
127.0.0.1:6380> Info Replication
# Replication
Role:master
Connected_slaves:1
Slave0:ip=127.0.0.1,port=6381,state=online,offset=267,lag=0
master_repl_offset:267
Repl_backlog_active:1
repl_backlog_size:1048576
Repl_backlog_first_byte_offset:2
repl_backlog_histlen:266
127.0.0.1:6380>

Copy principle:

Slave a sync command is sent when the boot is successfully connected to master

Master receives a command to start the background of the disk process, while collecting all the received to modify the DataSet command, after the background process completes, Master will transfer the entire data file to slave to complete a full synchronization

Full-volume replication: The slave service will save the database file data and load it into memory after receiving it.

Incremental replication: Master continues to pass all newly collected modification commands to slave, completing the synchronization, but once the master is reconnected, a full synchronization (full copy) will be performed automatically

Sentinel Mode (Sentinel):

What is it:

  His new automatic version, to be able to monitor the host if the failure, if the failure of the number of votes automatically from the library converted to the main library.

 How to Play:

Adjustment structure, 6379 with 80,81

    

127.0.0.1:6380> slaveof 127.0.0.1 6379OK127.0.0.1:6380>Info repication127.0.0.1:6380>info replication# replicationrole:slavemaster_host:127.0.0.1Master_port:6379Master_link_status:upmaster_last_io_seconds_ago:7master_sync_in_progress:0Slave_repl_offset:3833slave_priority:100slave_read_only:1connected_slaves:0Master_repl_offset:225repl_backlog_active:1repl_backlog_size:1048576Repl_backlog_first_byte_offset:2Repl_backlog_histlen:224127.0.0.1:6380>
 127.0.0.1:6381> slaveof 127.0.0.1 6379ok  127.0.0.1:6381> info replication# replicationrole:slavemaster_host:  127.0.0.1 Master_port:  6379master_link_status:upmaster_last_io_seconds_ago:  5 Span style= "COLOR: #000000" >master_sync_in_progress:  0slave_repl_offset:  3861slave_priority:  100slave_read_only:  1connected_slaves:  0master_repl_offset:  0repl_backlog_active:  0repl_backlog_ Size:  1048576repl_backlog_first_byte_offset:  0 repl_backlog_histlen:  0127.0.0.1:6381> 

Custom/myredis directory to create a new sentinel.conf file, the name can not be wrong

[email protected] myredis]# Touch sentinel.conf

Contents of the Sentinel.conf file: (at the end of the 1, the number of votes is 1 votes, who is the new leader)

Sentinel Monitor host6379 127.0.0.1 6379 1

Start Sentinel:

Start with redis-sentinel/myredis/sentinel.conf

      

[Email protected] myredis]# redis-sentinel/myredis/sentinel.conf9871:X 09:22:52.073 * Increased maximum number of open files to 10032 (it is originally set to 1024). _._                                                             _.-"__"-._                                                   _.-``    `.  `_. "-._ Redis 3.0.4 (00000000/0) 64bit.-`` .-```. "\ \ _.,_"-._                                    (    ',.-' | ', ') Running in Sentinel Mode | '-._ '-...-' __...-. '-._| '     ' _.-' |    port:26379 |     '-._ '. _/_.-' |                                   pid:9871 '-._ '-._ '-./_.-' _.-' | '-._ '-._ '-.__.-' _.-' _.-' |    |           '-._ '-._ _.-' _.-' | http//Redis.io'-._ '-._ '-.__.-' _.-' _.-' | '-._ '-._ '-.__.-' _.-' _.-' |    | '-._ '-._ _.-' _.-' |                                    `-._ '-._ '-.__.-' _.-' _.-'-._ '-.__.-' _.-'                                                 `-._ _.-'-.__.-' 9871:x 09:22:52.077 # warning:the TCP Backlog setting of 511 cannot be enforced BECAUSE/PROC/SYS/NET/CORE/SO Maxconn is set to the lower value of 128.9871:x 09:22:52.077# Sentinel Runid is 07858ebd94d326270f4a56e0501afefe82e444b19871:x 09:22:52.079 # +monitor Master host6379 127.0.0.1 6379 Quorum 19871:x-09:22:53.077 * +slave Slave 127 .0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 63799871:x Nov 09:22:53.091 * +slave slave 127.0.0.1:6381 127.0.0.1 638 1 @ host6379 127.0.0.1 6379

The original master hung up:

The Sentinel's Watch Window adds new content:

9895:x-09:42:54.522 # +sdown Master host6379 127.0.0.1 63799895:x (Nov 09:42:54.522 # +odown Master host6379 127. 0.0.1 6379 #quorum 1/19895:x 09:42:54.522 # +New-epoch 19895:x 09:42:54.522 # +Try-failover Master host6379 127.0.0.1 63799895:x Nov 09:42:54.648 # +vote- for-leader ccc2dc958a8ae1e16294b81567126ec4031bf192 19895:x 09:42:54.648 # +elected-leader Master host6379 127.0.0.1 63799895:x 09:42:54.648 # +failover-state-select-slave Master host6379 127.0.0.1 63799895:x-Nov 09:42:54.725 # + Selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 63799895:x-Nov 09:42:54.725 * +failover-state-s End-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 63799895:x-Nov 09:42:54.778 * +failover-stat  e-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 63799895:x-Nov 09:42:55.704 # +promoted-slave Slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 63799895:x (Nov 09:42:55.704 # +failover-state-reconf-slaves m Aster host6379 127.0.0.1 63799895:x Nov 09:42:55.752 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ host637  9 127.0.0.1 63799895:x 09:42:56.778 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 63799895:x 09:42:56.778 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 63799895:x (Nov 09:42:56.877 # +fai Lover-end Master host6379 127.0.0.1 63799895:x Nov 09:42:56.877 # +Switch-master host6379 127.0.0.1 6379 127.0.0.1 63809895:x Nov 09:42:56.878 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ Ho st6379 127.0.0.1 63809895:x 09:42:56.878 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380

6380 becomes the new master

127.0.0.1:6380> Info replication# replicationrole:masterconnected_slaves:1slave0:ip= 127.0.0.1,port=6381,state=online,offset=123955,lag=0master_repl_offset:123955repl_backlog_active: 1repl_backlog_size:1048576repl_backlog_first_byte_offset:2Repl_backlog_histlen: 

The status of 6381 at this time

127.0.0.1:6381> Info replication# replicationrole:slavemaster_host:127.0.0.1master_port:6380 Master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0Slave_repl_ Offset:125992slave_priority:slave_read_only: 1connected_slaves:0Master_ Repl_offset:0repl_backlog_active:0repl_backlog_size:1048576Repl_backlog_first_ Byte_offset:0Repl_backlog_histlen:

Problem: If the previous master reboots back, will the dual master conflict

After starting the 6379 hanging off

Sentinel Surveillance Print:

9895:x 09:43:26.931 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 63809895:x-Nov 09:45:51.01 5 #-sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 63809895:x Nov 09:46:00.994 * +convert-to-slave SLA ve 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380

After startup, the status of 6379:

becomes the salve node of the current master

127.0.0.1:6379> Info replication# replicationrole:slavemaster_host:127.0.0.1master_port:6380 Master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0Slave_repl_ Offset:132805slave_priority:slave_read_only: 1connected_slaves:0Master_ Repl_offset:0repl_backlog_active:0repl_backlog_size:1048576Repl_backlog_first_ Byte_offset:0Repl_backlog_histlen:

Disadvantages of replication:

Replication delay: Because all the write operation is idle on the master, and then update to the salve, so from master synchronization to the slave machine has a certain delay, when the system is busy, the delay problem will be more serious, the increase in the number of slave machines will also make this problem more serious.

Replication of Redis (Master/slave)

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.