Configs2026年7月13日·1 分钟阅读

Lettuce — Scalable Async Redis Client for Java

Lettuce is a thread-safe, non-blocking Redis client for Java built on Netty, supporting synchronous, asynchronous, and reactive programming models with full Redis Cluster and Sentinel support.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 29/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Single
信任
信任等级:Established
入口
Lettuce Overview
安全暂存命令
npx -y tokrepo@latest install 80031eef-7eb9-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

Introduction

Lettuce is an advanced Java client for Redis built on top of Netty. Unlike Jedis, Lettuce uses non-blocking I/O, making a single connection safely shareable across multiple threads. It is the default Redis driver in Spring Data Redis and supports synchronous, asynchronous (CompletableFuture), and reactive (Project Reactor) APIs.

What Lettuce Does

  • Provides synchronous, asynchronous, and reactive APIs for all Redis commands
  • Shares a single connection safely across threads via non-blocking Netty I/O
  • Supports Redis Cluster with automatic topology refresh and adaptive routing
  • Handles Redis Sentinel for automatic master discovery and failover
  • Offers client-side caching and connection pooling for high-throughput use cases

Architecture Overview

Lettuce uses Netty's event loop for network I/O, multiplexing commands over a single TCP connection. Commands are encoded into RESP protocol frames and queued; responses are demultiplexed and dispatched to the correct future or subscriber. For Redis Cluster, Lettuce maintains a topology map and routes commands by hash slot. The reactive API wraps responses in Project Reactor Mono and Flux types.

Self-Hosting & Configuration

  • Add lettuce-core as a Maven or Gradle dependency
  • Create a RedisClient with a Redis URI including host, port, and optional password
  • For Cluster, use RedisClusterClient with seed node URIs
  • Configure connection timeout, auto-reconnect, and topology refresh intervals via ClientOptions
  • For reactive usage, add Project Reactor to your classpath

Key Features

  • Non-blocking I/O with a single thread-safe connection per client
  • Three programming models: sync, async (CompletableFuture), and reactive (Reactor)
  • Automatic Redis Cluster topology refresh with periodic and adaptive triggers
  • TLS/SSL support for encrypted connections to Redis
  • Client-side caching with server-assisted invalidation (Redis 6+ tracking)

Comparison with Similar Tools

  • Jedis — synchronous and simpler; Lettuce is non-blocking and thread-safe by default
  • Redisson — higher-level distributed objects; Lettuce is a lower-level driver
  • Spring Data Redis — abstraction layer that uses Lettuce as its default driver
  • ioredis (Node.js) — similar async-first philosophy for the JavaScript ecosystem
  • redis-rs (Rust) — async Redis client for Rust with comparable design goals

FAQ

Q: When should I choose Lettuce over Jedis? A: Use Lettuce when you need async or reactive APIs, thread-safe shared connections, or Redis Cluster with automatic topology updates.

Q: Does Lettuce support connection pooling? A: Lettuce connections are thread-safe, so pooling is often unnecessary. For high-throughput scenarios, Lettuce provides an optional connection pool via AsyncPool.

Q: Is Lettuce compatible with Valkey? A: Yes. Lettuce communicates via the RESP protocol and works with Valkey, KeyDB, DragonflyDB, and other Redis-compatible servers.

Q: How does Lettuce handle Redis Cluster failover? A: Lettuce periodically refreshes the cluster topology and supports adaptive refresh triggered by MOVED/ASK redirections, automatically routing to new primaries.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产