Home Backend Development PHP Tutorial Introduction to the principles of Redis sentinel mechanism (picture and text)

Introduction to the principles of Redis sentinel mechanism (picture and text)

Feb 26, 2019 am 09:57 AM
php redis

The content of this article is to introduce the sentinel mechanism of Redis, so that everyone can understand the principle of the sentinel mechanism and how to implement it. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Introduction to the principles of Redis sentinel mechanism (picture and text)

Overview

Redis replication has a shortcoming. When the host Master goes down, we need Solve the switch manually, such as using slaveof no one. In fact, master-slave replication has not been implemented. High availability focuses on backup machines and uses the redundancy of the system in the cluster. When a machine in the system is damaged, other backup machines can quickly take over it to start the service.

Problems with master-slave replication

Introduction to the principles of Redis sentinel mechanism (picture and text)

Once the master node goes down and the write service cannot be used, You need to manually switch, reselect the master node, and manually set the master-slave relationship.

So how to solve it? If we have a monitoring program that can monitor the status of each machine and make timely adjustments, turning manual operations into automatic ones. The emergence of Sentinel is to solve this problem.

The principle and implementation of the sentinel mechanism

Redis Sentinel

Redis Sentinel is a distributed architecture that contains several Sentinel nodes and Redis data nodes. Each Sentinel node monitors the data node and other Sentinel nodes. When it finds that the node is unreachable, it will mark the node offline. If the identified master node is the master node, it will also "negotiate" with other Sentinel nodes. When most Sentinel nodes believe that the master node is unreachable, they will elect a Sentinel node to complete automatic failover and at the same time Notify the Redis application side of this change in real time. The entire process is completely automatic and does not require manual intervention, so this solution effectively solves the high availability problem of Redis.

As shown in the figure:


Introduction to the principles of Redis sentinel mechanism (picture and text)

Basic failover process

1) The master node fails. At this time, the two slave nodes lose connection with the master node, and the master-slave replication fails.

Introduction to the principles of Redis sentinel mechanism (picture and text)

2) Each Sentinel node discovers that the master node has failed through regular monitoring

Introduction to the principles of Redis sentinel mechanism (picture and text)

3) Multiple Sentinel The nodes agree on the failure of the primary node and elect one of the nodes as the leader responsible for failover.

Introduction to the principles of Redis sentinel mechanism (picture and text)

#4) The Sentinel leader node performed failover. The entire process is basically the same as our manual adjustment, but it is completed automatically.


Introduction to the principles of Redis sentinel mechanism (picture and text)

5) After the failover, the entire Redis Sentinel structure re-elected a new master node.

Introduction to the principles of Redis sentinel mechanism (picture and text)

Instance

Use docker to create the following redis container
redis-sentinel1    172.10.0.9    22530 -> 22530    sentinel
redis-sentinel2    172.10.0.10    22531 -> 6379    sentinel
redis-sentinel3    172.10.0.11    22532 -> 6379    sentinel
redis-master2    172.10.0.5    6383  -> 6379    Master
redis-slave2    172.10.0.6    6384  -> 6379    Slave
redis-slave3    172.10.0.7    6385  -> 6379    Slave
Copy after login

Configuration

Sentinel's core configuration

sentinel monitor mymaster 127.0.0.1 7000 2
Copy after login

The name, IP and port of the monitored master node. The last 2 means how many Sentinels are found. problem, failover will occur. For example, if the configuration is 2, it means that at least 2 Sentinel nodes believe that the master node is unreachable, so this unreachable determination is objective. The smaller the setting, the looser the conditions for reaching the offline level, and vice versa. It is generally recommended to set it to half the Sentinel node plus 1.

sentinel down-after-millseconds mymaster 30000
Copy after login

This is the timeout (unit: milliseconds). For example, when you go to ping a machine and you still can't ping it after a long time, then it is considered to be a problem.

sentinel parallel-syncs mymaster 1
Copy after login

当 Sentinel 节点集合对主节点故障判定达成一致时,Sentinel 领导者节点会做故障转移操作,选出新的主节点,原来的从节点会向新的主节点发起复制操作,parallel-syncs 就是用来限制在一次故障转移之后,每次向新的主节点发起复制操作的从节点个数,指出 Sentinel 属于并发还是串行。1代表每次只能复制一个,可以减轻 Master 的压力。

Introduction to the principles of Redis sentinel mechanism (picture and text)

sentinel auth-pass <master-name> <password></password></master-name>
Copy after login

如果 Sentinel 监控的主节点配置了密码,sentinel auth-pass 配置通过添加主节点的密码,防止 Sentinel 节点对主节点无法监控。

sentinel failover-timeout mymaster 180000
Copy after login

表示故障转移的时间。

技巧

1)Sentinel 节点不应该部署在一台物理“机器”上。

这里特意强调物理机是因为一台物理机做成了若干虚拟机或者现今比较流行的容器,它们虽然有不同的 IP 地址,但实际上它们都是同一台物理机,同一台物理机意味着如果这台机器有什么硬件故障,所有的虚拟机都会受到影响,为了实现 Sentinel 节点集合真正的高可用,请勿将 Sentinel 节点部署在同一台物理机器上。

2)部署至少三个且奇数个的 Sentinel 节点。

3个以上是通过增加 Sentinel 节点的个数提高对于故障判定的准确性,因为领导者选举需要至少一半加1个节点,奇数个节点可以在满足该条件的基础上节省一个节点。

【相关文章】

Redis主从复制的原理介绍(图文)

以上就是本篇文章的全部内容,希望能对大家的学习有所帮助。更多精彩内容大家可以关注php中文网相关教程栏目!!!

The above is the detailed content of Introduction to the principles of Redis sentinel mechanism (picture and text). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

PHP: Is It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

See all articles