当前位置:网站首页>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
边栏推荐
- 16. 最接近的三数之和
- 57 - II. Continuous positive sequence with sum s
- 67. convert string to integer
- How does WiFi 802.11 correspond to 802.3
- Go interface oriented programming practice
- 个人申请OV类型SSL证书
- Redis cluster cluster capacity expansion and data migration
- Details of FPGA syntax
- How much Ma is the driving current of SIM card signal? Is it adjustable?
- March 22, 2021
猜你喜欢

Why should state-owned enterprises go public

Webrtc AEC process analysis

March 23, 2021

GRP development: four communication modes of GRP

Legal liabilities to be borne by the person in charge of the branch

Select gb28181, RTSP or RTMP for data push?

网络加速谁更猛?CDN领域再现新王者

Nature | make an account of the new crown casualties in the world

公司注册认缴资金多久

Automated test - dark horse headline test project
随机推荐
[GPIO] 如何通过adb shell 修改/显示 GPIO 状态
Nature | 给全球的新冠伤亡算一笔账
Tkinter uses webview2 web component (sequel)
什么是工程预付款
Halcon 用点来拟合平面
Analysis of pointer
公司注册认缴资金多久
38. arrangement of strings
March 23, 2021
【js小知识】轻松了解js防抖与节流
[long time series prediction] the [4] autocorrelation mechanism of aotoformer code explanation
60. points of N dice
43. Number of occurrences of 1 in 1 ~ n integers
Word frequency statistics using Jieba database
Matlab: halftone and dither conversion
[gin] gin framework for golang web development
31. stack push in and pop-up sequence
Rtmp/rtsp/hls public network real available test address
Introduction to redis cluster
Details of FPGA syntax