How to Connect to Redis Cluster in Java

Redis is an in-memory data structure store that can be used to implement NoSQL databases, caches, message brokers, and more. Redis Cluster is a special version of Redis that improves the scalability and availability of your Redis database by sharding (partitioning) data across multiple nodes.

Whether you're using Redis or Redis Cluster, you need to establish a connection to the Redis server to begin storing and retrieving data. However, Java developers need to know that the Java programming language isn't compatible with Redis or Redis Cluster out of the box. Instead, programmers who want to use Java with Redis can download a third-party Redis Java framework such as Redisson.

Redisson is a Redis Java client that helps lower the learning curve for Java developers when using Redis. The Redisson library includes many familiar Java data structures, services, and components and makes them available within Redis. This helps programmers start using Redis while relying on the Java constructs they already know.

To make a connection to Redis Cluster in Java using Redisson, follow the steps below.

Step 0: Run a Redis Cluster Server

Before you get started, you'll need to have a Redis Cluster server running. You can pull a prebuilt Bitnami image of Redis Cluster from the Docker Hub Registry:

      docker pull bitnami/redis-cluster:latest

The details of how to set up Redis Cluster are available in this guide. Once you've set it up properly, you can run Redis Cluster with the command:

      docker run --name redis-cluster -e ALLOW_EMPTY_PASSWORD=yes bitnami/redis-cluster:latest

Step 1: Add the Redisson Dependency to Your Project Files

The next step is to add the Redisson dependency to the build automation tool in your Java project, such as Maven or Gradle. If you're using Maven, add the following code block to your pom.xml file:


If you're using Gradle, add the following line to your build.gradle file:

compile 'org.redisson:redisson:3.28.0'

Step 2: Create the Configuration and Connect

Next, create a Redisson configuration for Redis Cluster in your Java code using the Config object. Insert the lines below:

      Config config = new Config();

You can also use "rediss" instead of "redis" to establish an SSL connection.

Below are three ways of establishing a connection to Redis Cluster in Java, depending on what kind of Redisson client you want to use: the sync/async API, the reactive API, and the RxJava3 API, respectively.

      RedissonClient redisson = Redisson.create(config);
      RedissonReactiveClient redissonReactive = redisson.reactive();
      RedissonRxClient redissonRx = redisson.rxJava();

Step 3: Interact With Redis in Java

Once the connection is established, you can start working with Redis Cluster in Java with the Redisson framework. For example, the code below illustrates how to use the RMap interface in Redisson, which is a Redis version of the Map interface in Java:

      RMap map = redisson.getMap("simple");
      map.put(1, 0);
      map.put(3, 5);
      map.put(4, 6);
      map.put(7, 8);

Want to learn more about how to connect to Redis Cluster in Java using the Redisson library? Check out the Redisson documentation on GitHub and get started.

Similar articles