Databases 7 min read

Unlock Redis 6.0 Client‑Side Caching with Lettuce: A Hands‑On Guide

This article explains Redis 6.0’s built‑in client‑side caching, demonstrates how to install Redis via Docker, configure Lettuce snapshot dependencies, write Java code to enable and test cache synchronization, monitor Redis commands, and outlines current limitations of Lettuce and Spring Boot support.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Unlock Redis 6.0 Client‑Side Caching with Lettuce: A Hands‑On Guide

Redis Client Cache

Cache solutions generally fall into two categories:

L1 In‑memory caches (e.g., Caffeine, Ehcache) – fast, process‑local, but lost on restart and prone to cache avalanche.

L2 Centralized caches (e.g., Redis) – serve multiple nodes, but bandwidth can become a bottleneck under high concurrency.

Many open‑source frameworks combine L1 speed with L2 clustering, such as:

J2Cache two‑level cache framework

hotkey hotspot data real‑time sync

Redis 6.0 natively supports client‑side caching. The popular Java client lettuce provides support in its latest snapshot version (6.0.0.BUILD‑SNAPSHOT). The following code demonstrates the feature.

Redis 6.0 Installation

Install Redis 6.0 via Docker:

<code>docker run --name redis6 -p 6379:6379 --restart=always -d redis:6.0.6</code>

Jar Dependency

Use the lettuce client with the 6.0 snapshot version. Add the snapshot repository and dependency to

pom.xml

:

1. lettuce 6.0 snapshot dependency

<code>&lt;dependency&gt;
    &lt;groupId&gt;io.lettuce&lt;/groupId&gt;
    &lt;artifactId&gt;lettuce-core&lt;/artifactId&gt;
    &lt;version&gt;6.0.0.BUILD-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;</code>

2. configure snapshot repository

<code>&lt;repositories&gt;
    &lt;repository&gt;
        &lt;id&gt;sonatype-snapshots&lt;/id&gt;
        &lt;name&gt;Sonatype Snapshot Repository&lt;/name&gt;
        &lt;url&gt;https://oss.sonatype.org/content/repositories/snapshots/&lt;/url&gt;
        &lt;snapshots&gt;
            &lt;enabled&gt;true&lt;/enabled&gt;
        &lt;/snapshots&gt;
    &lt;/repository&gt;
&lt;/repositories&gt;</code>

Code Operation

Use lettuce to connect Redis and loop to read the value of

k1

.

<code>// <1> create single‑node connection info
RedisURI redisUri = RedisURI.builder()
        .withHost("127.0.0.1")
        .withPort(6379)
        .build();
RedisClient redisClient = RedisClient.create(redisUri);
StatefulRedisConnection<String, String> otherParty = redisClient.connect();
RedisCommands<String, String> commands = otherParty.sync();
StatefulRedisConnection<String, String> connection = redisClient.connect();
// <2> create cache accessor
Map<String, String> clientCache = new ConcurrentHashMap<>();
CacheFrontend<String, String> frontend = ClientSideCaching.enable(
        CacheAccessor.forMap(clientCache), connection,
        TrackingArgs.Builder.enabled());
// <3> write test data k1 v1
String key = "k1";
commands.set(key, "v1");
// <4> loop read
while (true) {
    // <4.1> get value from cache accessor, see if it syncs with server
    String cachedValue = frontend.get(key);
    System.out.println("当前 k1 的值为:--->" + cachedValue);
    Thread.sleep(3000);
}
</code>

Use

redis-cli

to modify

k1

value.

<code>./redis-cli -h 127.0.0.1 -p 6379
set k1 v2</code>

Observe console logs.

<code>...
当前 k1 的值为:--->v1
当前 k1 的值为:--->v1
当前 k1 的值为:--->v2
当前 k1 的值为:--->v2
...</code>

The above shows that when another client (redis‑cli) changes

k1

, the lettuce client perceives the change. To verify whether

CacheFrontend.get

still queries Redis, we can monitor the server.

Monitoring

<code>./redis-cli -h 127.0.0.1 -p 6379
MONITOR
OK
1595922453.165088 [0 172.16.1.96:57482] "SET" "k1" "v1"
1595922453.168238 [0 172.16.1.96:57483] "GET" "k1"
1595922472.046488 [0 172.16.1.96:57498] "set" "k1" "v2"
1595922474.208214 [0 172.16.1.96:57483] "GET" "k1"
</code>

Although the loop runs indefinitely, only the commands shown above appear, confirming that client‑side caching is effective.

Summary

Currently only lettuce supports this feature; jedis does not.

spring‑boot‑data‑redis does not yet support it; expected in Spring Boot 2.5.

Reference

[1] J2Cache two‑level cache framework: https://gitee.com/ld/J2Cache

[2] hotkey hotspot data real‑time sync: https://gitee.com/jd-platform-opensource/hotkey

JavaDockerRedisdatabasesLettuceClient Cache
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.