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 Jedis, the supported Redis client for Java.

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

Redis Stack unifies and simplifies the developer experience of the leading Redis modules and the capabilities they provide. Redis Stack provides the following in addition to Redis Open Source: JSON, Search, Time Series, and Probabilistic data structures.

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


Using Jedis#

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


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

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

Movie Database app in Java based on Search capabilities

Leaderboard app in Java

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


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

More developer resources#

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

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 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.

Last updated on Feb 19, 2024