DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Trending

  • Designing the Runtime Layer for Stateful AI Agents: Where Agent Substrate Fits
  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI
  • RAG Architecture Patterns: Proxy, Caching, and Routing
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Quickstart: How to Use Redis on Java

Quickstart: How to Use Redis on Java

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

By 
Nikita Koksharov
·
Mar. 13, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
66.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");

        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);

        redisson.shutdown();
    }
}


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:

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


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.

Redis (company) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook