当前位置:网站首页>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 :
- Add... To the configuration file slaveof{masterHost}{masterPort} along with Redis Start to take effect
- stay redis-server Add after starting command –slaveof{masterHost}{masterPort} take effect
- 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
边栏推荐
- 在小程序中实现视频通话及互动直播的一种方法
- Redis learning notes - slow query analysis
- H-index of leetcode topic analysis
- Best time to buy and sell stock
- Combination sum of leetcode topic analysis
- 4sum of leetcode topic analysis
- Click Add drop-down box
- 438. Find All Anagrams in a String
- 文件的打开新建与存储
- Best time to buy and sell stock II
猜你喜欢
随机推荐
Redis学习笔记—遍历键
Learn SCI thesis drawing skills (E)
65. Valid Number
Unity grid programming 06
Fraction to recursing decimal
'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
Redis learning notes - detailed explanation of redis benchmark
297. Serialize and Deserialize Binary Tree
Redis学习笔记—数据类型:字符串(string)
Redis learning notes - data type: Set
Node request module cookie usage
扫码登录基本流程
MySQL fault case | error 1071 (42000): specified key was too long
Aiming at the overseas pet market, "grasshand" has developed an intelligent tracking product independent of mobile phones | early project
MySQL故障案例 | mysqldump: Couldn’t execute ‘SELECT COLUMN_NAME
Basic use of lua
Redis学习笔记—持久化机制之AOF
Intelligent operation and maintenance exploration | anomaly detection method in cloud system
12个球,有一个与其他不一样,提供一个天平,三次找出来
1、 Software architecture evaluation





![[learning resources] understand and love mathematics](/img/a3/e1b0915c48c85d17c48a4bee523424.png)
