当前位置:网站首页>Introduction to redis high availability
Introduction to redis high availability
2022-06-12 05:41:00 【Xicai pengyuyan】
High availability base - Master slave copy
Redis The replication function of is to support data synchronization between multiple databases ,
The main database can be read and write , When the master database data changes, it will be automatically synchronized to the slave database ,
The slave database is generally read-only , It will receive the data synchronized from the note database .
A master database can have more than one slave database , A slave database can only have one master database 
By default , Each server is the master node , Can be set by configuration Redis Nodes are called slave databases
The principle of replication
1、 When starting a slave node , It will send one psync To the master node
2、 If the slave node is connected to the master node for the first time , Will trigger a full copy , At this point, the master node will start a background process , Start generating a RDB Snapshot file
3、 At the same time, new commands received from the client will be written into the cache memory ,RDB After file generation , The master node will RDB The file is sent to the slave node , The slave node will first RDB File write to local disk , Then it is loaded into memory from the local disk
4、 Then the master node will send the cache write command in memory to the slave node , Synchronize this data from the node
5、 If a network failure occurs before the slave node and the master node , Connection is broken , Will automatically reconnect , After connection, the master node will synchronize some missing data to the slave node
Master slave replication configuration
By default , Each node is the master node , Just configure the slave node
You can copy Redis.conf The configuration file , Modify the main information
Port number
pid name
log File name
dump.rdb name
You can modify the configuration , The configuration is permanently valid , It can also be processed in the form of commands
command :info replication: View current library information 
command :slaveof ip port : Execute the name... On the slave server , Given the port and address of the primary server IP
command :Slave no one : You can restore from a node to a host
Start the three Redis service , Given port 6380、6381、6382
Given 6380 Master node
1、 establish Redis example
nohup redis-server --port 6380 >> /usr/local/redis/data/log/6380.log 2>&1 &
nohup redis-server --port 6381 >> /usr/local/redis/data/log/6381.log 2>&1 &
nohup redis-server --port 6382 >> /usr/local/redis/data/log/6382.log 2>&1 &
[root@redis ~]# ps -ef |grep redis
root 16421 16314 0 03:01 pts/1 00:00:00 redis-server *:6380
root 16427 16314 0 03:01 pts/1 00:00:00 redis-server *:6381
root 16431 16314 0 03:01 pts/1 00:00:00 redis-server *:6382
2、 Connect to the database and set up master-slave replication
Gong-Dezhe-MBP:log gongdezhe$ redis-cli -p 6380
127.0.0.1:6380>
Gong-Dezhe-MBP:~ gongdezhe$ redis-cli -p 6381
127.0.0.1:6381> slaveof 127.0.0.1 6380
OK
Gong-Dezhe-MBP:~ gongdezhe$ redis-cli -p 6382
127.0.0.1:6382> slaveof 127.0.0.1 6380
OK
After the master-slave configuration is complete , Master node information 
From node information :
Data synchronization test :

