Home » Java » Enterprise Java » Spring Data Redis: High-Availability with Sentinel

About Michael Good

Michael Good
Michael is a software engineer located in the Washington DC area that is interested in Java, cyber security, and open source technologies. Follow his personal blog to read more from Michael.

Spring Data Redis: High-Availability with Sentinel

1. Overview

For high-availability with Redis, we can use Spring Data Redis’ support for Redis Sentinel. With Sentinel, we can create a Redis deployment that automatically resists certain failures.

Redis Sentinel also provides other collateral tasks such as monitoring, notifications and acts as a configuration provider for clients.

At a high level, Sentinel’s capabilities are:

  • Automated failover. When a master is not working as expected, Sentinel starts a failover process for us where a slave is promoted to master. Additionally, the other slaves are reconfigured to use the new master and the applications using the Redis server are informed about the new address to use.
  • Configuration source. When a failover happens, Sentinels will report the new address. This is because Sentinel functions as a source of authority for clients. When clients do service discovery, they connect to Sentinels to request the address of the current Redis master responsible for a given service.
  • Monitoring. Sentinel periodically checks if our master and slave instances are working as they are intended to.
  • Notifying. Sentinel can be configured to notify a variety of targets when an error occurs with one of the Redis instances. These targets include other applications, a sysadmin, or an API.

2. How to Run Sentinel

A stable release of Sentinel has shipped with Redis since Redis 2.8.

Starting Sentinel is very easy. When we reviewed Spring Data Redis (with Spring Boot) in my previous article, we installed Redis using homebrew on Mac. This command allows us to run Sentinel with that installation:

redis-sentinel /path/to/sentinel.conf

If we are using the redis-sentinel executable (or if have a symbolic link using that name to the redis-server executable), we can run Sentinel with the above command as well.

Alternatively, we can use the redis-server executable and start it in Sentinel mode, like this:

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

3. Key Concepts to Know Before Deploying Sentinel

Some concepts we should review before deploying to Sentinel include:

  1. We require at least three Sentinel instances for a durable Redis deployment.
  2. We should place the three Sentinel instances into computers or virtual machines that are believed to fail independently rather than together. For instance, this could mean different availability zones.
  3. Redis uses asynchronous replication and therefore does not guarantee that received writes are kept during failures, even when using Sentinel. However, we can deploy Sentinel that mitigates the amount of time that writes can be lost.
  4. Any high-availability setup must be tested periodically and Sentinel is no different. We need to test in both development environments and in our production environments. By planning and testing for failure, we limit our failures.

4. Configuration in Spring Data

When we use a Sentinels-based configuration, we do not provide the Redis host/port information to Spring Data Redis. Instead we provide the property for the master server and a list of Sentinel URLs. Each Sentinel process has its own configuration file that lists the master Redis server, such as:

sentinel monitor themaster 127.0.0.1 6379 2
sentinel down-after-milliseconds themaster 60000
sentinel failover-timeout themaster 180000
sentinel parallel-syncs themaster 1

Once we have configured our master, slaves and Sentinels, we need to change the spring data redis configuration in our application to work with the sentinels.

4.1 Java Configuration

The Java configuration can be done using both Jedis and Lettuce:

/**
 * Jedis
 */
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
  .master("themaster")
  .sentinel("127.0.0.1", 26579)
  .sentinel("127.0.0.1", 26580);
  return new JedisConnectionFactory(sentinelConfig);
}

/**
 * Lettuce
 */
@Bean
public RedisConnectionFactory lettuceConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
  .master("themaster")
  .sentinel("127.0.0.1", 26579)
  .sentinel("127.0.0.1", 26580);
  return new LettuceConnectionFactory(sentinelConfig);
}

4.2 Properties Configuration

A ProperySource, such as application.properties, can be used for the configuration. For example, if we use a localhost:

spring.redis.sentinel.master= themaster # Name of our Redis server.
spring.redis.sentinel.nodes= localhost:26579, localhost:26580, localhost:26581 # Comma-separated list of host:port pairs.

5. Conclusion

Today we reviewed how high-availability can be achieved with Redis by using Sentinel and how Spring Data Redis supports this is in our Spring applications. For more information about Sentinel, the Redis website is a good source.

On my site, there’s also information starting with Spring Data Redis and Spring Boot and several articles about the Spring Framework in general.

Published on Java Code Geeks with permission by Michael Good, partner at our JCG program. See the original article here: Spring Data Redis: High-Availability with Sentinel

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of