Over a million developers have joined DZone.

Quickstart: How to Use Redis on Java

DZone's Guide to

Quickstart: How to Use Redis on Java

Learn more about how you can quickly start using Redis on Java.

· Java Zone ·
Free Resource

Secure your Java app or API service quickly and easily with Okta's user authentication and authorization libraries. Developer accounts are free forever. Try Okta Instead.

If you want to use Java with Redis, you'll need to do a little tinkering in order to make both technologies work with each other. This quickstart guide will walk you through everything you need to use Redis on Java.

1. Run Redis

To get up and running quickly with Redis, you can consult this Redis Quick Start guide. The basic steps you'll need to follow are:

  1. Download and install Redis. Using the Linux package manager is not recommended because the available version is likely out of date. Instead, Redis advises you to compile from the source code, which is a relatively straightforward process.

  2. Start the Redis server with the redis-server command.

  3. Verify that Redis is working properly with the redis-cli ping command. If all is well, you will receive the message PONG in response.

  4. Note that there are a few more steps you should follow if you plan to use Redis in production. For example, Redis does not have any authentication by default, which is highly risky if it is exposed to the internet. In addition, you should use a configuration file if you plan to deploy Redis in production with any degree of complexity. However, the three steps above should suffice for this Redis quickstart tutorial.

2. Create a Redis-Based Java App

Your next step is to test Redis and Java by creating a Redis-based Java app.

Java is not configured to work with Redis right off the bat. Instead, developers use libraries such as Redisson to make Redis development in Java much easier and faster.

The source code below demonstrates an example Java application using the Redisson library.

package redis.demo;

import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

 * Redis based demo application on Java
public class Application 
    public static void main( String[] args )
        Config config = new Config();
        // use single Redis server

        RedissonClient redisson = Redisson.create(config);

        // perform operations

        RBucket<String> bucket = redisson.getBucket("simpleObject");
        bucket.set("This is object value");

        RMap<String, String> map = redisson.getMap("simpleMap");
        map.put("mapKey", "This is map value");

        String objectValue = bucket.get();
        System.out.println("stored object value: " + objectValue);

        String mapValue = map.get("mapKey");
        System.out.println("stored map value: " + mapValue);


By default, Redis uses port 6379 to communicate. The application performs a few basic tests using the RBucket and RMap data structures before shutting down.

3. Build and Run the App

To run the application, first generate a new Maven project with the command:

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.3 -DgroupId=redis.demo -DartifactId=redistest -Dversion=1.0

You'll need to edit the pom.xml file in the project directory in order to add the dependency for Redisson:


You can then compile and run your application with the following terminal commands:

mvn compile
mvn exec:java -D exec.mainClass=redis.demo.Application

And that's it! Once you've completed this tutorial, you can use this example as a foundation for building a real Redis application in Java.

Final Thoughts

The bad news is that Redis isn't compatible with Java immediately. The good news is that when you use a client like Redisson, it's not at all difficult to write Redis-based applications in Java. Redisson offers an accessible implementation of distributed Java objects and services so that you can spend less time on the technical details and more time building quality applications.

Secure your Java app or API service quickly and easily with Okta's user authentication and authorization libraries. Developer accounts are free forever. Try Okta Instead.

redis ,java ,quickstart ,app ,database

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}