当前位置:网站首页>Redis learning notes master-slave copy

Redis learning notes master-slave copy

2022-06-23 09:11:00 Love Guoba

In order to solve the single point problem in distributed system , It is common to deploy multiple copies of data replication to other machines , Meet the requirements of fault recovery and load balancing .Redis So it is with , It provides us with replication function , Realize multiple... Of the same data Redis copy . Replication is highly available Redis The basis of

Create replication

Participating in replication Redis Instances are divided into main nodes (master) And slave nodes (slave). Each slave can only have one master , A master node can have multiple slaves , The replicated data flow is unidirectional , It can only be copied from the master node to the slave node , There are three ways :

  1. Add... To the configuration file slaveof{masterHost}{masterPort} along with Redis Start to take effect
  2. stay redis-server Add after starting command –slaveof{masterHost}{masterPort} take effect
  3. Direct use command :slaveof{masterHost}{masterPort} take effect

Open two different port services locally , They are the default 6379 Port and self started 6666 port , stay 6666 Start replication in the service of port :

127.0.0.1:6666> slaveof 127.0.0.1 6379
....
Finished with success

Now let's look at one 6666 Port instance key by myname There is no value for

127.0.0.1:6666> get myname
(nil)

At the main node 6379 Instance to insert

127.0.0.1:6379> set myname Charlie
OK

Let's look at it later 6666 Port instance , Values have been copied

127.0.0.1:6666> get myname
"Charlie"

adopt info View master-slave information
Master node 6379 port

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6666,state=online,offset=957,lag=0
master_replid:3e32aa6882ae57742f8d12bd9eb3c530c3ff5a74
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:957
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:957

From the node 6666 port

# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:915
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:3e32aa6882ae57742f8d12bd9eb3c530c3ff5a74
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:915
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:113
repl_backlog_histlen:803

Disconnect replication

slaveof no one

To break off 6666 The slave node , At this time

127.0.0.1:6666> slaveof no one
···
OK

Now info replication When you view the command, you will find that the status has changed from the slave node to the master node

127.0.0.1:6666> info replication
# Replication
role:master
connected_slaves:0
master_replid:f15dd49506a152bd5fbbeaab728314e7cc62fc15
master_replid2:3e32aa6882ae57742f8d12bd9eb3c530c3ff5a74
master_repl_offset:1279
second_repl_offset:1280
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:113
repl_backlog_histlen:1167

Security

For nodes with important data , The master node will be set requirepass Parameters for password verification , At this time, all client access must use auth The command performs verification . The replication connection between the slave node and the master node is completed through a specially identified client , Therefore, you need to configure the slave node masterauth The parameter is consistent with the password of the master node , In this way, the slave node can correctly connect to the master node and initiate the replication process

read-only

replica-serve-stale-data yes The default slave node is read-only , Ensure data consistency

# When a replica loses its connection with the master, or when the replication
# is still in progress, the replica can act in two different ways:
#
# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will
# still reply to client requests, possibly with out of date data, or the
# data set may just be empty if this is the first synchronization.
#
# 2) if replica-serve-stale-data is set to 'no' the replica will reply with
# an error "SYNC with master in progress" to all the kind of commands
# but to INFO, replicaOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG,
# SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB,
# COMMAND, POST, HOST: and LATENCY.
#
replica-serve-stale-data yes

Transmission delay

The master and slave nodes are often not on the same server , Replication over the network is required , At this time, network delay will become an unstable factor ,Redis For us repl-disable-tcp-nodelay Parameter is used to control whether to turn off TCP_NODELAY, Off by default

# Disable TCP_NODELAY on the replica socket after SYNC?
#
# If you select "yes" Redis will use a smaller number of TCP packets and
# less bandwidth to send data to replicas. But this can add a delay for
# the data to appear on the replica side, up to 40 milliseconds with
# Linux kernels using a default configuration.
#
# If you select "no" the delay for data to appear on the replica side will
# be reduced but more bandwidth will be used for replication.
#
# By default we optimize for low latency, but in very high traffic conditions
# or when the master and replicas are many hops away, turning this to "yes" may
# be a good idea.
repl-disable-tcp-nodelay no
  • When closed , The command data generated by the master node will be sent to the slave node in time regardless of its size , In this way, the delay between master and slave will be reduced , But it increases the consumption of network bandwidth . It is suitable for good network environment between master and slave , Just like the rack or the same machine room
  • When opened , The master node will merge smaller TCP Packets to save bandwidth . The default send interval depends on Linux The kernel of , The general default is 40 millisecond . This configuration saves bandwidth but increases the delay between master and slave . It is suitable for the scenarios with complex master-slave network environment or tight bandwidth , Such as cross machine room deployment
原网站

版权声明
本文为[Love Guoba]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230900184050.html