Java

How to Connect to Redis with Java

This tutorial will illustrate how to connect your Java application to your Redis server using Maven.

Pre-requisites

To follow along with this tutorial, we will recommend:

  • The latest version of the Redis server installed and configured on your system.
  • Java Development Kit installed and configured.
  • IntelliJ IDEA Community Edition.

Redis Java Client – Lettuce

To connect our application with Java, we need a Java client. Redis has various Java clients you can use. However, for this tutorial, we will use Lettuce.

Lettuce is a free, open-source, thread-safe Redis client that is easy to configure. It provides synchronous and asynchronous connections to Redis with minimum setup.

New Project

Let us start by creating a new project in IntelliJ. First, open your IDE and select New Project.

Select project type as Java and set your project SDK on the left-hand pane.

Select create from the template in the following windows and choose from the command line.

Click Next and set the name and path for your project.

Once completed, you will land on the code editor.

Install Lettuce

To Install Lettuce on our Java project, we need to add a framework. In the Project structure, right-click the root directory and select “Add framework support.”

Here, Select Maven and click OK.

Navigate into the src directory and open the pom.xml file. Edit the file and add the following lines.

<!-- https://mvnrepository.com/artifact/io.lettuce/lettuce-core -->
<dependency>
    <groupId>io.lettuce</groupId>
    <artifactId>lettuce-core</artifactId>
    <version>6.1.6.RELEASE</version>
</dependency>

The above should install Lettuce as a Maven dependency.

Exapand your dependencies windows and you should see the structure as shown:

NOTE: IntelliJ will fetch and install the Maven dependencies for you.

Java Connect to Redis

Add the code below to connect to your Redis cluster in your source file.

package com.csalem;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.api.sync.RedisStringCommands;
import io.lettuce.core.api.StatefulRedisConnection;

public class Main {

    public static void main(String[] args) {
        RedisClient client = RedisClient.create("redis://[email protected]:6379/0");
        StatefulRedisConnection connection = client.connect();
        RedisCommands syncCommands = connection.sync();
        System.out.println("Connection successful!");
        connection.close();
        client.shutdown();
    }
}

The above code should connect the Redis cluster and print messages if successful.

Redis Set new Key-Value

To set a new key-value pair, add the code as:

syncCommands.set("mykey", "myvalue");

The above code should add a new ley and value to the Redis database.

To add an expiring key, use the code:

syncCommands.setex("mykey", 60, "myvalue");

The above tells Redis to add a new key that expires in 60 seconds.

Redis get Value

To get a key associated with a specific key, we can do:

System.out.println(syncCommands.get("mykey"));

The above should return:

myvalue

Conclusion

In this tutorial, we discussed how to connect Redis with your Java application using the Lettuce client and Maven.

Thanks for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list