Redis's low-cost, high availability design has taken nearly a year to sort out the list of Java core knowledge

Posted by psychowolvesbane on Mon, 06 Sep 2021 03:23:37 +0200

This paper mainly introduces the design of redis high availability scheme using official sentinel.

Redis Sentinel

Sentinel introduction

Sentinel is a highly available solution officially provided by redis for clusters. In actual projects, sentinel can be used for automatic redis failover to reduce the workload of manual intervention.

In addition, sentinel also provides the client with the notification of monitoring messages, so that the client can judge the state of the server according to the message type and do the corresponding adaptation operation.

The following is a list of Sentinel's main functions:

  • Monitoring: Sentinel continuously checks the status of master and slave in the cluster to determine whether they are alive.

  • Notification: when a redis instance is found dead, Sentinel can notify the system administrator or other program scripts through the API.

  • Automatic failover: if a master hangs up, sentinel immediately starts failover and promotes a slave to master. Other slave configurations point to the new master.

  • Configuration provider: sentinel notifications are valid and trusted for clients. The client will connect sentinel to request the address of the current master. In case of failure, sentinel will provide a new address to the client.

Sentinel configuration

Sentinel is essentially just a redis server running in a special mode. It provides services through different configurations.

sentinel.conf configuration:

// [monitor name] [ip] [port] [how many sentinel consents before failover]

sentinel monitor mymaster 127.0.0.1 6379 2

// [monitoring name] [if the master does not respond to the ping command after milliseconds, it is considered that the master is in the subjective offline state]

sentinel down-after-milliseconds mymaster 60000

// [failover timeout]

sentinel failover-timeout mymaster 180000

//[when performing failover, how many slave servers can synchronize the new master server at the same time]

sentinel parallel-syncs mymaster 1 

sentinel needs to use redis2.8 or above. The startup is as follows:

redis-sentinel sentinel.conf 

Sentinel will:

  • Send the info command to the monitored master once every 10 seconds to obtain the current information of the master according to the reply.

  • Send a PING command to all redis servers, including sentinel, once every 1 second, and judge whether the server is online by replying.

  • Once every 2 seconds, send a message containing the current sentinel and master information to all monitored master and slave servers.

    In addition, it is recommended that sentinel should have at least three instances and configure two instances to agree to the transfer. 5 instances, 3 instances are configured, and so on.

3 ways to receive failover messages

Once the redis server sends a fault, sentinel votes for a new master through the raft algorithm. The failover process can obtain / subscribe to receive event messages through Sentinel's API. Redis series of interview questions and answers have been sorted out. I am concerned about the official account Java technology stack reply: interview, free access.

Script receiving

//During failover, you can specify a "notification" script to inform the system administrator of the current situation of the cluster.

//The maximum time allowed for script execution is 60 seconds. If it times out, the script will be terminated (KILL)

sentinel notification-script mymaster /var/redis/notify.sh 

//After the failover period, configure the script to notify the client

sentinel client-reconfig-script mymaster /var/redis/notifyReconfig.sh 

Client direct reception

Sentinel's failover message notification uses redis publishing subscription, that is, all event information generated during failover is published through the channel.

For example, if we add a slave server, sentinel will publish the message of adding a slave to the "+ Slave" channel after listening. The client only needs to subscribe to the "+ Slave" channel to receive the corresponding message.

The message format is as follows: [instance type] [event server name] [server ip] [server port] @ [master name] [ip] [port]

<instance-type> <name> <ip> <port> @ <master-name> <master-ip> <master-port> 

Example of notification message format:

*          //Subscription type, * that is, subscribe to all event messages.

-sdown     //Message type

slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381 

Example of subscription message:

using (RedisSentinel rs = new RedisSentinel(CurrentNode.Host, CurrentNode.Port))

{


# summary

If you choose IT The industry is not firmly going forward. There must be no problem in this direction. This is a high salary industry, but the high salary is obtained by studying hard. This time I P8 Some learning notes used by the boss( pdf)It's all sorted out in this article. Please be sure if you need it**Like to share this article**

**[CodeChina Open source project: [first tier big factory] Java Analysis of interview questions+Core summary learning notes+Latest explanation Video]](https://codechina.csdn.net/m0_60958482/java-p7)**

**<Java Comprehensive analysis of intermediate and advanced core knowledge**

![](https://img-blog.csdnimg.cn/img_convert/d7ad292b656a721579600eadc17f66b9.png)

**Xiaomi shopping mall project actual combat, don't worry that there is no actual combat project in the interview:**



**[CodeChina Open source project: [first tier big factory] Java Analysis of interview questions+Core summary learning notes+Latest explanation Video]](https://codechina.csdn.net/m0_60958482/java-p7)**

**<Java Comprehensive analysis of intermediate and advanced core knowledge**

[External chain picture transfer...(img-S4uhqJtl-1630838424806)]

**Xiaomi shopping mall project actual combat, don't worry that there is no actual combat project in the interview:**

![](https://img-blog.csdnimg.cn/img_convert/ce5f65759882b907654d6d54eecc91da.png)

Topics: Java Database Redis Back-end Programmer