当前位置:网站首页>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.
边栏推荐
- [Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation
- apifox介绍及使用(1)。
- Mysql实现乐观锁
- leetcode 204. Count Primes 计数质数 (Easy)
- 行测不会概念
- MySQL 8.0.29 设置和修改默认密码
- 2021年软件测试面试题大全
- mysql 查询表 所有字段
- 【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组
- How much does a test environment cost? Start with cost and efficiency
猜你喜欢

12个MySQL慢查询的原因分析

C language: Check for omissions and fill in vacancies (3)

JDBC revisited

公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范

Mysql子查询关键字的使用(exists)

腾讯注册中心演进及性能优化实践

pg数据库报错问题,有懂的吗

MySQL implements sorting according to custom (specified order)

Google Chrome(谷歌浏览器)安装使用

"Digital reconstruction of the system, getting the CEO is the first step"
随机推荐
MySql copies data from one table to another table
MySQL String Concatenation - Various String Concatenation Practical Cases
区块元素、内联元素(<div>元素、span元素)
H5接入支付流程-微信支付&支付宝支付
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组
mysql 查询表 所有字段
MySQL安装教程
Three methods of importing sql files in MySQL
Brush LeetCode topic series - 10. Regular expression match
el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
[PSQL] 函数、谓词、CASE表达式、集合运算
prisma使用mongodb副本集群报错引发的一些列问题
canvas 像素操作(图片像素操作)
Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
mysql 8.0.28版本安装配置方法图文教程
Android studio连接MySQL并完成简单的登录注册功能
Navicat报错:1045-Access denied for user [email protected](using passwordYES)
21 Day Learning Challenge Schedule
Matlab学习第二天