Find tutorials, examples and technical articles that will help you to develop with Redis and Java.
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.
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.
redis-cli
localhost>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.0.2</version>
</dependency>
import redis.clients.jedis.*;
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);
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 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 based on Search capabilities
How to implement leaderboard app using Redis & Java(Spring)
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 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.