Spring Boot 3,Redis Sentinel,Lettuce client and Docker Compose for High availability

Hüseyin Tugay Yeşilyurt
3 min readFeb 5, 2023

What is Redis?

Redis (“REmote DIctionary Service”) is an open-source key-value database server.

The most accurate description of Redis is that it’s a data structure server. This specific nature of Redis has led to much of its popularity and adoption amongst developers.

Redis Sentinel

What is Redis Sentinel?

Redis Sentinel is the high-availability solution for open-source Redis server. It provides monitoring of all Redis nodes and automatic failover should the master node become unavailable. This guide provides a sample configuration for a three-node Redis cluster. For additional details, see the official documentation here.

  • Monitoring: This is basically when sentinel checks if the master and slave instances are working as expected.
  • Notification: This is when the sentinel notifies other programs or other system administrators via an API when there is something wrong with the monitoring instances.
  • Automatic Failover: On a master failure, the sentinel promotes one of the slaves to become the new master and then makes the other additional slaves use the new master.

Configuration

We will run 1 master,1 slave and 3 sentinel instance.

What is Quorum ?

  • The quorum is the number of Sentinels that need to agree about the fact the master is not reachable, in order to really mark the master as failing, and eventually start a failover procedure if possible.
  • However the quorum is only used to detect the failure. In order to actually perform a failover, one of the Sentinels need to be elected leader for the failover and be authorized to proceed. This only happens with the vote of the majority of the Sentinel processes.
  • REDIS_SENTINEL_QUORUM: Number of Sentinels that need to agree about the fact the master is not reachable. Default: 2.

Here is docker-compose file

version: '3.8'
services:
redis-master:
container_name: redis-master
image: 'bitnami/redis:latest'
environment:
- REDIS_REPLICATION_MODE=master
- REDIS_PASSWORD=redispassword
ports:
- "6379:6379"
redis-slave:
container_name: slave-redis
image: 'bitnami/redis:latest'
environment:
- REDIS_REPLICATION_MODE=slave
- REDIS_MASTER_HOST=redis-master
- REDIS_MASTER_PASSWORD=redispassword
- REDIS_PASSWORD=redispassword
ports:
- "7000:6379"
depends_on:
- redis-master
redis-sentinel-1:
image: 'bitnami/redis-sentinel:latest'
container_name: sentinel-1
environment:
- REDIS_MASTER_SET=mymaster
- REDIS_MASTER_HOST=127.0.0.1
- REDIS_MASTER_PASSWORD=redispassword
- REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=10000
depends_on:
- redis-master
- redis-slave
ports:
- "26379:26379"
redis-sentinel-2:
image: 'bitnami/redis-sentinel:latest'
container_name: sentinel-2
environment:
- REDIS_MASTER_SET=mymaster
- REDIS_MASTER_HOST=127.0.0.1
- REDIS_MASTER_PASSWORD=redispassword
- REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=10000
depends_on:
- redis-master
- redis-slave
ports:
- "26380:26379"
redis-sentinel-3:
image: 'bitnami/redis-sentinel:latest'
container_name: sentinel-3
environment:
- REDIS_MASTER_SET=mymaster
- REDIS_MASTER_HOST=127.0.0.1
- REDIS_MASTER_PASSWORD=redispassword
- REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=10000
depends_on:
- redis-master
- redis-slave
ports:
- "26381:26379"

Just use the script for run

  • docker-compose up -d

Lettuce Client and Spring Boot

Lettuce is a scalable thread-safe Redis client for synchronous, asynchronous and reactive usage. Multiple threads may share one connection if they avoid blocking and transactional operations such as BLPOP and MULTI/EXEC. Lettuce is built with netty. Supports advanced Redis features such as Sentinel, Cluster, Pipelining, Auto-Reconnect and Redis data models.

Configuration

Firstly,add the maven dependency of Spring Data Redis to my pom.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Then, continue with the YAML file and I add my connection information that I will use for configuration.

Before the spring boot 3 : @ConfigurationProperties(prefix = “spring.redis”)

After the spring boot 3: @ConfigurationProperties(prefix = “spring.data.redis”)

server:
port: 2000

spring:
application:
name: spring-redis-sentinel
data:
redis:
password: redispassword
sentinel:
master: mymaster
nodes:
- 127.0.0.1:26379
- 127.0.0.1:26380
- 127.0.0.1:26381
lettuce:
shutdown-timeout: 200ms

Create a connection configuration RedisConfig.java using LettuceConnectionFactory

ReadFrom

MASTER: Setting to read from the upstream only.

MASTER_PREFERRED: Setting to read preferred from the upstream and fall back to a replica if the master is not available.

UPSTREAM: Setting to read from the upstream only.

UPSTREAM_PREFERRED: Setting to read preferred from the upstream and fall back to a replica if the upstream is not available.

REPLICA_PREFERRED: Setting to read preferred from replicas and fall back to upstream if no replica is available.

REPLICA: Setting to read from the replica only.

ANY: Setting to read from any node.

We are using REPLICA_PREFERRED in here but this configuration can be important in production!

RedisUtil Configuration

Conclusion

Thank you for reading :)

You can find the full source code of this demo project on my github

Happy Coding!

--

--