lettuce
Advanced Java Redis client for thread-safe sync, async, and reactive usage. Supports Cluster, Sentinel, Pipelining, and codecs.
Top Related Projects
Provides support to increase developer productivity in Java when using Redis, a key-value store. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
Redisson - Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava/Reactive API. Over 50 Redis or Valkey based Java objects and services: Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Spring, Tomcat, Scheduler, JCache API, Hibernate, RPC, local cache...
Redis Java client
General purpose redis client
Redis Go client
Quick Overview
Lettuce is a scalable, thread-safe Redis client for Java applications. It provides a high-performance, non-blocking API for interacting with Redis, supporting both synchronous and asynchronous operations. Lettuce is designed to be used in multi-threaded environments and offers support for Redis Cluster, Sentinel, and various Redis data structures.
Pros
- High performance and low latency due to its non-blocking nature
- Comprehensive support for Redis features, including Cluster and Sentinel
- Thread-safe and suitable for concurrent use in multi-threaded applications
- Supports both synchronous and asynchronous programming models
Cons
- Steeper learning curve compared to simpler Redis clients
- May be overkill for small-scale applications with simple Redis needs
- Documentation can be overwhelming for beginners
- Requires careful connection management in certain scenarios
Code Examples
- Connecting to Redis and performing basic operations:
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "Hello, Redis!");
String value = syncCommands.get("key");
System.out.println(value); // Output: Hello, Redis!
connection.close();
redisClient.shutdown();
- Using asynchronous commands:
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisAsyncCommands<String, String> asyncCommands = connection.async();
RedisFuture<String> future = asyncCommands.get("key");
future.thenAccept(System.out::println);
connection.close();
redisClient.shutdown();
- Working with Redis Pub/Sub:
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
StatefulRedisPubSubConnection<String, String> pubSubConnection = redisClient.connectPubSub();
RedisPubSubAsyncCommands<String, String> pubSubAsync = pubSubConnection.async();
pubSubConnection.addListener(new RedisPubSubAdapter<>() {
@Override
public void message(String channel, String message) {
System.out.println("Received message: " + message + " on channel: " + channel);
}
});
pubSubAsync.subscribe("mychannel");
pubSubAsync.publish("mychannel", "Hello, PubSub!");
pubSubConnection.close();
redisClient.shutdown();
Getting Started
To use Lettuce in your Java project, add the following dependency to your Maven pom.xml
:
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.2.3.RELEASE</version>
</dependency>
For Gradle, add this to your build.gradle
:
implementation 'io.lettuce:lettuce-core:6.2.3.RELEASE'
Then, you can start using Lettuce in your Java code as shown in the code examples above.
Competitor Comparisons
Provides support to increase developer productivity in Java when using Redis, a key-value store. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
Pros of Spring Data Redis
- Provides a higher-level abstraction for Redis operations, simplifying development
- Integrates seamlessly with other Spring projects and frameworks
- Offers automatic serialization and deserialization of objects
Cons of Spring Data Redis
- May have a steeper learning curve for developers not familiar with Spring ecosystem
- Can be overkill for simple Redis use cases or non-Spring projects
- Potentially slower performance due to additional abstraction layers
Code Comparison
Spring Data Redis:
@Repository
public interface UserRepository extends CrudRepository<User, String> {
User findByUsername(String username);
}
Lettuce:
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "value");
String value = syncCommands.get("key");
Spring Data Redis provides a more declarative approach with repository interfaces, while Lettuce offers a lower-level API for direct Redis operations. Spring Data Redis abstracts away much of the boilerplate code, making it easier to work with Redis in a Spring environment. Lettuce, on the other hand, gives more fine-grained control over Redis operations and can be used in any Java project, not just Spring-based ones.
Redisson - Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava/Reactive API. Over 50 Redis or Valkey based Java objects and services: Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Spring, Tomcat, Scheduler, JCache API, Hibernate, RPC, local cache...
Pros of Redisson
- More comprehensive feature set, including distributed objects, locks, and services
- Built-in support for Redis Cluster and Redis Sentinel
- Automatic reconnection and failover handling
Cons of Redisson
- Steeper learning curve due to its extensive API
- Potentially higher memory footprint for smaller applications
- Less frequent updates compared to Lettuce
Code Comparison
Lettuce:
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "Hello, Redis!");
String value = syncCommands.get("key");
Redisson:
Config config = new Config();
config.useSingleServer().setAddress("redis://localhost:6379");
RedissonClient redisson = Redisson.create(config);
RBucket<String> bucket = redisson.getBucket("key");
bucket.set("Hello, Redis!");
String value = bucket.get();
Both Lettuce and Redisson are popular Java Redis clients, each with its own strengths. Lettuce offers a more lightweight and straightforward approach, while Redisson provides a richer set of features for complex distributed systems. The choice between them depends on the specific requirements of your project and the level of Redis functionality you need to leverage.
Redis Java client
Pros of Jedis
- Simpler API, easier to learn for beginners
- Lightweight with fewer dependencies
- Synchronous operations by default, which can be more intuitive for some developers
Cons of Jedis
- Limited support for advanced Redis features and data structures
- Less robust connection pooling and thread-safety compared to Lettuce
- Fewer options for customization and fine-tuning
Code Comparison
Jedis:
Jedis jedis = new Jedis("localhost");
jedis.set("key", "value");
String value = jedis.get("key");
jedis.close();
Lettuce:
RedisClient client = RedisClient.create("redis://localhost");
StatefulRedisConnection<String, String> connection = client.connect();
RedisCommands<String, String> commands = connection.sync();
commands.set("key", "value");
String value = commands.get("key");
Both Jedis and Lettuce are popular Java clients for Redis, each with its own strengths. Jedis offers simplicity and ease of use, making it a good choice for smaller projects or developers new to Redis. Lettuce, on the other hand, provides more advanced features, better performance, and support for reactive programming, making it suitable for larger, more complex applications. The choice between the two depends on the specific requirements of your project and your familiarity with Redis and Java concurrency patterns.
General purpose redis client
Pros of StackExchange.Redis
- Designed specifically for .NET, offering seamless integration with C# and other .NET languages
- Extensive documentation and strong community support
- High-performance and thread-safe implementation
Cons of StackExchange.Redis
- Limited to .NET ecosystem, not suitable for Java or other non-.NET projects
- Steeper learning curve compared to Lettuce's more intuitive API
- Less flexible configuration options for advanced Redis features
Code Comparison
StackExchange.Redis:
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase db = redis.GetDatabase();
db.StringSet("key", "value");
string value = db.StringGet("key");
Lettuce:
RedisClient redisClient = RedisClient.create("redis://localhost");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "value");
String value = syncCommands.get("key");
Both libraries provide similar functionality for basic Redis operations, but StackExchange.Redis uses a more .NET-centric approach with its ConnectionMultiplexer
and IDatabase
interfaces. Lettuce, on the other hand, offers a more Java-style API with its RedisClient
and RedisCommands
classes. The choice between the two largely depends on the programming language and ecosystem of your project.
Redis Go client
Pros of go-redis
- Written in Go, offering better performance and concurrency support
- Simpler API and easier to use for Go developers
- Actively maintained with frequent updates and improvements
Cons of go-redis
- Limited feature set compared to Lettuce
- Less mature ecosystem and fewer advanced features
- May require more manual configuration for complex scenarios
Code Comparison
go-redis:
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
err := rdb.Set("key", "value", 0).Err()
val, err := rdb.Get("key").Result()
Lettuce:
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "value");
String value = syncCommands.get("key");
Both libraries provide straightforward ways to connect to Redis and perform basic operations. go-redis offers a more concise syntax, while Lettuce provides a more structured approach with separate connection and command objects.
go-redis is ideal for Go projects seeking simplicity and performance, while Lettuce is better suited for Java applications requiring advanced features and a more comprehensive Redis client.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Lettuce - Advanced Java Redis client
Lettuce is a scalable thread-safe Redis client for synchronous,
asynchronous and reactive usage. Multiple threads may share one connection if they avoid blocking and transactional
operations such as BLPOP
and MULTI
/EXEC
.
Lettuce is built with netty.
Supports advanced Redis features such as Sentinel, Cluster, Pipelining, Auto-Reconnect and Redis data models.
This version of Lettuce has been tested against the latest Redis source-build.
- synchronous, asynchronous and reactive usage
- Redis Sentinel
- Redis Cluster
- SSL and Unix Domain Socket connections
- Streaming API
- CDI
- Codecs (for UTF8/bit/JSON etc. representation of your data)
- multiple Command Interfaces
- Support for Native Transports
- Compatible with Java 8++ (implicit automatic module w/o descriptors)
See the reference documentation and API Reference for more details.
How do I Redis?
Learn for free at Redis University
Build faster with the Redis Launchpad
Communication
- GitHub Discussions (Q&A, Ideas, General discussion)
- Stack Overflow (Questions): https://stackoverflow.com/questions/tagged/lettuce
- Discord:
- Twitter:
- GitHub Issues (Bug reports, feature requests)
Documentation
Binaries/Download
Binaries and dependency information for Maven, Ivy, Gradle and others can be found at http://search.maven.org.
Releases of lettuce are available in the Maven Central repository. Take also a look at the Releases.
Example for Maven:
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>x.y.z</version>
</dependency>
If you'd rather like the latest snapshots of the upcoming major version, use our Maven snapshot repository and declare the appropriate dependency version.
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>x.y.z.BUILD-SNAPSHOT</version>
</dependency>
<repositories>
<repository>
<id>sonatype-snapshots</id>
<name>Sonatype Snapshot Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
Basic Usage
RedisClient client = RedisClient.create("redis://localhost");
StatefulRedisConnection<String, String> connection = client.connect();
RedisStringCommands sync = connection.sync();
String value = sync.get("key");
Each Redis command is implemented by one or more methods with names identical to the lowercase Redis command name. Complex commands with multiple modifiers that change the result type include the CamelCased modifier as part of the command name, e.g. zrangebyscore and zrangebyscoreWithScores.
See Basic usage for further details.
Asynchronous API
StatefulRedisConnection<String, String> connection = client.connect();
RedisStringAsyncCommands<String, String> async = connection.async();
RedisFuture<String> set = async.set("key", "value");
RedisFuture<String> get = async.get("key");
LettuceFutures.awaitAll(set, get) == true
set.get() == "OK"
get.get() == "value"
See Asynchronous API for further details.
Reactive API
StatefulRedisConnection<String, String> connection = client.connect();
RedisStringReactiveCommands<String, String> reactive = connection.reactive();
Mono<String> set = reactive.set("key", "value");
Mono<String> get = reactive.get("key");
set.subscribe();
get.block() == "value"
See Reactive API for further details.
Pub/Sub
RedisPubSubCommands<String, String> connection = client.connectPubSub().sync();
connection.getStatefulConnection().addListener(new RedisPubSubListener<String, String>() { ... })
connection.subscribe("channel");
Building
Lettuce is built with Apache Maven. The tests require multiple running Redis instances for different test cases which
are configured using a Makefile
. Tests run by default against Redis unstable
.
To build:
$ git clone https://github.com/redis/lettuce.git
$ cd lettuce/
$ make prepare ssl-keys
$ make test
- Initial environment setup (clone and build
redis
):make prepare
- Setup SSL Keys:
make ssl-keys
- Run the build:
make test
- Start Redis (manually):
make start
- Stop Redis (manually):
make stop
Bugs and Feedback
For bugs, questions and discussions please use the GitHub Issues.
License
- This repository is licensed under the "MIT" license. See LICENSE.
- Fork of https://github.com/wg/lettuce
Contributing
Github is for social coding: if you want to write code, I encourage contributions through pull requests from forks of this repository. Create Github tickets for bugs and new features and comment on the ones that you are interested in and take a look into CONTRIBUTING.md
Top Related Projects
Provides support to increase developer productivity in Java when using Redis, a key-value store. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
Redisson - Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava/Reactive API. Over 50 Redis or Valkey based Java objects and services: Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Spring, Tomcat, Scheduler, JCache API, Hibernate, RPC, local cache...
Redis Java client
General purpose redis client
Redis Go client
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot