Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is the difference between RedisTemplate and StringRedisTemplate, and why do keys sometimes appear as unreadable bytes in Redis?

RedisTemplate, the generic template Spring Boot configures as RedisTemplate<Object, Object>, serializes keys and values with Java's built-in JDK serialization by default, so a key like user:42 is stored as binary bytes that start with \xac\xed. StringRedisTemplate is the same template preconfigured with String serializers for keys and values, so what we write is what we see in redis-cli. To get readable and portable data with objects, we configure serializers explicitly, typically String keys and JSON values.

Why this question is asked

Almost every team that adds Redis to a Spring application meets this the first time someone opens redis-cli and cannot find the keys the application wrote. It looks like a bug, but it is just a default. Interviewers ask about it because it reveals whether we understand serialization, the step that turns Java objects into bytes, and the hidden coupling JDK serialization brings. In this answer, we will recap what the templates do, reproduce the unreadable keys, explain the bytes, configure proper serializers, and look at why JDK serialization is a poor format for shared data. We will also mention which client library sits underneath.

1A Quick Recap: What the Templates Do

Redis itself stores only bytes: a key is a byte array, and a value is a byte array, list of byte arrays, hash of byte arrays and so on. A Java application works with objects. A RedisTemplate is Spring Data Redis's helper class that sits in between: we call methods such as opsForValue().set(key, value), and the template uses a serializer, an object that converts a Java object to bytes and back, for the key and another for the value.

Spring Boot auto-configures two beans when spring-boot-starter-data-redis is on the classpath. One is a RedisTemplate<Object, Object> named redisTemplate, which has no serializers set and therefore falls back to JdkSerializationRedisSerializer for everything. The other is StringRedisTemplate, a subclass of RedisTemplate<String, String> that uses a String serializer, UTF-8 by default, for keys, values, hash keys and hash values. Underneath both is Lettuce, the Redis client library Spring Boot uses by default, which handles the network connection; Jedis is the alternative if we exclude Lettuce and add it.

2Reproducing the Unreadable Keys

Let us write the same key with both templates and look at what Redis holds.

Java
redisTemplate.opsForValue().set("user:42", "Alice"); // RedisTemplate<Object, Object>
stringRedisTemplate.opsForValue().set("user:43", "Bob"); // StringRedisTemplate
// redis-cli
> KEYS *
1) "\xac\xed\x00\x05t\x00\auser:42"
2) "user:43"
> GET user:42
(nil) // the plain key does not exist
> GET user:43
"Bob"

The first key is not user:42 at all. It is the Java serialization of the String object user:42. Looking up the plain text key in redis-cli, or from a Python service, finds nothing, which is why it looks as if the data disappeared.

3What Those Bytes Mean

JDK serialization writes a stream format with a header. The bytes \xac\xed are the stream's magic number and \x00\x05 is the stream version, so every key written this way starts with the same four bytes. Then t, which is the byte 0x74, marks a String, followed by two bytes for the length, \x00\x07, which redis-cli prints as \x00\a because 0x07 is the bell character. Only then come the characters of our key.

For values that are objects, it gets worse: the stream contains the full class name, a serialVersionUID and the field layout, so a small Product object can take several times the space its data needs.

One key, two serializers
SerializerBytes stored for "user:42"Readable elsewhere?
JdkSerializationRedisSerializerac ed 00 05 74 00 07 75 73 65 72 3a 34 32No, Java only
StringRedisSerializer (UTF-8)75 73 65 72 3a 34 32Yes, any client

4Configuring Serializers Explicitly

When we want to store objects rather than plain strings, we define our own template with String keys and JSON values. Keys stay readable, and values become JSON documents any language can read.

