How to Connect to Redis in Java

Redis is an in-memory data structure store with use cases that include NoSQL databases, caches, and message brokers. In order to implement these use cases, Redis developers need to be able to connect to a Redis instance so they can start storing and retrieving data.

However, there's one catch you need to know: the base version of Redis isn't compatible with programming languages such as Java. Instead, Java developers who want to use Redis should install a third-party Redis Java framework such as Redisson.

Redisson is a Redis Java client that is designed to help Java developers get up and running with Redis. The Redisson library translates dozens of familiar Java data structures, services, and components to work with Redis, allowing Java programmers to use Redis with minimal effort.

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

Step 0: Run a Redis Server

Before you get started, you'll need to have a Redis server running. If you're using a Linux distribution such as Ubuntu, for example, enter the following commands to start Redis:

sudo apt-get update
sudo apt-get install redis-server
redis-server

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. This tells the build tool that your Java project will be using Redisson so that it can automatically download and install the library.

Adding Redisson to your Java project is simple. If you're using Maven, add the following code block to your pom.xml file:

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

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

The next step is to create a Redisson configuration in your Java code using the Config object. Insert the lines below:

Config config = new Config();
config.useSingleServer().addNodeAddress("redis://127.0.0.1:7181");

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

Establishing a connection to Redis in Java will depend on what kind of Redisson client you want to use. The code snippet below illustrates three ways of connecting to Redis in Java: 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 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);

To learn more about how to connect to Redis in Java using the Redisson library, check out the Redisson documentation on GitHub.

Similar articles