{{announcement.body}}
{{announcement.title}}

High Availability With Redis Sentinel on Java

DZone 's Guide to

High Availability With Redis Sentinel on Java

Let's explore Redis Sentinel and see how to run it on Java.

· Database Zone ·
Free Resource

Image title

High availability with Redis Sentinel

What Is Redis Sentinel?

Availability is one of the most important qualities of any enterprise database. Users must have the guarantee that they can access the information and insights they require in order to excel at their jobs.

However, ensuring that databases are available when they need to be is easier said than done. The term “high availability” refers to a system that can operate continuously without failure for a length of time that is longer than average.

Redis Sentinel is a high availability solution for Redis, an open-source, in-memory data structure store that can be used as a non-relational key-value database. The goal of Redis Sentinel is to manage Redis instances via three different functions: monitoring your Redis deployment, sending notifications if something is amiss, and automatically handling the failover process by creating a new master node.

As a distributed system, Redis Sentinel is intended to run together with other Sentinel processes. This reduces the likelihood of false positives when detecting that a master node has failed, and also inoculates the system against the failure of any single process.

The creators of Redis Sentinel recommend that you have at least three Sentinel instances in order to have a robust Sentinel deployment. These instances should be distributed across computers that are likely to fail independently of each other, such as those that are located in different geographical areas.

You might also like:  Redis in a Microservices Architecture 

How to Run Redis Sentinel

Running Redis Sentinel will require one of two executables: either redis-sentinel or redis-server.

With the redis-sentinel executable, you can run Redis Sentinel with the following command:

redis-sentinel /path/to/sentinel.conf

where “/path/to/sentinel.conf” is the path to the Sentinel configuration file.

With the redis-server executable, you can run Redis Sentinel with the following command:

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

Note that in both cases, you must provide a link to the Sentinel configuration file. Using a configuration file is required when running Redis Sentinel in order to save the current state of the system in the event that it restarts.

An example Redis Sentinel configuration file looks as follows:

sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
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 5

In this example, the line “sentinel monitor <master-group-name> <ip> <port> <quorum>” defines a Sentinel master node called master-group-name on a given IP address and port number. The quorum argument is the number of Sentinel processes that must agree on the fact that the master node is not reachable.

The other lines define the following settings:

“sentinel down-after-milliseconds”: Defines the number of milliseconds after which the master node will be considered unreachable.

“sentinel failover-timeout”: Defines the amount of time after which a Sentinel process will try to vote for the failover of the master node.

“sentinel parallel-syncs”: Defines the number of slave nodes that can be reconfigured to use the same master node after a failover at the same time.

Connecting to Redis Sentinel on Java

The bad news for Java programmers is that Redis Sentinel isn’t compatible with Java out of the box. However, the good news is Redis Sentinel and Java can easily work together using frameworks such as Redisson, a Java client for Redis that uses many familiar constructs of the Java programming language. Redisson offers dozens of Java objects, collections, locks, and services implemented in a distributed fashion, allowing users to share them across different applications and servers.

The following code sample demonstrates how to get started using Redis Sentinel in Java. After setting up a configuration file and a Redisson client, the application performs a few basic operations to demonstrate the feasibility of using Redis with Java.

package redis.demo;
import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

/**
 * Redis Sentinel Java example
 *
 */
public class Application 
{
    public static void main( String[] args )
    {
        Config config = new Config();
        config.useSentinelServers()
              .addSentinelAddress("redis://127.0.0.1:6379")
              .setMasterName("myMaster");

        RedissonClient redisson = Redisson.create(config);

        // perform operations

        // implements java.util.concurrent.ConcurrentMap
        RMap<String, String> map = redisson.getMap("simpleMap");
        map.put("mapKey", "This is a map value");

        String mapValue = map.get("mapKey");
        System.out.println("stored map value: " + mapValue);

        // implements java.util.concurrent.locks.Lock
        RLock lock = redisson.getLock("simpleLock");
        lock.lock();

        try {
           // do some actions
        } finally {
           lock.unlock();
        }

        redisson.shutdown();
    }
}

It’s important to note that Redisson users must specify at least one Redis Sentinel server and at least one Redis master node. After starting up, Redisson continues to monitor the list of the available master and slave nodes in Redis Sentinel, as well as the Sentinel nodes. This means that there’s no need for users to monitor the state of the Redis topology in order to handle failover cases; Redisson handles this task all by itself.

Further Reading

Java-Distributed Caching in Redis

Introduction to Spring Data Redis

Topics:
java ,redis ,sentinel ,tutorial ,database ,redis sentinel ,how to run redis sentinel

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}