当前位置:网站首页>Redis6 master-slave replication and clustering
Redis6 master-slave replication and clustering
2022-07-05 13:31:00 【Do you know what a code monster is?】
Catalog
It's passed down from generation to generation
Master slave copy
After the host data is updated, according to the configuration and Policy , Automatic synchronization to the standby machine master/slaver Mechanism ,Master Write first ,Slave Mainly reading , It can realize the separation of reading and writing , Performance expansion . Disaster recovery and rapid response .
The host does write operations , Read from the machine .
Set up process
(1) stay Linux Create a new folder in the system , Copy a configuration file redis.conf Go to this folder . Will copy appendonly Set to no.

(2) In the file created in the first step , Create multiple redis The configuration file , introduce redis.conf, Set the port number port ,pid File name pidfile , Persistent file name dbfilename . Only the numbers in the three documents are different , The rest are the same .

(3) Start created redis The server , Three session windows are connected separately 3 individual redis The server .

View system progress , Three redis The server has started and three connections have been established .

(4) Enter info replication Check the operation of the three hosts , It is found that there is no master-slave relationship , every last redis Servers are hosts .
info replication 
(5) Establish a master-slave relationship , Input slaveof host IP Port number .
slaveof < host IP> < Port number >Check the slave information again , It is found that there is already a host IP And port

View host information , Display the number of slave connections 2 individual .

(6) The host writes , The slave can synchronize data , And the slave cannot write .


The principle of replication
Copy in full : After receiving the database file data from the slave , Save it and load it into memory .
Incremental replication : The host transmits all newly collected modification commands to the slave in turn , Complete synchronization .
One master and many servants
seeing the name of a thing one thinks of its function , One master connects multiple slaves .

The copying process :(1) When the master-slave relationship is established between the slave and the host , The slave sends a data synchronization request to the host (2) The host will persist the current data after receiving the request , Then send the persistent file to the slave , Reading from the machine completes synchronization .(3) After the host adds data , The host sends the new data to the slave for data synchronization , The slave only applies for data synchronization when it is connected , In the rest of the time, the host takes the initiative to send .
It's passed down from generation to generation
The slave of the host can also be the host of other servers .

The copying process : The master is connected to the slave , The slave can also be the host of other servers . Be similar to java The inheritance relationship . When the host modifies the information , From the machine to update the data of other servers .
Going to
Host down , Slave host .

When the host goes down , The slave can be upgraded to the host , By inputting the following line of code in the slave .
slaveof no oneYou can automatically monitor the host by setting sentinels , When the host goes down, the slave is automatically promoted to the host , There is no need to set it manually .
Problem analysis
From the machine down
(1) After hanging up from the machine , Restart the slave , Slaves exist in the form of hosts , It is not the slave of the previous master .
(2) If the host adds new data during the period when the slave hangs up , After restarting the slave and establishing the master-slave relationship , The data of the slave is still consistent with that of the host .
(3) After the slave is restarted , The data added before the master-slave relationship is established with the host will disappear after the relationship is established , And then the data of the host will be preserved .
Host down
(1) After the host computer is down , The master remains unchanged in the slave display information , Just the state changes to down.
(2) Restart the host , Still the primary server .
colony
Redis The cluster has realized to Redis Horizontal expansion of , Start now N individual redis node , The entire database is distributed and stored here N A node in the , Each node stores the total data 1/N.
Redis Clusters are partitioned (partition) To provide a certain degree of usability (availability): Even if some nodes in the cluster fail or fail to communicate , The cluster can also continue to process command requests .
Set up the cluster
(1) Create a new folder in the master-slave relationship , modify redis6379.conf The content of the document , Add the following information . And copy 5 Share .
cluster-enabled yes Open cluster mode
cluster-config-file nodes-6379.conf Set the node profile name
cluster-node-timeout 15000 Set the node loss time , Beyond that time ( millisecond ), The cluster automatically switches between master and slave .

Modify the numbers in each copy of the data .

(2) Start this 6 individual redis service , And synthesize a cluster .

Enter into redis Install under directory src In the folder , Execute the following command , The command means to set up a cluster in the simplest way , A master node , A slave node corresponds one by one . The allocation principle tries to ensure that each master database runs on different servers IP Address , Each slave library and master library are not in one IP Address .
redis-cli --cluster create --cluster-replicas 1 <ip>:< port > <ip>:< port >Successful cluster construction

