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-server
7.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 redis
7.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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. Start the [Start] menu, enter [cmd], right-click [Command Prompt], and select Run as [Administrator]. 2. Enter the following commands in sequence (copy and paste carefully): SCconfigwuauservstart=auto, press Enter SCconfigbitsstart=auto, press Enter SCconfigcryptsvcstart=auto, press Enter SCconfigtrustedinstallerstart=auto, press Enter SCconfigwuauservtype=share, press Enter netstopwuauserv , press enter netstopcryptS

PHP function bottlenecks lead to low performance, which can be solved through the following steps: locate the bottleneck function and use performance analysis tools. Caching results to reduce recalculations. Process tasks in parallel to improve execution efficiency. Optimize string concatenation, use built-in functions instead. Use built-in functions instead of custom functions.

The caching strategy in GolangAPI can improve performance and reduce server load. Commonly used strategies are: LRU, LFU, FIFO and TTL. Optimization techniques include selecting appropriate cache storage, hierarchical caching, invalidation management, and monitoring and tuning. In the practical case, the LRU cache is used to optimize the API for obtaining user information from the database. The data can be quickly retrieved from the cache. Otherwise, the cache can be updated after obtaining it from the database.

In PHP development, the caching mechanism improves performance by temporarily storing frequently accessed data in memory or disk, thereby reducing the number of database accesses. Cache types mainly include memory, file and database cache. Caching can be implemented in PHP using built-in functions or third-party libraries, such as cache_get() and Memcache. Common practical applications include caching database query results to optimize query performance and caching page output to speed up rendering. The caching mechanism effectively improves website response speed, enhances user experience and reduces server load.

Using Redis cache can greatly optimize the performance of PHP array paging. This can be achieved through the following steps: Install the Redis client. Connect to the Redis server. Create cache data and store each page of data into a Redis hash with the key "page:{page_number}". Get data from cache and avoid expensive operations on large arrays.

First you need to set the system language to Simplified Chinese display and restart. Of course, if you have changed the display language to Simplified Chinese before, you can just skip this step. Next, start operating the registry, regedit.exe, directly navigate to HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlNlsLanguage in the left navigation bar or the upper address bar, and then modify the InstallLanguage key value and Default key value to 0804 (if you want to change it to English en-us, you need First set the system display language to en-us, restart the system and then change everything to 0409) You must restart the system at this point.

Yes, Navicat can connect to Redis, which allows users to manage keys, view values, execute commands, monitor activity, and diagnose problems. To connect to Redis, select the "Redis" connection type in Navicat and enter the server details.

1. First, double-click the [This PC] icon on the desktop to open it. 2. Then double-click the left mouse button to enter [C drive]. System files will generally be automatically stored in C drive. 3. Then find the [windows] folder in the C drive and double-click to enter. 4. After entering the [windows] folder, find the [SoftwareDistribution] folder. 5. After entering, find the [download] folder, which contains all win11 download and update files. 6. If we want to delete these files, just delete them directly in this folder.