High availability solution - Sentinel mode sentinel
The built master-slave replication scheme can achieve the purpose of data synchronization , But when the primary server goes down , You need to manually switch one from the server to the master server , This process requires manual intervention , Switching at the same time will lead to Redis The write function for is not available .
Need a way to automatically complete master Fault detection and be able to put a Slave Switch to a master, Sentinels are needed at this time sentinel Pattern , Sentinel mode can automatically switch between master and slave nodes .
Introduction to sentinel mode
When we set up redis A problem will be found after the master-slave replication scheme , That is, when the primary server goes down , You need to manually switch one from the server to the primary server , This requires human intervention , laborious , At the same time, it will also cause redis Server write not available . So we need a way to accomplish Master After a fault, it can automatically send a Slave Switch to a Master, This is the time sentinel Sentinel mode . The sentinel mechanism can automatically switch between master and slave nodes .( Automatic election of the boss )
Introduction to sentinel mode
sentinel It is an official high availability solution , The principle is that the sentry sends orders , wait for Redis Server response , So as to monitor the operation of multiple Redis example . meanwhile sentinel It's a distributed system , You can run multiple Sentinel process , It can be done sentinel High availability .
sentinel working process :
● By sending to the master and slave servers ping command , Return the server to running state .
● When the Sentinels detect master Downtime , Will automatically send a slave Switch to master, Then the other slave servers are informed through publish subscribe mode , Modify the configuration file , Let them switch hosts .
About sentinel Three scheduled tasks :
● Every time 1 Every second sentinel For others sentinel and redis Node execution ping operation , The heartbeat detection .
● Every time 10 Every second sentinel Would be right master and slave perform info command , The purpose is to find slave node , Determine the master-slave relationship .
● Every time 2 Every second sentinel adopt master Node channel Exchange information (pub/sub).master There is a publish subscribe channel on the node (sentinel:hello).sentinel Node passing __sentinel__:hello Channels exchange information ( For nodes " view " And your own information ), Reach a consensus .
The sentinel principle
But a sentinel process is right Redis The server monitors , There may be problems , So , We can use multiple sentinels to monitor , There will be surveillance between the Sentinels , In this way, the multi sentry mode is formed
sentinel It's a distributed system , You can run multiple Sentinel process . So monitor the same Master Of Sentinel Will automatically connect , Make up a distributed Sentinel The Internet , Communicate with each other and exchange information about the monitored server .
Sentinel troubleshooting principle
1. Subjective offline :
When the primary server fails , One at this time sentinel Fault found , The system doesn't work right away failover The process ( This phenomenon is called subjective offline ), It's going to go to the rest of the network Sentinel Confirm .
2. Objective offline :
And then the rest Sentinel Faults have also been found one after another , At this time, one of them Sentinel Will vote . A certain number of sentinels ( Specified in the configuration file ) confirm Master Marked as subjective offline , At this time will be Master Mark as objective .
3.sentinel Of leader The election :
To complete failover ( Will malfunction master To eliminate , And will be one slave Upgrade to master) You have to elect one first leader. The first to find the fault sentinel Make a request to other sentinels to become leader, The other sentinels did not agree with the other sentinels leader When asked , Will vote for the sentinel. When more than half sentinel After the vote is passed, it is determined that sentinel by leader. The next failover has this leader complete .
4.master The election :
leader After selection, the fault will master To eliminate , from slave Choose one of them to be master. The principles followed are as follows :
● slave The priority of the
● slave from master The amount of data synchronized , that slave More is preferred .
5. new Master Then notify all users through publish subscribe mode sentinel Update monitoring host information .
6. After the failed master server is repaired, it will become the slave server to continue working .
The example is as follows :
Failure occurs 
Fail over 
Master After re launching 
The advantages of sentinel mode
● The sentry cluster 、 Based on master-slave replication mode , All the advantages of master-slave replication , It will have
● Master slave can switch , Faults can be transferred , The availability of the system will be better
● Sentinel mode is the upgrade of master-slave mode , Manual to automatic , More robust
Disadvantages of sentinel mode
● Redis Bad online expansion , Once the cluster capacity reaches the upper limit , Online expansion is very troublesome
● Implementing the configuration of sentinel mode is cumbersome , There are many choices
边栏推荐
- [grpc development] go language builds simple server and client
- 14- II. Cutting rope II
- Rtmp/rtsp/hls public network real available test address
- [gin] gin framework for golang web development
- [long time series prediction] the [4] autocorrelation mechanism of aotoformer code explanation
- Lldp protocol
- 38. 外观数列
- [Speech] 如何根据不同国家客制化ring back tone
- Codis 3. X expansion and contraction
- [road of system analyst] collection of wrong topics in software engineering chapters
猜你喜欢

CODIS long link test

Nature | 给全球的新冠伤亡算一笔账

Stack and queue classic interview questions

分公司负责人需要承担的法律责任

March 23, 2021

Is the individual industrial and commercial door a legal person enterprise

RTMP streaming +rtmp playback low delay solution in unity environment

Halcon uses points to fit a plane

Project requirements specification

Nature | make an account of the new crown casualties in the world
随机推荐
Introduction to redis cluster
Individual application for ov type SSL certificate
[untitled]
Wireshark filter rule
60. points of N dice
第五讲:数据仓库搭建(三)
DMA RDMA technology details
March 23, 2021
Select gb28181, RTSP or RTMP for data push?
UBI details and JFFS2 square FS UBIFS
How much Ma is the driving current of SIM card signal? Is it adjustable?
Detailed explanation of WiFi 802.1x authentication process
Flex/fixed upper, middle and lower (mobile end)
Field xxxxDAO in com. nero. hua. service. impl. LoginServiceImpl required a bean of type
tkinter使用WebView2网页组件(续篇)
Automated testing - Po mode / log /allure/ continuous integration
[fastapi] use pycharm to configure and use environment variables for fastapi projects
个体工商户是不是法人企业
Nature | make an account of the new crown casualties in the world
flex/fixed上中下(移动端)