Home >Database >Redis >Let's talk about the Sentinel mechanism in Redis and introduce its usage!

Let's talk about the Sentinel mechanism in Redis and introduce its usage!

青灯夜游
青灯夜游forward
2021-12-15 10:14:032022browse

This article will take you to understand the Sentinel mechanism in Redis and introduce two ways to run sentinel. I hope it will be helpful to you!

Let's talk about the Sentinel mechanism in Redis and introduce its usage!

1. Overview

Redis-Sentinel is the official recommended high availability (HA) solution for Redis Solution, when using Redis as a high-availability solution for Master-slave, if the master goes down, Redis itself (including many of its clients) does not implement automatic master-slave switching, and Redis-sentinel itself is also an independent operation. process, it can monitor multiple master-slave clusters and perform self-understanding switching after discovering that the master is down. [Related recommendations: Redis Video Tutorial]

Its main functions are as follows:

  • Monitor whether redis follows the It is expected to run well;
  • If it is found that a certain redis node is running abnormally, it can notify another process (such as its client);
  • It can automatically switch. When a master node is unavailable, one of the master's multiple slaves (if there is more than one slave) can be elected as the new master. The other slave nodes will change the address of the master they follow to be promoted to The new address of the master's slave.

2. Sentinel supports cluster

Obviously, using only a single sentinel process to monitor the redis cluster is unreliable. After the sentinel process crashes (sentinel itself also has a single-point-of-failure), the entire cluster system will not be able to operate as expected. Therefore, it is necessary to cluster sentinel, which has several advantages:

  • Even if some sentinel processes are down, the active and backup switching of the redis cluster can still be performed;
  • If there is only one Sentinel process, if this process runs incorrectly, or the network is blocked, then the active/standby switch of the redis cluster will not be possible (single point problem);
  • If there are multiple sentinels, the redis client can connect at will Any sentinel to obtain information about the redis cluster.

3. Sentinel Version

The current latest stable version of Sentinel is called Sentinel 2 (to distinguish it from the previous Sentinel 1 ). Released together with the redis2.8 installation package. After installing Redis2.8, you can find the Redis-sentinel startup program in redis2.8/src/.

Strongly recommended: If you are using redis2.6 (sentinel version is sentinel 1), you'd better use redis2.8 version of sentinel 2, because sentinel 1 has many bugs. It is officially deprecated, so it is strongly recommended to use redis2.8 and sentinel 2.

4. Running Sentinel

There are two ways to run Sentinel:

First type

redis-sentinel /path/to/sentinel.conf

Second type

redis-server /path/to/sentinel.conf --sentinel

Both of the above two methods must specify a sentinel configuration file sentinel.conf. If not specified, Sentinel will not start. Sentinel listens to port 26379 by default, so you must make sure that the port is not occupied by other processes before running it.

5. Sentinel configuration

The Redis source package contains a sentinel.conf file as the sentinel configuration file. Configuration The file comes with explanations about each configuration item. Typical configuration items are as follows:

sentinel monitor mymaster 127.0.0.1 6379 2

sentinel down-after-milliseconds mymaster 60000 sentinel failover -timeout mymaster 180000 sentinel parallel-syncs mymaster 1 sentinel monitor resque 192.168.1.3 6380 4 sentinel down-after-milliseconds resque 10000 sentinel failover-timeout resque 180000 sentinel parallel-syncs resque 5

The above configuration The item configures two masters named mymaster and resque. The configuration file only needs to configure the master information. There is no need to configure the slave information, because the slave can be automatically detected (the master node will have messages about the slave). It should be noted that the configuration file will be dynamically modified while sentinel is running. For example, when a master-slave switchover occurs, the master in the configuration file will be modified to another slave. In this way, if sentinel is restarted later, it can restore the status of the redis cluster it previously monitored based on this configuration.

Next we will explain the above configuration items line by line:

sentinel monitor mymaster 127.0.0.1 6379 2

This line represents that the name of the master monitored by sentinel is mymaster, and the address is 127.0.0.1:6379. What does the last 2 at the end of the line mean? We know that the network is unreliable. Sometimes a sentinel will mistakenly think that a master redis is dead due to network congestion. When the sentinel is clustered, the solution to this problem becomes very simple. It only requires multiple sentinels to communicate with each other. Communicate to confirm whether a master is really dead. This 2 means that when there are two sentinels in the cluster that think the master is dead, the master can truly be considered unavailable. (Each sentinel in the sentinel cluster also communicates with each other through the gossip protocol).

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of Let's talk about the Sentinel mechanism in Redis and introduce its usage!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete