Learn how to fix the “Could not connect to Redis connection refused “ error with ease. Our Server Management Support team is here to help you with your questions and concerns.
Could not connect to Redis connection refused – Fix it easily
What is Redis?
Redis (Remote Dictionary Server) is an open-source program that operates as a message broker, database, and cache. By storing data in RAM, it offers sub-millisecond response speeds, which are perfect for real-time applications. Redis allows for a wide range of use cases, including caching, session management, and real-time analytics, by implementing a variety of data formats, including strings, hashes, lists, sets, and sorted sets. Redis can persist data to disk for durability even if it mostly works in memory. Replication, Redis Sentinel, and Redis Cluster are some of its sophisticated capabilities that guarantee scalability, dependability, and high availability in remote environments.
Understanding “Redis Connection Refused” Error
The “Redis connection refused” message appears when a client fails to establish a connection with the Redis server. This issue usually indicates that the server is not accepting requests or cannot be reached. Several underlying reasons can trigger this error, which are explained in the following sections.
Most webmasters and web hosts that use Redis would have seen this error:
Could not connect to Redis at 127.0.0.1:6379: Connection refused
It is usually caused when the Redis service is stopped in the server.
As part of our Server Management Services for online service providers and website owners, we have seen several other causes for this error.
Today we’ll go through the top causes and fixes for the Redis connection refused error.
Facing Redis connection issues?
What causes the Redis connection refused error?
Redis is used by many to reduce application load time. However, users at times complain that they receive a connection refused error after installing the Redis extension.
The common reasons that trigger the error include:
- Redis-Server not Started
- Firewall Restriction
- Resource usage
Apart from these, there are some alternate reasons that trigger the Redis connection error. Today, let’s look into the reasons behind this error and its fixes.
Redis-Server is not started
One of the most common reasons for seeing the Redis connection refused error is that the Redis server has not started. When the server is inactive, the client cannot establish a connection, leading to this error. To confirm if Redis is running, open your terminal and check its status. If you receive a message that says “Could not connect to Redis at 127.0.0.1:6379: Connection refused,” it clearly means the Redis service is not active. You can start it right away by typing the command redis-server in your terminal.
If Redis is already running but you still face the error, it often means your client is pointing to the wrong host or port. This misconfiguration can easily occur when Redis is hosted in a different environment such as Docker or a remote server. Make sure your connection details are correct before moving on to other fixes.
Here’s what to check:
- Running Redis locally: Use host 127.0.0.1 and port 6379
- Running Redis on Docker: Set host as the Redis service name in your Docker configuration and keep port 6379
By verifying these settings and ensuring Redis is active, you can quickly fix the connection issue and get your Redis server running smoothly again. This simple check often resolves the problem without needing deeper troubleshooting.
Hence, we need to start the Redis server with the correct command for our operating system, like ‘redis-server’ or ‘sudo systemctl start redis’.
For example:
- To begin with, run redis-server or sudo redis-server to start the Redis server.
- Then, if we get a permission error, run the command with sudo.
- In order to run Redis in the background, use this command:
redis-server --daemonize yes - Finally, verify Redis is running by running this command and checking for the “PONG” response:
redis-cli ping
Firewall restriction
Firewall restriction is another common reason that can trigger the “could not connect to Redis connection refused”.
By default, the Redis server listens to the TCP port 6379. If another application is using the port or if the firewall restrictions block the port, the connection refused error can be triggered.
We can fix this with these steps:
- To begin with, check the firewall configuration and make sure the Redis port (6379) is open.
- If another application is using the Redis port, we have to stop that application or configure Redis to use a different port.
Resource usage
Redis uses the main memory to store the data. Thus if the resource in the server is not sufficient for the process to run, it may get terminated abruptly.
When the status of the Redis process is down, it triggers the could not connect to Redis error. We could confirm if it is running with the ps command.
ps -aux | grep redisOur Support Engineers analyze the Redis log file to confirm if the service is repeatedly flapping in the server. From a simultaneous analysis of other log files, we confirm if any other process is consuming resources in the server and is causing Redis to terminate.
Ensure the server has enough resources (RAM, CPU) for Redis to operate.
Alternate reasons
Apart from the reasons mentioned above some settings in the Redis configuration file could also result in the connection failed error. The default location for the configuration file in Ubuntu 18 is /etc/redis/redis.conf.
Some of the reasons that may trigger the connection refused error include:
- The password is set in the Redis configuration file. To fix it, comment the following line.
#requirepass <some pass if any> - If the IP binding is not open for access from the internet in the config. Commenting the following lines will fix the issue.
# bind 127.0.0.1 ::1 - Verify that the correct Redis port settings are configured. The default port is usually 6379.
If the issue recurs, we can try changing the port number in the Redis configuration file to a different value and then restart Redis.
Redis may not be installed
A common cause of the Redis connection refused error is that Redis is not installed on your system. Before connecting, check if Redis is available by opening your terminal and typing redis-cli ping. If you see a command not found, it means Redis is missing. To fix this, install Redis using the instructions on the official Redis website. After installation, run the same command again to confirm that Redis is set up and ready to use.
Restart Redis Server
- If none of the above steps resolve the issue, we can try restarting the Redis server.
- We can stop the Redis server as seen here:
- For Linux:
sudo systemctl stop redis - For macOS:
brew services stop
- For Linux:
Then, wait for a few seconds before starting the Redis server again
Alternative Solution
If you still face the Redis connection refused error, try these quick steps to fix it.
Start by checking if Redis is running:
CODE_BLOCK_PLACEHOLDER_0 If it isn’t active, start the service:
CODE_BLOCK_PLACEHOLDER_1 Next, confirm that Redis is listening on the right port:
CODE_BLOCK_PLACEHOLDER_2You should see 127.0.0.1 on port 6379.
Open the Redis configuration file and verify these lines are present and active:
bind 127.0.0.1
port 6379
Test the connection with:
CODE_BLOCK_PLACEHOLDER_5If you see PONG, Redis is running properly.
Check your firewall settings. For UFW, run:
CODE_BLOCK_PLACEHOLDER_6If it is active, allow Redis:
CODE_BLOCK_PLACEHOLDER_7 Restart Redis after making changes:
CODE_BLOCK_PLACEHOLDER_8 Ensure your app connects using the correct host and port:
redis://127.0.0.1:6379 Prevention Tips
Monitor Redis regularly, enable auto-start at boot using:
CODE_BLOCK_PLACEHOLDER_10 Keep Redis updated, and check logs for errors if issues persist:
CODE_BLOCK_PLACEHOLDER_11 [Are you struggling with Redis errors? We’ll fix it right away.]
Conclusion
In short, a number of reasons can trigger the “Could not connect to Redis connection refused” error. This ranges from firewall restrictions to resource limitations. Today we discussed how our Support Engineers fix the error in each scenario.
I am getting this issue intermittently when my redis is running on a gcp VM (debian 10)?
Hi Saumya,
Please contact our support team via live chat
excellent info. Thanks. Got me going
Getting java.net.ConnectException: Connection refused: no further information while connecting Redis with postgres using Spring Boot and redis dependency in pom.xml.
kindly help.
Thanks.
Hi,
Please contact our support team through live chat (click on the icon at right-bottom).
I am getting the same error,
2024-07-07 00:52:14.271 ERROR 1 — [nio-8390-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost/:6379] with root cause
java.net.ConnectException: Connection refused
Hello Robert,
Our experts are available to assist you with your concerns. We would be delighted to discuss this with you via our live chat feature. Simply click on the icon located in the bottom right corner to get started.