Skip to main content

Java and Redis

Find tutorials, examples and technical articles that will help you to develop with Redis and Java.

Getting Started

Java community has built many client libraries that you can find here. For your first steps with Java and Redis, this article will show how to use the two main libraries: Jedis and Lettuce.

The blog post “Jedis vs. Lettuce: An Exploration” will help you to select the best for your application; keeping in mind that both are available in Spring & SpringBoot framework.

Redis is an open source, in-memory, key-value data store most commonly used as a primary database, cache, message broker, and queue. Redis delivers sub-millisecond response times, enabling fast and powerful real-time applications in industries such as gaming, fintech, ad-tech, social media, healthcare, and IoT.

Run a Redis server

You can either run Redis in a Docker container or directly on your machine. Use these commands to setup a Redis server locally on Mac OS:

 brew tap redis-stack/redis-stack
brew install --cask redis-stack
INFO

Redis Stack unifies and simplifies the developer experience of the leading Redis modules and the capabilities they provide. Redis Stack bundles five Redis modules: RedisJSON, RedisSearch, RedisGraph, RedisTimeSeries, and RedisBloom. Learn more

Ensure that you are able to use the following Redis command to connect to the Redis instance.

 redis-cli
localhost>

Using Jedis

Step 1. Add dependencies Jedis dependency to your Maven (or Gradle) project file:

 <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.4.0</version>
</dependency>

Step 2. Import the required classes

 import redis.clients.jedis.*;

Step 3. Create a Connection Pool

Once you have added the Jedis library to your project and imported the necessary classes you can create a connection pool.

You can find more information about Jedis connection pool in the Jedis Wiki. The connection pool is based on the Apache Common Pool 2.0 library.

  JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "localhost", 6379);

Step 4. Write your application code

Once you have access to the connection pool you can now get a Jedis instance and start to interact with your Redis instance.

  // Create a Jedis connection pool
JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "localhost", 6379);

// Get the pool and use the database
try (Jedis jedis = jedisPool.getResource()) {

jedis.set("mykey", "Hello from Jedis");
String value = jedis.get("mykey");
System.out.println( value );

jedis.zadd("vehicles", 0, "car");
jedis.zadd("vehicles", 0, "bike");
Set<String> vehicles = jedis.zrange("vehicles", 0, -1);
System.out.println( vehicles );

}

// close the connection pool
jedisPool.close();

Find more information about Java & Redis connections in the "Redis Connect".

Redis Launchpad

Redis Launchpad is like an “App Store” for Redis sample apps. You can easily find apps for your preferred frameworks and languages. Check out a few of these apps below, or click here to access the complete list.

Movie Database app in Java

launchpad

Movie Database app in Java based on RediSearch capabilities

Leaderboard app in Java

launchpad

How to implement leaderboard app using Redis & Java(Spring)

Ecosystem

As developer you can use the Java client library directly in your application, or you can frameworks like: Spring, Quarkus, Vert.x, and Micronaut.

Develop with Spring

Spring logo

Spring Data Redis, part of the larger Spring Data project. It provides easy access to Redis from Spring applications.

Develop with Quarkus

Quarkus logo

Redis Client extension allows you to connect your Quarkus application to a Redis instance.

Develop with Vert.x

Vert.x logo

Eclipse Vert.x is a framework to build reactive applications on the JVM. Vert.x-redis is redis client to be used with Vert.x.

Develop with Micronaut

Micronaut logo

Micronaut is a framework for building microservices and serverless applications. The Micronaut Redis extension provides the integration with Redis.


More developer resources

Sample Code

Brewdis - Product Catalog (Spring) See how to use Redis and Spring to build a product catalog with streams, hashes and RediSearch

Redis Stream in Action (Spring) See how to use Spring to create multiple producer and consumers with Redis Streams

Rate Limiting with Vert.x See how to use Redis Sorted Set with Vert.x to build a rate limiting service.

Redis Java Samples with Lettuce Run Redis Commands from Lettuce


Redis University

Redis for Java Developers

Redis for Java Developers teaches you how to build robust Redis client applications in Java using the Jedis client library. The course focuses on writing idiomatic Java applications with the Jedis API, describing language-specific patterns for managing Redis database connections, handling errors, and using standard classes from the JDK. The course material uses the Jedis API directly with no additional frameworks. As such, the course is appropriate for all Java developers, and it clearly illustrates the principles involved in writing applications with Redis.