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.

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 Marketplace#

Redis Marketplace is like an “App Store” for Redis sample apps. You can easily find apps for your preferred frameworks and languages. Below are few of these apps. Click here to access the complete lists of apps

Movie Database app in Java#

marketplace

Movie Database app in Java based on RediSearch capabilities

Leaderboard app in Java#

marketplace

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.