当前位置:网站首页>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.
边栏推荐
- [PSQL] 窗口函数、GROUPING运算符
- 12 reasons for MySQL slow query
- 18年程序员生涯,读了200多本编程书,挑出一些精华分享给大家
- MySQL 5.7 detailed download, installation and configuration tutorial
- 提高软件测试能力的方法有哪些?看完这篇文章让你提升一个档次
- MySQL夺命10问,你能坚持到第几问?
- Browser onload event
- Detailed explanation of AMQP protocol
- Android Studio 实现登录注册-源代码 (连接MySql数据库)
- MySQL安装教程
猜你喜欢

非关系型数据库MongoDB的特点及安装

MySQL implements sorting according to custom (specified order)

apifox介绍及使用(1)。

MySQL 8.0.29 decompressed version installation tutorial (valid for personal testing)

利用浏览器本地存储 实现记住用户名的功能

swinIR论文阅读笔记

go语言中的goroutine(协程)

MySQL 8.0.29 set and modify the default password
[email protected](使用passwordYES)"/>Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)

MySql将一张表的数据copy到另一张表中
随机推荐
数学建模笔记:TOPSIS方法(优劣解距离法)和熵权法修正
07-传统的生产者消费者问题、防止虚假唤醒
Google notes cut hidden plug-in installation impression
牛客-TOP101-BM41
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了
【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组
Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
Navicat new database
c语言:查漏补缺(三)
Go language study notes - grpc serverclient protobuf Go language from scratch
MySQL 游标
Detailed explanation of AMQP protocol
MySQL安装常见报错处理大全
浏览器的onload事件
"Digital reconstruction of the system, getting the CEO is the first step"
C language: Check for omissions and fill in vacancies (3)
MySQL 字符串拼接 - 多种字符串拼接实战案例
说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~
apisix-Getting Started
腾讯注册中心演进及性能优化实践