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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Trending

  • Start Coding With Google Cloud Workstations
  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • Automatic Code Transformation With OpenRewrite
  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 user avatar
Nikita Koksharov
·
Mar. 13, 19 · Tutorial
Likes (14)
Comment
Save
Tweet
Share
65.3K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: