Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Explanation of Redis Sentinel Mechanism

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Redis Sentinel Mechanism explanation". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Redis Sentinel Mechanism explanation".

Sentinel function

Sentinel is a very important component of Redis cluster architecture, and its main functions are as follows:

Cluster monitoring: responsible for monitoring whether redis master and slave processes are normal

Message notification: if a redis instance fails, the sentry is responsible for sending a message to the administrator as an alarm notification.

Failover: if the master node dies, it will be automatically transferred to the slave node

Configuration Center: if a failover occurs, notify the client client of the new master address

The core knowledge of Sentinel

During failover, it requires the consent of most sentinels to determine whether a master node is down, which involves the issue of distributed election.

The Sentinel needs at least three examples to ensure his robustness.

The deployment architecture of Sentinel + redis master and slave does not guarantee zero data loss, but can only ensure the high availability of redis clusters.

Sdown and odown

Two failed states of sdown and odown

Sdown is a subjective downtime. If a sentry thinks a master is down, it is a subjective downtime.

Odown is an objective downtime. If the number of sentinels in quorum thinks that a master is down, then it is an objective downtime.

The condition agreed by sdown: if a sentry ping a master, after exceeding the millisecond specified by is-master-down-after-milliseconds, the master is considered to be down.

Odown reached a condition: if a sentry receives a specified number of quorum within a specified period of time, and other sentinels also think that the master is down, then it is considered to be odown, and master is objectively considered to be down.

Quorum and majority

Quorum: confirm the minimum number of sentinels in odown

Majority: the minimum number of sentinels authorized for master-slave handover

For each Sentinel to switch between active and standby, first the number of quorum Sentinels think odown, then elect a Sentinel to switch, and this Sentinel must also get the privilege of majority Sentinel before switching.

If quorummajority, then the number of quorum Sentinels must be authorized, for example, 5 Sentinels, quorum is 5, then all 5 Sentinels must agree to authorize. (who listens to whom more)

Why does the Sentinel have at least 3 nodes?

The Sentinel cluster must deploy more than two nodes. If the Sentinel cluster deploys only 2 sentinel instances, then its majority is 2 (majority=3,4 majority=2 of 2 majority=2,3 majority=2,5). If one of the sentinels is down, the condition of majority > = 2 cannot be met, and master-slave switching cannot be performed in the event of master failure.

working principle

Each Sentienl sends a ping command to Master,Slave and other Sentinel instances that he knows at a frequency of once per second.

If the time from the last valid reply to a ping command to an instance exceeds the value indicated by the down-after-milliseconds option, the instance will be marked as subjective downtime by Sentinel

If a master is marked as a subjective outage, all sentinel monitoring the master should confirm that the Master has indeed entered the subjective downtime at a frequency of once per second.

When a sufficient number of Sentinel (greater than or equal to the value specified in the profile) confirms that master has indeed entered a subjective downtime state within a specified time range, the master will be marked as objective.

Under normal circumstances, each Sentinel sends INFO commands to all its consistent master,slave at a frequency of 1 / 10 seconds.

When master is marked as an objective downtime by Sentinel, the frequency of Sentinel sending INFO commands to all slave of offline master will be changed from 1 / 10 s to 1 / s.

If there is not enough Sentinel to agree that master has gone offline, the objective downtime status of master will be removed; if master returns a valid reply from Sentinel's ping command, the subjective downtime status of master will be removed.

Configuration of Sentinel Mode

First configure the master and slave server of redis, and modify the redis.conf file as follows

# make the Redis server accessible across the network

Bind 0.0.0.0

# set password

Requirepass "123456"

# specify master server. Note: the configuration of slaveof only configures slave server, but master server does not need to be configured.

Slaveof 192.168.11.128 6379

# password of master server. Note: the configuration of slaveof is only to configure the slave server, but not the master server.

Masterauth 123456

The above content is mainly to configure the Redis server. The slave server has one more slaveof configuration and password than the master server.

Configure 3 sentinels, each of which is the same. There is a sentinel.conf file in the Redis installation directory, and a copy of copy will be modified.

# prohibit protection mode

Protected-mode no

# configure the listening master server, where sentinel monitor represents the monitoring server, and mymaster represents the name of the server, which can be customized. 192.168.11.128 represents the monitoring master server, 6379 represents the port, and 2 represents the failover operation only when two or more sentinels consider the master server unavailable.

Sentinel monitor mymaster 192.168.11.128 6379 2

# sentinel author-pass defines the password of the service. Mymaster is the name of the service and 123456 is the password of the Redis server.

# sentinel auth-pass

Sentinel auth-pass mymaster 123456

Start the server and Sentinel and enter the src directory of the Redis installation directory

# start the Redis server process

. / redis-server.. / redis.conf

# start the Sentinel process

/ redis-sentinel.. / sentinel.conf pay attention to the startup sequence: first, the Redis service process of the host (192.168.11.128), then start the service process of the cluster machine, and finally start the service process of three Sentinels using Sentinel mode in Java / * *

* Test Redis Sentinel mode

* @ author liu

, /

Public class TestSentinels {

@ SuppressWarnings ("resource")

@ Test

Public void testSentinel () {

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig ()

JedisPoolConfig.setMaxTotal (10)

JedisPoolConfig.setMaxIdle (5)

JedisPoolConfig.setMinIdle (5)

/ / Sentinel information

Set sentinels = new HashSet (Arrays.asList ("192.168.11.128VR 26379")

"192.168.11.129VR 26379", "192.168.11.130RU 26379"))

/ / create a connection pool

JedisSentinelPool pool = new JedisSentinelPool ("mymaster", sentinels,jedisPoolConfig, "123456")

/ / get the client

Jedis jedis = pool.getResource ()

/ / execute two commands

Jedis.set ("mykey", "myvalue")

String value = jedis.get ("mykey")

System.out.println (value)

}

} Thank you for your reading. the above is the content of "explanation of Redis Sentinel Mechanism". After the study of this article, I believe you have a deeper understanding of the explanation of Redis Sentinel Mechanism, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report