Learnitweb

Connecting to Redis Using Redisson in Java (Programmatic Configuration)

In this tutorial, we will learn how to create a Redis connection using Redisson, one of the most widely used Redis clients in the Java ecosystem.
We will configure Redisson programmatically (without YAML or configuration files) and understand how to work in single-server mode. Later, you can easily extend this to master-slave or cluster modes.


1. Project Setup

We’ll assume you have a Maven or Gradle project already created.
The project structure will look like this:

GoldwasserTest
 └── src
     └── main
         └── java
             └── com
                 └── guru
                     └── config
                         └── RedisConfig.java

So, we are creating a package hierarchy:

  • com.guru.config
  • Inside that, we’ll create a Java class named RedisConfig.java.

2. Adding Redisson Dependency

To use Redisson, add the dependency to your pom.xml if you are using Maven:

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.29.0</version>
</dependency>

If you are using Gradle, add this line in your build.gradle:

implementation 'org.redisson:redisson:3.29.0'

3. Creating the Configuration Class

Let’s now create our configuration class named RedisConfig.java.

package com.guru.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.api.RedissonReactiveClient;
import org.redisson.config.Config;

public class RedisConfig {

    private RedissonClient redissonClient;
    private RedissonReactiveClient redissonReactiveClient;

    public RedisConfig() {
        createRedisClient();
        createReactiveRedisClient();
    }

    // Method to create Redisson Client
    private void createRedisClient() {
        Config config = new Config();
        config.useSingleServer()
              .setAddress("redis://127.0.0.1:6379");

        // Create Redisson client
        redissonClient = Redisson.create(config);
        System.out.println("Redisson Client created successfully!");
    }

    // Method to create Reactive Redisson Client
    private void createReactiveRedisClient() {
        Config config = new Config();
        config.useSingleServer()
              .setAddress("redis://127.0.0.1:6379");

        // Create Reactive Redisson client
        redissonReactiveClient = Redisson.createReactive(config);
        System.out.println("Reactive Redisson Client created successfully!");
    }

    public RedissonClient getRedissonClient() {
        return redissonClient;
    }

    public RedissonReactiveClient getRedissonReactiveClient() {
        return redissonReactiveClient;
    }
}

4. Explanation of Code

Let’s break down what each part of the code does:

a. Importing Dependencies

We import Redisson classes such as:

  • RedissonClient — the main client to interact with Redis synchronously.
  • RedissonReactiveClient — for reactive programming style using Reactor.
  • Config — used to define configuration for connecting to Redis.

b. Creating Configuration

We create a Config object and specify the server mode:

Config config = new Config();
config.useSingleServer()
      .setAddress("redis://127.0.0.1:6379");
  • useSingleServer() — tells Redisson that we’re connecting to a single Redis instance (not a cluster or sentinel setup).
  • setAddress("redis://127.0.0.1:6379") — sets the Redis host and port.
    • The prefix redis:// is mandatory.
    • The default Redis port is 6379.

c. Creating the Client

Once the configuration is ready, we create a client instance:

redissonClient = Redisson.create(config);

This establishes a connection with Redis and returns a RedissonClient instance which can be used to perform all Redis operations (get/set, lists, maps, etc.).

d. Creating Reactive Client

Similarly, to create a reactive version:

redissonReactiveClient = Redisson.createReactive(config);

This gives you a RedissonReactiveClient that supports asynchronous, non-blocking operations using Reactor (Flux/Mono types).


5. Testing the Configuration

Let’s write a simple test class to verify that our Redisson connection is working.

package com.guru;

import com.guru.config.RedisConfig;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

public class RedisTest {
    public static void main(String[] args) {
        RedisConfig config = new RedisConfig();
        RedissonClient client = config.getRedissonClient();

        // Create or access a Redis key
        RBucket<String> bucket = client.getBucket("testKey");
        bucket.set("Hello Redis!");

        // Retrieve and print value
        String value = bucket.get();
        System.out.println("Stored value in Redis: " + value);

        // Shutdown client when done
        client.shutdown();
    }
}

Expected Output:

Redisson Client created successfully!
Reactive Redisson Client created successfully!
Stored value in Redis: Hello Redis!

6. Understanding Redisson Modes

Right now, we are using single-server mode for simplicity.
Redisson also supports other configurations:

  • Master/Slave Mode config.useMasterSlaveServers() .setMasterAddress("redis://127.0.0.1:6379") .addSlaveAddress("redis://127.0.0.1:6380");
  • Cluster Mode config.useClusterServers() .addNodeAddress("redis://127.0.0.1:7000", "redis://127.0.0.1:7001");
  • Sentinel Mode config.useSentinelServers() .setMasterName("mymaster") .addSentinelAddress("redis://127.0.0.1:26379");

You can easily switch between these modes depending on your Redis setup.


7. Summary

  • We created a Redis configuration programmatically using Redisson.
  • We connected to a single Redis instance.
  • We built both synchronous and reactive clients.
  • We tested the connection using a simple Redis key-value example.
  • Finally, we learned how to switch between different Redis modes (Single, Master-Slave, Cluster).