Redis Sentinel provides Redis with high availability and can be used to automatically recover Redis service unavailability without human intervention in some cases.

features

Sentinels provide the following capabilities:

  • Monitoring: Periodically check the information of the Master and Slave nodes of Redis
  • Notification: The API notifies the system administrator that a monitored Redis instance has encountered some kind of problem
  • Automatic fault recovery: If a Master node does not work, Sentinel can enable failover to promote a Slave node to Master. Sentinel reconfigures the Slave node and informs the clients connected to Sentinel that the Master address has changed
  • Configuration provides: the client connects to Sentinel to obtain the current Redis Master address

Sentinels are distributed and designed to cooperate with each other. Running multiple sentinels has the following advantages:

  • Fault detection is that multiple sentries agree that the Master is unavailable, and then determine that the Master is unavailable. If the number of nodes is small, miscarriage of justice may occur
    • Multiple Sentinels found problems with the Master node
    • To elect a Sentinel as Master, the Sentinel node is usually found to be unavailable to the Master. The Sentinel Master has the function of promoting the Slave to Master
    • Select a Slave as the new Master
    • Notify other slaves of the new Master address
    • Notify customer of master/slave change
    • After the old Master is detected to be alive again, it becomes the Slave of the new Master
  • Even if a few Sentinels fail, the remaining Sentinel nodes still work, enhancing the robustness of the system.

A Sentinel cluster can monitor multiple Redis primary and secondary clusters

Three scheduled Tasks

1 Perform Info information for the Master and Slave every 10s for each Sentinel

  • Discovering the Slave node
  • Confirm the master/slave relationship

2 Every 2s for each Sentinel, the information is exchanged through the Channel of the Master node (pub/sub)

  • The new Sentinel node is automatically registered for this channel by interacting with the __ sentinel __: Hello channel
  • The “view” of a node is exchanged with information about itself

3 Ping other Sentinel and Redis nodes every 1S

  • Heartbeat detection, failure judgment basis

The client connects to the Sentinel principle

Redis Sentinel high availability refers to high availability on the server side. If our Master node directly connected to Redis fails, then our client becomes unavailable.

What we need is a combination of high availability on the server and high availability on the client.

1 Obtain all Sentinel nodes and traverse to obtain an available Sentinel node

2 Run the Sentinel get-master-addr-by-name MasterName command on an available Sentinel node to return master node information, including the address and port number

3 After obtaining the Master node, run role or role replication to check whether it is the Master node

4 If the Master node changes, Sentinel notifies the client that the Master node information has changed. The client receives the change through a Redis Channel

Start the Sentinel

The default port on which Sentinel listens is TCP 26379. There are two ways to run Sentinel:

redis-sentinel /path/to/sentinel.conf
redis-server /path/to/sentinel.conf --sentinel
Copy the code

When you download Redis, you usually have the sentinel.conf configuration file, which contains the sentinel configuration along with the documentation.

configuration

The general Redis Sentinel configuration is as follows:

Sentinel = 'Master'; sentinel = 'Master'
Sentinel monitor 
      
        < IP > 
        
        
       
      
# quorum requires several sentinels to confirm that the master is unavailable when the master is inaccessibleSentinel Monitor MyMaster 127.0.0.1 6379 2
       
       
      

# down-after-milliseconds: Sentinel failed to ping myMaster for 60 seconds, indicating that mymaster was unavailable
sentinel down-after-milliseconds mymaster 60000

If no failover operation is triggered within this period, the sentinel considers the failoer to have failed
sentinel failover-timeout mymaster 180000

# When a slave is promoted to the new master, several slaves will update their configurations at the same time. The smaller the value, the better. If the value is too high, the master may block.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 5Copy the code

Sentinel automatically updates the configuration file when a Slave is discovered, promoted to Master, or added to a Sentinel node.

Other configurations:

# Enable daemon mode
daemonize yes

# Disable protected mode
protected-mode no

Make Sentinel work only in/TMP directory to reduce security risks
dir "/tmp"

If you want to set the password for sentinel to connect to master, you must set the password for sentinel to connect to master
sentinel auth-pass <master-name> <password>
Copy the code

Spring Redis configuration

1 Specify the address of sentinel in the configuration file. If there are multiple sentinel nodes, separate sentinel nodes with commas (,)

Spring. Redis. Sentinel. Master = mymaster spring. Redis.. Sentinel nodes = 192.168.26.129:26379 spring. Redis. Password = 123456Copy the code

If you don’t want to use property configuration you can create it in your own code:

/**
 * Jedis
 */
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
  .master("mymaster")
  .sentinel("127.0.0.1", 26379)
  .sentinel("127.0.0.1", 26380);
  return new JedisConnectionFactory(sentinelConfig);
}
Copy the code

2. Configure the RedisTemplate sequencer, otherwise it is easy to appear some strange characters at the beginning of the key. Such as:

 @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        return redisTemplate;
    }
Copy the code

3 inject RedisTemplate to use.

The last

This concludes the Redis Sentinel configuration and use.