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 by Michael

I started my website in 2017 to help people learn more about information technology in order to help them become more successful in their careers. I earned my bachelor's degree from New York University, have been working in cyber security since 2012, and live in the Washington D.C. Metro area. I have found running a blog to be one of the greatest assets in helping my career and have found it could even bring in a few extra bucks each month. Starting a blog is easy and rewarding. To keep my knowledge current, I take courses on Udemy and Treehouse. By using my links, you can get a discount on both.

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *

%d bloggers like this: