Skip to content

How to resolve 'RedisMessageListenerContainer : Connection failure occurred' error when using redis in springboot apps

Problem

When starting a Spring Boot application with a Redis connection, you might encounter the following error:

Terminal window
vm 1 | 2021-02-26 09:08:42.910 ERROR 4060 --- [ container-1] o.s.d.r.l.RedisMessageListenerContainer : Connection failure occurred. Restarting subscription task after 5000 ms
^Cwrapper | INT trapped. Shutting down.

The core error is:

RedisMessageListenerContainer : Connection failure occurred. Restarting subscription task after 5000 ms

Why does this error occur? The Spring Boot application is correctly configured, I promise!

Environment

  • Spring Boot 1.x and 2.x
  • JDK 8

Configuration

Let’s check the configuration properties of the Spring Boot application:

application.properties
redisHost=12.43.2.1
redisPort=6379
spring.redis.host=${redisHost}
spring.redis.port=${redisPort}

You can see that our Redis server’s IP address is 12.43.2.1, and the port is the default Redis port 6379.

Debug

First, we check the network connection from the client to the Redis server:

Terminal window
telnet 12.43.2.1 6379
Trying 12.43.2.1...
Connected to 12.43.2.1.
Escape character is '^]'.
quit
+OK
Connection closed by foreign host.

The Redis connection from the client to the server is okay, and the Redis server’s network connection is ready to be used. Why can’t our application connect to it?

Reason

After checking for almost an hour, I found the cause of the problem: there is a space character behind the IP address of the Redis server!

Solution

We should remove the space from the IP address:

The original (for display purposes, I use [space] to denote the space character):

application.properties
redisHost=12.43.2.1[space]
redisPort=6379

After modification:

application.properties
redisHost=12.43.2.1
redisPort=6379

Run the app again, and no error messages appear. It works!

Summary

In this post, we explored how to resolve the RedisMessageListenerContainer: Connection failure occurred error in Spring Boot applications using Redis. The issue was caused by a trailing space in the Redis host configuration. By removing the space, the application successfully connected to the Redis server. Always double-check your configuration files for such subtle issues, as they can lead to frustrating debugging sessions.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!