当前位置:网站首页>Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
2022-08-02 06:18:00 【Have_MonkeyG】
One, one master multi-slave mode (master-slave mode)
1. Concept of master-slave mode
One master node and multiple slave nodes, then the master node can be responsible for: read operations, write operations.The slave node can only be responsible for read operations, not write operations.In this way, the reading pressure can be distributed from the master node to the slave node to reduce the pressure on the master node.
When the master node finishes executing the write command, it will synchronize the data to the slave node.
2. Build a master-slave mode
Principle: The match never matches the master.
(1) Preparation conditions:
Three virtual machines, one master and two slaves
In order to save memory, we can open three redis on a virtual machine
(2) Create a new folder in the redis directory to place the configuration files of 3 machines
(3) Copy 3 copies of the redis.conf configuration file into the created folder, and modify the configuration file
(4) Modify the port number and the name of the rdb file. We use 7001 as the main node, 7002 and 7003 as the child nodes

![]()
The other two are modified according to their respective port numbers
(5) Three start redis services at the same time
Start from your own configuration file
(6) Enter the client and view your own master-slave relationship

7. Configure the master-slave relationship with 7001 as the master, 7002 as the slave, and 7003 as the slave
port of the IP master node where the slaveof master node is located8. Check the master-slave relationship again

9. Test
Through the test, we can see that the data is also synchronized among the three nodes, and the master node can read and write.But slave nodes can only read.
Summary:
1. If there is a problem with the master node, the child node will not replace it and become the master node.
2. If a new slave node is added, the new slave node will synchronize the previous data.
Second, sentry mode
Due to the master-slave mode, after the master node is single, the slave node will not automatically go up.
Add a sentinel service, which monitors the master at all times. If the master hangs up, the sentinel will elect a master node from the slave nodes [sentry voting mechanism].
1. Modify the configuration
(1) Find the sentinel.conf configuration file in the redis directory
(2) Modify the configuration file

#sentinel montor Name of the master node IP of the master node Port number of the master node Number of sentinel approvalssentinel monitor mymaster 127.0.0.1 6379 2(3) Start Sentinel
redis-sentinel sentinel.conf
(4) At this point we close the master node
You will find that it selects one of the child nodes to be the parent node through a voting mechanism.
(5) Restart the previous master node
We found it became a child node
Three, clustering mode (decentralization)
Regardless of the above master-slave mode or sentinel mode, the problem of single-node write operations cannot be solved.If the concurrency of write operations is relatively high at this time.This is an experimental clustering mode [decentralized mode]
Principle:
There are 16384 hash slots built in the redis cluster. When a key-value needs to be placed in the Redis cluster, redis first uses the crc16 algorithm to calculate an integer result for the key, and then calculates the remainder of the result to 16384, so that eachThe key will correspond to a hash slot numbered between 0-16383, and redis will map the hash slot to different nodes roughly equally according to the number of nodes.
When you add a Key to Redis Cluster, it will calculate which hash slot the key should be distributed to according to crc16(key) mod 16384. There will be many keys and values in a hash slot.You can understand it as a table partition. When using redis on a single node, there is only one table, and all keys are placed in this table; after switching to Redis Cluster, 16384 partition tables will be automatically generated for you, and you will insert data according toSimple algorithm above to decide which partition your key should exist in, each partition has many keys.
Building a cluster:
1. Creation of virtual server
Three masters and three slaves:
6001,6002,6003 Masternodes
6004,6005,6006 child nodes
2. Create a cluster folder under redis to store the configuration file of the cluster
3. Modify the redis.conf configuration file
Because we are opening 6 redis services on a virtual machine, we need to modify a lot. If we only need to modify the bolded ones in future work
Redis-1 Firewall --6379
bind 0.0.0.0 line 69
port 6001 92 lines
daemonize yes 136 lines
dbfilename dump6001.rdb
# Turn on aof persistence
appendonly yes 700 lines
appendonly appendonly6001.aof
# Start the cluster
cluster-enabled yes line 833
# The configuration file of the cluster, which is automatically generated
cluster-config-file nodes-6001.conf line 841
# Cluster timeout
cluster-node-timeout 15000 847 lines
4. Put the configuration files of 6 modification numbers into the newly created folder
5. Start the server
Start all six servers
6. Allocation slot
redis-cli --cluster create --cluster-replicas 1 192.168.118.110:7000 192.168.118.110:7001 192.168.118.110:7002 192.168.118.111:7003 192.168.118.111:7004 192.168.118.111:7005Just change the ip and port to your own settings
Note: Make sure that each node has no data.
7.Test
redis-cli ==-c== -h 192.168.40.199 -p 6001Write operations can be evenly distributed to different nodes, reducing the pressure on a single master node.
边栏推荐
猜你喜欢
随机推荐
21天学习挑战赛安排
Redis-集群模式(主从复制模式,哨兵模式,集群化模式)
MySQL 的 limit 分页查询及性能问题
浏览器的onload事件
JDBC revisited
Mysql常用命令大全
MySQL 8.0.29 set and modify the default password
MySQL 8.0.29 解压版安装教程(亲测有效)
07-传统的生产者消费者问题、防止虚假唤醒
Mysql存储json格式数据
Timing task library in the language use Cron, rounding
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
MySQL安装常见报错处理大全
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
c语言:查漏补缺(三)
C language: Check for omissions and fill in vacancies (3)
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
golang generics
利用浏览器本地存储 实现记住用户名的功能









