DEV Community

0xpremf
0xpremf

Posted on

Debugging a Redis Connection That Wasn't Actually Broken

So I was building a little Visit Counter app with Spring Boot and Redis nothing fancy, just bump a number every time someone hits a page. Should've been an easy weekend project. Instead I spent a solid hour convinced Redis had personally betrayed me.

Okay so here's what happened

Everything looked fine on the surface. The app ran, no errors, no red text screaming at me in the console. But when I went to actually check what was in Redis, things got weird.

I popped open redis-cli to peek at the data, expecting to see something clean like:

home
Enter fullscreen mode Exit fullscreen mode

What I actually got was this:

"\xac\xed\x00\x05t\x00\x04home"
Enter fullscreen mode Exit fullscreen mode

Yeah. That.

My brain immediately went to the worst-case scenario: "great, Redis is broken, the connection's busted, something's corrupted, I'm going to be here all night." Classic developer panic spiral.

Chasing my own tail

So I checked the connection. Is Redis even running? Yep. Right host, right port? Yep. Can the app actually talk to it? Also yep. Everything I poked at said "this is fine," but the data in Redis very much did not look fine.

This is the annoying part of debugging: the symptom (ugly garbage text in redis-cli) really looks like a broken connection. It's got that "something is corrupted" energy. So naturally I kept circling back to "but is it really connected though?" even after I'd already confirmed it ten times.

Turns out I was debugging the wrong layer entirely.

The actual culprit

Buried in my config, I had this:

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        // no serializers set — oops
        return template;
    }
}
Enter fullscreen mode Exit fullscreen mode

I'd made my own RedisTemplate bean at some point, probably copy-pasted from a tutorial or an old project, and completely forgot Spring Boot already auto-configures one for you. My custom bean took priority, which would've been totally fine... except I never told it how to serialize anything.

Without a serializer set, it just defaults to Java's built-in JdkSerializationRedisSerializer. Which doesn't store plain strings — it stores actual Java object serialization, binary blobs with class metadata baked in. That weird \xac\xed at the start of my "broken" data? That's literally Java's magic number for "hey, this is a serialized object." Redis wasn't corrupted at all. It was doing exactly what I told it to do I just never told it to store strings like a normal person.

Fixing it

Two options here, and honestly either works:

1. just delete the custom bean. Spring Boot's default already handles strings fine. Less code, fewer ways to mess it up.

2. keep your custom bean, but actually configure it properly:

@Configuration
public class RedisConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Primary
    @Bean
    public RedisTemplate<String, String> redisTemplate() {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setHashValueSerializer(RedisSerializer.string());
        return redisTemplate;
    }
}
Enter fullscreen mode Exit fullscreen mode

I went with deleting the custom bean in the end, just to keep things simple. But if you do need a custom one (say, custom connection settings or interceptors), at least set those serializers explicitly so future-you doesn't have this exact same 2 a.m. panic.

Once that was sorted, redis-cli finally showed me what I'd wanted to see the whole time:

127.0.0.1:6379> KEYS *
1) "home"
Enter fullscreen mode Exit fullscreen mode

Beautiful. Readable. No more cursed escape sequences.

What I actually took away from this

Don't assume the dependency is broken just because the output looks broken. Garbage-looking data is way more likely to be a "you told it to do this" problem than a "this tool is fundamentally broken" problem. Tools are usually doing exactly what you configured them to do, even when that configuration was accidental.

THE MOST IMPORTANT Spring Boot's auto-configuration is genuinely good. I didn't need a custom RedisTemplate at all. I made one because I'd seen it in some old tutorial and just assumed it was necessary. Lesson learned: don't override the defaults unless you actually have a reason to.

And serializers are sneaky. They quietly decide what your data looks like once it's stored, completely separate from whether the connection itself is healthy. You can have a perfectly working Redis instance and still end up with data that looks like it came from a different planet, just because of one missing line of config.

If I'd just followed a more boring, structured process from the start, I would've saved myself the hour:

  1. Is the code actually running the path I think it is?
  2. Is the external service (Redis) actually up?
  3. Is the framework configuration doing what I expect (beans, serializers, all that)?

I jumped straight to "Redis must be broken" because that felt like the scariest, most dramatic explanation. In reality, the boring answer, check your own config first — is almost always right. Lesson filed away for next time, even if I probably won't fully learn it until I do this again in six months.

Top comments (0)