(3) Connect clusters , View cluster information .
Use the following instructions to connect to the cluster
redis-cli -c -p 6379Use the following instructions to view the cluster node information
cluster nodes
Cluster operation
After the cluster is built successfully ,redis The number of slots in this cluster will be displayed , namely slot.

One Redis The cluster contains 16384 Slots (hash slot), Every key in the database belongs here 16384 One of the slots , The cluster uses the formula CRC16(key) % 16384 To calculate the key key Which slot does it belong to , among CRC16(key) Statements are used to evaluate keys key Of CRC16 The checksum .
Each node in the cluster is responsible for processing a portion of the slots . for instance , If a cluster can have a master node , among :
node A Responsible for handling 0 No. to 5460 Slot number .
node B Responsible for handling 5461 No. to 10922 Slot number .
node C Responsible for handling 10923 No. to 16383 Slot number .
Store the data
Test case 1 : Add data

We are 6379 The port corresponds to redis Add data to the server , Finally added to 6381 Under port , stay 6381 Data added under port , Again 6379 Add below .
This is because redis take key adopt CRC16 Statement calculates the checksum , According to the checksum and 16384 To get the result of the remainder , To select different servers to add data .
Test case 2 : Multi key operation

slot Different key values , Can't use mget,mset Wait for multi key operation .
Test case 3 : Group storage

have access to {} To define the concept of a group , Give Way key in {} Key value pairs with the same content in the same slot in .
Reading data
Test case 1 : Reading data

Different key, You need to get data from the corresponding node .
Test case 2 : of key and slot The operation of
see key Corresponding slot
cluster keyslot <key>see slot How many key( Only those within the current node can be viewed slot)
cluster countkeysinslot <slot>return count individual slot Key in groove ( Only those within the current node can be viewed slot)
cluster getkeysinslot <slot> <count> 
Fault recovery
(1) If a host goes down , The corresponding slave will be upgraded to the master .
(2) If you start the previous host again , Will become a slave .( It can be understood as Feng Shui turns , The boss takes turns to be )
(3) If both the master and slave are down , The cluster does not necessarily hang up . If redis.conf Parameters in cluster-require-coverage Set to yes , Then the cluster hangs up . If set to no , Then the slot data corresponding to this node cannot be used , Can't store .
边栏推荐
- Shandong University Summer Training - 20220620
- #从源头解决# 自定义头文件在VS上出现“无法打开源文件“XX.h“的问题
- Win10 - lightweight gadget
- 碎片化知识管理工具Memos
- Changing JS code has no effect
- 手把手带你入门Apache伪静态的配置
- 国际自动机工程师学会(SAE International)战略投资几何伙伴
- What is a network port
- [daily question] 1200 Minimum absolute difference
- 量价虽降,商业银行结构性存款为何受上市公司所偏爱?
猜你喜欢

RHCSA9

Android本地Sqlite数据库的备份和还原

Idea设置方法注释和类注释

Write API documents first or code first?

今年上半年,通信行业发生了哪些事?

Huawei push service content, read notes
![[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging](/img/52/5e85743b1817de96a52e02b92fd08c.png)
[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging

记录一下在深度学习-一些bug处理
![[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接](/img/b6/f9da8a36167db10c9a92dabb166c81.png)
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接

精彩速递|腾讯云数据库6月刊
随机推荐
My colleague didn't understand selenium for half a month, so I figured it out for him in half an hour! Easily showed a wave of operations of climbing Taobao [easy to understand]
STM32 reverse entry
FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
Interviewer soul torture: why does the code specification require SQL statements not to have too many joins?
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
go map
个人组件 - 消息提示
Notion 类笔记软件如何选择?Notion 、FlowUs 、Wolai 对比评测
Shuttle INKWELL & ink components
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
MySQL --- 数据库查询 - 排序查询、分页查询
Flutter 3.0更新后如何应用到小程序开发中
Realize the addition of all numbers between 1 and number
Integer = = the comparison will unpack automatically. This variable cannot be assigned empty
数据湖(七):Iceberg概念及回顾什么是数据湖
国际自动机工程师学会(SAE International)战略投资几何伙伴
Idea set method annotation and class annotation
TortoiseSVN使用情形、安装与使用
How to choose note taking software? Comparison and evaluation of notion, flowus and WOLAI
Go array and slice