Let's talk about master-slave replication, sentinels, and clusters in Redis
This article will take you through the master-slave replication, sentinels, and clusters in Redis. I hope it will be helpful to you!
1. Redis master-slave replication
1. Overview of master-slave replication
Master-slave replication refers to copying data from one server to other Redis servers. The former is called the master node (Master), and the latter is called the slave node (Slave); data replication is one-way, and can only be from the master node to the slave node.
By default, each Redis server is a master node; and a master node can have multiple slave nodes, but a slave node can only have one master node. [Related recommendations: Redis video tutorial]
2. The role of master-slave replication
● data Redundancy: Master-slave replication implements hot backup of data, which is a data redundancy method in addition to persistence.
● Failure recovery: When a problem occurs on the master node, the slave node can provide services to achieve rapid failure recovery; it is actually a kind of service redundancy.
● Load balancing: Based on master-slave replication, combined with read-write separation, the master node can provide write services and the slave nodes can provide read services (that is, when writing Redis data, the application connects to the master node , when reading Redis data, apply the connection slave node) to share the server load; especially in the scenario of less writing and more reading, sharing the read load through multiple slave nodes can greatly increase the concurrency of the Redis server.
● Cornerstone of High Availability: In addition to the above functions, master-slave replication is also the basis for the implementation of sentinels and clusters. Therefore, master-slave replication is the basis of Redis high availability.
3. Master-slave replication process
(1) If you start a Slave machine process, Then it will send a "sync command" command to the Master machine to request a synchronous connection.
(2) Whether it is the first connection or reconnection, the Master machine will start a background process to save the data snapshot to the data file (perform RDB operation). At the same time, the Master will also record and cache all commands to modify the data. in the data file.
(3) After the background process completes the caching operation, the Master machine will send the data file to the Slave machine. The Slave machine will save the data file to the hard disk and then load it into the memory. Then the Master machine will modify the data file. All operations on the data are sent to the Slave machine at the same time. If the Slave fails and causes downtime, it will automatically reconnect when it returns to normal.
(4) After the Master machine receives the connection from the Slave machine, it sends its complete data file to the Slave machine. If the Master receives multiple synchronization requests from the Slave at the same time, the Master will start a synchronization request in the background. process to save the data file and then send it to all slave machines to ensure that all slave machines are normal.
4. Set up Redis master-slave replication
4.1 Server IP configuration
| Server | Host Name | IP | ||
|---|---|---|---|---|
| master | 192.168.122.10 | |||
| slave1 | 192.168.122.11 | |||
| slave2 | 192.168.122.12 |
| Server | Host Name | IP | ||
|---|---|---|---|---|
| master | 192.168.122.10 | |||
| slave1 | 192.168.122.11 | |||
| slave2 | 192.168.122.12 |
| 服务器 | 主机名 | IP | 主端口 | 从端口 |
|---|---|---|---|---|
| Node1节点 | node | 192.168.122.10 | 6001 | 6004 |
| Node2节点 | node | 192.168.122.10 | 6002 | 6005 |
| Node3节点 | node | 192.168.122.10 | 6003 | 6006 |
7.2 服务器防火墙环境
systemctl stop firewalld && systemctl disable firewalld setenforce 0
7.3 创建集群配置目录及文件
[root@node ~]# cd /etc/redis
[root@node redis]# mkdir -p redis-cluster/redis600{1..6}
[root@node redis]# for i in {1..6}
> do
> cp /opt/redis-5.0.7/redis.conf /etc/redis/redis-cluster/redis600$i
> cp /opt/redis-5.0.7/src/redis-cli /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis600$i
> done
[root@node redis]# ls -R redis-cluster/
redis-cluster/:
redis6001 redis6002 redis6003 redis6004 redis6005 redis6006
redis-cluster/redis6001:
redis-cli redis.conf redis-server
redis-cluster/redis6002:
redis-cli redis.conf redis-server
redis-cluster/redis6003:
redis-cli redis.conf redis-server
redis-cluster/redis6004:
redis-cli redis.conf redis-server
redis-cluster/redis6005:
redis-cli redis.conf redis-server
redis-cluster/redis6006:
redis-cli redis.conf redis-server7.4 开启群集功能
仅以redis6001为例,其他5个文件夹的配置文件以此类推修改,特别注意端口号的修改。
[root@node redis]# cd redis-cluster/redis6001 [root@node redis6001]# vim redis.conf ##69行,注释掉bind项,默认监听所有网卡 #bind 127.0.0.1 ##88行,修改,关闭保护模式 protected-mode no ##92行,修改,redis监听端口 port 6001 ##136行,开启守护进程,以独立进程启动 daemonize yes ##832行,取消注释,开启群集功能 cluster-enabled yes ##840行,注销注释,群集名称文件设置 cluster-config-file nodes-6001.conf ##846行,注销注释,群集超时时间设置 cluster-node-timeout 15000 ##700行,修改,开启AOF持久化 appendonly yes
7.5 启动redis节点
分别进入那六个文件夹,执行命令:“redis-server redis.conf”,来启动redis节点
[root@node redis6001]# for d in {1..6}
> do
> cd /etc/redis/redis-cluster/redis600$i
> ^C
[root@node redis6001]# for d in {1..6}
> do
> cd /etc/redis/redis-cluster/redis600$d
> redis-server redis.conf
> done
[root@node1 redis6006]# ps -ef | grep redis
root 992 1 0 13:45 ? 00:00:07 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 2289 1 0 14:41 ? 00:00:00 redis-server *:6001 [cluster]
root 2294 1 0 14:41 ? 00:00:00 redis-server *:6002 [cluster]
root 2299 1 0 14:41 ? 00:00:00 redis-server *:6003 [cluster]
root 2304 1 0 14:41 ? 00:00:00 redis-server *:6004 [cluster]
root 2309 1 0 14:41 ? 00:00:00 redis-server *:6005 [cluster]
root 2314 1 0 14:41 ? 00:00:00 redis-server *:6006 [cluster]
root 2450 2337 0 14:50 pts/0 00:00:00 grep --color=auto redis7.6 启动集群
[root@node redis6006]# redis-cli --cluster create 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006 --cluster-replicas 1
六个实例分为三组,每组一主一从,前面的做主节点,后面的做从节点。下面交互的时候需要输入yes才可以成功创建。
–replicas 1表示每个主节点有1个从节点。
7.7 测试集群
[root@node1 redis6006]# redis-cli -p 6001 -c
#加-c参数,节点之前就可以互相跳转
127.0.0.1:6001> cluster slots
#查看节点的哈希槽编号范围
1) 1) (integer) 0
#哈希槽起始编号
2) (integer) 5460
#哈希槽终止编号
3) 1) "127.0.0.1"
2) (integer) 6001
#node节点主
3) "18e59f493579facea29abf90ca4050f566d66339"
4) 1) "127.0.0.1"
2) (integer) 6004
#node节点从
3) "2635bf6a0c286ef910ec5da03dbdc7cde308c588"
2) 1) (integer) 10923
2) (integer) 16383
3) 1) "127.0.0.1"
2) (integer) 6003
3) "51460d417eb56537e5bd7e8c9581c66fdd817b3c"
4) 1) "127.0.0.1"
2) (integer) 6006
3) "51a75667dcf21b530e69a3242a3e9f81f577168d"
3) 1) (integer) 5461
2) (integer) 10922
3) 1) "127.0.0.1"
2) (integer) 6002
3) "6381d68c06ddb7ac43c8f7d7b8da0644845dcd59"
4) 1) "127.0.0.1"
2) (integer) 6005
3) "375ad927116d3aa845e95ad5f0586306e7ff3a96"
127.0.0.1:6001> set num 1
OK
127.0.0.1:6001> get num
"1"
127.0.0.1:6001> keys *
1) "num"
127.0.0.1:6001> quit
[root@node1 redis6006]# redis-cli -p 6002 -c
127.0.0.1:6002> keys *
#6002端口无键值对
(empty list or set)
127.0.0.1:6002> get num
-> Redirected to slot [2765] located at 127.0.0.1:6001
"1"
#6002端口获取到num键位于6001端口,切换到6001端口并显示键值
127.0.0.1:6001> set key1 11111
-> Redirected to slot [9189] located at 127.0.0.1:6002
OK
#6001端口创建键值对,将其存至6002端口,并切换至6002端口
127.0.0.1:6002>更多编程相关知识,请访问:编程入门!!
The above is the detailed content of Let's talk about master-slave replication, sentinels, and clusters in Redis. For more information, please follow other related articles on the PHP Chinese website!
Redis: Exploring Its Data Model and StructureApr 16, 2025 am 12:09 AMRedis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.
Redis: Classifying Its Database ApproachApr 15, 2025 am 12:06 AMRedis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.
Why Use Redis? Benefits and AdvantagesApr 14, 2025 am 12:07 AMRedis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,
Understanding NoSQL: Key Features of RedisApr 13, 2025 am 12:17 AMKey features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.
Redis: Identifying Its Primary FunctionApr 12, 2025 am 12:01 AMThe core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.
Redis: A Guide to Popular Data StructuresApr 11, 2025 am 12:04 AMRedis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.
How to implement redis counterApr 10, 2025 pm 10:21 PMRedis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.
How to use the redis command lineApr 10, 2025 pm 10:18 PMUse the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment







