DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Trending

  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  1. DZone
  2. Coding
  3. Java
  4. High Availability With Redis Sentinel on Java

High Availability With Redis Sentinel on Java

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

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Sep. 11, 19 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
17.4K Views

Join the DZone community and get the full member experience.

Join For Free

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

Redis (company) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!