search
HomeBackend DevelopmentPHP TutorialIntroduction to the principles of Redis sentinel mechanism (picture and text)

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

Configuration

Sentinel's core configuration

sentinel monitor mymaster 127.0.0.1 7000 2

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

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

当 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>

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

sentinel failover-timeout mymaster 180000

表示故障转移的时间。

技巧

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
This article is reproduced at:思否 segmentfault. If there is any infringement, please contact admin@php.cn delete
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools