Spring Data Redis with Sentinels

Redis Sentinel is a highly available deployment setup for Redis. In this post I will setup a Redis Sentinel Deployment with the following configuration.

  • 1 x Master
  • 2 x Slaves
  • 3 x Sentinels

First I want to thank ServiceStack and mustafaileri for creating the Deployment Scripts and making them available on GitHub.

Now let’s get started.

Deploying Redis

I am going to use start-all.sh script in redis-config. Before running the command make sure you select cd into the correct Directory for your Operating System first.

Use the below command to see the instances running.

You will see six instances with three sentinels running on your computer as shown below.

Use redis-cli to connect to a Sentinel. I installed redis-cli using npm.

Use SENTINEL Commands to see who the master is.

The Master is running on 6381 on my machine.

Now update your application.properties file with correct values for spring.redis.sentinel.master and spring.redis.sentinel.nodes.

Now start your Spring Application and hit the exposed endpoint with the browser.

You can check the is the key is stored on Redis using redis-cli Commands shown below.

The output of the command will look like this.

Now let’s kill the Master to see if a Slave gets promoted to a Master.

Then use redis-cli to see if the Deployment works as expected.

Looks like the Slave on Port 6380 got promoted to a Master.

You should be able to kill one of the Sentinels and still get this to work as quorum is set to 2.

Your Spring Application should keep working despite the Infrastructure change.

Redis Sentinels on Docker

I will use docker-compose do the deployment as indicated in redis-cluster-with-sentinel.

Find the IP Addresses of the Sentinels by using docker inspect.

Update the application.properties with the correct values as we did before.

Now Build the Docker Image of the Spring Application and run it. Remember to build the Spring Application first before building its Docker Image as the jar file has to be there for the Docker Image.

In addition to this you can use docker-compose pause or docker pause to pause the current master and pause it to monitor whether a Slave takes over.

Source Code

My Spring Source Code is available here. It is under MIT License.

You may also like

Leave a Reply

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