Java
@Bean
RedisTemplate<String, Product> productTemplate(RedisConnectionFactory cf, ObjectMapper mapper) {
RedisTemplate<String, Product> t = new RedisTemplate<>();
t.setConnectionFactory(cf);
t.setKeySerializer(RedisSerializer.string());
t.setValueSerializer(new Jackson2JsonRedisSerializer<>(mapper, Product.class));
t.setHashKeySerializer(RedisSerializer.string());
t.setHashValueSerializer(RedisSerializer.string());
return t;
}
productTemplate.opsForValue().set("product:7", new Product("7", "Lamp", new BigDecimal("19.90")));
// redis-cli> GET product:7
// "{\"id\":\"7\",\"name\":\"Lamp\",\"price\":19.90}"

Notice that we set the hash serializers too; forgetting them is a common reason why hash fields still appear as binary after the keys were fixed. A typed Jackson serializer for one class produces clean JSON. The generic JSON serializer, which handles any type, adds a type property such as @class to each value so it can deserialize it, which is convenient but again couples the data to Java class names. With Spring Data Redis 4 and Jackson 3 the JSON serializer classes have new names, so check your version.

5Why JDK Serialization Is a Poor Choice for Shared Data

Readability is only part of it. JDK serialization couples the stored data to the exact Java class: rename a package, change a field, or forget a serialVersionUID, and deployed instances can no longer read what older instances wrote, failing with an InvalidClassException during a rolling deployment. It also makes the data unusable for services written in other languages, and Java deserialization of untrusted bytes has a long security history. JSON, or a compact schema-based format, avoids all three.

6Follow-up questions

When is StringRedisTemplate enough?

Whenever keys and values are text or numbers we format ourselves: counters with increment, flags, tokens, rate-limit buckets, or JSON we serialize explicitly with our own ObjectMapper. Many teams use only StringRedisTemplate and convert objects to JSON in their own code, which keeps the format fully under their control.

We fixed the serializer; why can we still not read old keys?

Because the existing data was written in the old format and Redis just stores bytes. The new template looks up plain keys and finds nothing. We either let the old keys expire through their TTLs, or run a one-off migration that reads with the JDK serializer and rewrites with the new one.

Does Spring Cache with Redis have the same issue?

Yes. Spring Boot's Redis cache manager uses String keys but JDK serialization for values by default, so cached objects must be Serializable and appear as binary. We can set a JSON value serializer through RedisCacheConfiguration.

7Conclusion

In this answer, we saw that RedisTemplate<Object, Object> falls back to JDK serialization, which turns even a String key into binary bytes beginning with the stream header \xac\xed, while StringRedisTemplate writes keys and values as UTF-8 strings. We configured a template with String keys and JSON values, including the hash serializers, and saw why JDK serialization also couples data to Java class versions and to Java itself. Underneath both templates, Spring Boot uses the Lettuce client by default.

What to say in the interviewAbout 80 seconds aloud

Open with

Both are the same template class, the difference is the serializers. Redis only stores bytes, and the template uses a serializer to turn the key and value into bytes.

Then, if they want more

The RedisTemplate that Spring Boot auto-configures is typed Object, Object and has no serializers set, so it falls back to JDK serialization. That means even a String key like user:42 is stored as a Java serialization stream: it starts with the magic bytes ac ed 00 05, then a type marker and length, and only then the text.

In redis-cli you see backslash-x-ac-backslash-x-ed and a GET on the plain key returns nil, so it looks like the data vanished. StringRedisTemplate is preconfigured with UTF-8 String serializers for keys, values and hash fields, so what you write is what you see.

When I need to store objects, I define my own template with a String key serializer and a JSON value serializer, and I set the hash serializers too, because people forget those.

Beyond readability, JDK serialization ties the stored data to the exact Java class, so a field change can break reads during a rolling deploy with an InvalidClassException, and no other language can read it. JSON avoids that. Underneath, Spring Boot uses Lettuce as the Redis client by default.

About this answer

Last updated
12 November 2026
Versions it names
Redis 4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Data reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between RedisTemplate and StringRedisTemplate, and why do keys...": https://scrivio-interview-library.netlify.app/databases/spring-data-nosql/3dc9a9720b/