Redis Client for Java: Jedis, Lettuce, and Redisson — A Detailed Guide
Redis is a high-performance, in-memory key-value data store. Several mature libraries exist for interacting with Redis from Java applications. In this article, we will look at the three most popular Redis clients for Java: Jedis, Lettuce, and Redisson. You will learn their features, advantages, and get ready-to-use code examples for a quick start.
1. Jedis — A Simple and Lightweight Client
Jedis is a minimalist, thread-unsafe client that is ideal for simple projects and monolithic applications. It provides direct access to Redis commands through a simple API. Jedis does not support asynchronous calls out of the box, but compensates with ease of use.
Adding the Dependency (Maven)
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>5.1.0</version></dependency>Example: Connection and Basic Operations
import redis.clients.jedis.Jedis;
public class JedisExample { public static void main(String[] args) { // Connecting to Redis (default localhost:6379) try (Jedis jedis = new Jedis("localhost", 6379)) { // Authentication (if a password is set) // jedis.auth("password");
// Setting a value jedis.set("user:100", "Alice"); // Getting a value String user = jedis.get("user:100"); System.out.println("User: " + user); // User: Alice
// Working with hashes jedis.hset("user:200", "name", "Bob"); jedis.hset("user:200", "age", "30"); String name = jedis.hget("user:200", "name"); System.out.println("Name: " + name); // Name: Bob
// Setting TTL (key time to live) jedis.expire("user:100", 60); // Key lives for 60 seconds } }}Pros and Cons of Jedis
- Pros: minimal learning curve, lightweight, full support for Redis commands.
- Cons: not thread-safe (requires a connection pool), no asynchronous API, no built-in support for distributed data structures.
2. Lettuce — Asynchronous and Reactive Client
Lettuce is a modern, thread-safe Redis client built on Netty. It supports synchronous, asynchronous, and reactive (Reactive Streams) programming. Lettuce is ideal for high-load and asynchronous applications on Java 8+.
Adding the Dependency (Maven)
<dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>6.3.1.RELEASE</version></dependency>Example: Asynchronous Connection
import io.lettuce.core.RedisClient;import io.lettuce.core.api.StatefulRedisConnection;import io.lettuce.core.api.async.RedisAsyncCommands;
import java.util.concurrent.CompletableFuture;
public class LettuceAsyncExample { public static void main(String[] args) { // Creating a client RedisClient redisClient = RedisClient.create("redis://localhost:6379"); // Opening a connection try (StatefulRedisConnection<String, String> connection = redisClient.connect()) { RedisAsyncCommands<String, String> async = connection.async(); // Asynchronous setting a value CompletableFuture<String> setFuture = async.set("key:1", "Hello from Lettuce!").toCompletableFuture(); // Asynchronous getting