I Finally Deleted the Java Code That Reads the Whole Redis Object to Update One Field
The article explains how the old pattern of GET‑ing an entire JSON string from Redis, deserializing it in Java, modifying a single field and writing it back is inefficient and error‑prone, and shows how Spring Data Redis 4.2’s RedisJsonTemplate lets you update or read individual JSON paths directly, reducing code, network traffic and race‑condition risks.
In many Spring Boot projects a user cache is stored as a JSON string in Redis. When a user changes their nickname the typical implementation reads the whole value, deserializes it with ObjectMapper, modifies the field, re‑serializes and writes the entire object back:
public void updateNickname(Long userId, String nickname) {
String key = "user:profile:" + userId;
String json = stringRedisTemplate.opsForValue().get(key);
if (json == null) return;
UserProfile profile = objectMapper.readValue(json, UserProfile.class);
profile.setNickname(nickname);
String newJson = objectMapper.writeValueAsString(profile);
stringRedisTemplate.opsForValue().set(key, newJson);
}This approach does a full GET → deserialize → modify → serialize → SET cycle just to change a single attribute, which is wasteful and can cause lost updates when concurrent requests modify different fields.
Redis JSON becomes a native data type in Redis 8
Redis 8 integrates the RedisJSON module into the core server, making JSON a first‑class data structure. Commands such as JSON.SET, JSON.GET, JSON.ARRAPPEND, JSON.TOGGLE and others operate directly on JSON paths without needing to transfer the whole document.
Spring Data Redis 4.2 adds RedisJsonTemplate
Spring Data released RedisJsonTemplate in version 4.2.0‑M1 (2026.1.0‑M1). It provides a fluent API that mirrors the RedisJSON commands, allowing developers to work with JSON documents using familiar Spring Data patterns:
// Store a Java object as JSON
redisJsonTemplate.set("user:profile:1001", profile);
// Update a single field
redisJsonTemplate.value("user:profile:1001")
.path("$.nickname")
.set("王哥");
// Read a field
String nickname = redisJsonTemplate.value("user:profile:1001")
.path("$.nickname")
.get()
.as(String.class);The template also supports array operations ( .array(...).path("$.tags").append("redis")), boolean toggling ( .bool(...).path("$.enabled").toggle()), conditional writes ( .setIfAbsent, .setIfPresent) and multi‑path reads ( .paths(key, "nickname", "level")), all of which map to the underlying JSON.* commands.
Benefits over the old approach
Reduced network traffic : only the needed field is transferred.
Avoided lost updates : concurrent modifications of different paths no longer overwrite each other.
Simpler code : no manual ObjectMapper handling.
Performance gains : fewer bytes moved and less CPU spent on (de)serialization.
When to use JSON vs. plain strings
Redis JSON is ideal for keys that store a structured object where individual fields are frequently read or updated (e.g., user profiles, product snapshots, shopping carts). Simple KV use‑cases such as verification codes, counters, or locks should continue to use native Redis types for efficiency.
Version considerations
The new API requires a Redis server that supports the JSON data type. Redis 8 includes it natively; older versions (6, 7) need the RedisJSON module or Redis Stack. The Spring Data 4.2 release is a milestone (M1) and not yet stable, so it is recommended to experiment in a non‑production environment and upgrade to the GA release before using it in production.
Practical migration steps
Run a demo with Java 21, Spring Boot 4.2.0‑M1, Spring Data Redis 4.2.0‑M1 and Redis 8.
Identify cache keys where the whole object is currently fetched only to modify a single field.
Replace the GET‑modify‑SET pattern with the appropriate RedisJsonTemplate calls.
Benchmark the latency and throughput improvements.
Verify the Redis version in production supports JSON.
By adopting RedisJsonTemplate, developers can treat fields inside a cached JSON document as first‑class update units, eliminating the need to move the entire object back and forth between Java and Redis.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
LuTiao Programming
LuTiao Programming is a friendly community offering free programming lessons. We inspire learners to explore new ideas and technologies and quickly acquire job-ready skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
