1. Overview

Redis is a popular in-memory data store that can be used as a database, cache, and message client.

However, once we install Redis, problems sometimes occur when trying to connect to the Redis server for the first time.

In this tutorial, we’ll study ways to check if Redis works correctly for both local and remote connections. Moreover, we’ll learn how to fix connection problems if they occur.

2. Check Local Setup

Firstly, let’s check if our Redis works correctly on localhost.

To that end, let’s use the standard Redis ping command:

$ redis-cli
127.0.0.1:6379>ping
PONG
127.0.0.1:6379>

Usually, this step works correctly without any extra network setup. If we get an error at this point, then Redis most likely isn’t present on our machine.

To install both Redis client and server, we can use the apt-get command with sudo:

$ sudo apt-get install redis

Now, we should be able to repeat the local ping-pong test without errors.

3. Check Remote Setup

Next, we can check if our client can ping a remote Redis server correctly.

For that, we’ll start the Redis client with the -h option, and we’ll specify the remote server IP address:

$ redis-cli -h <server-ip>
<server-ip>:6379> ping
PONG
<server-ip>:6379>

If we see the pong response arriving as above, then our connection is successful.

However, if we see no pong response here, or if there is a Connection Refused error, that means that the Redis server restricts access to itself.

In the next sections, we’ll see how to fix some common server connection problems.

4. Update Server Bind Address

To fix the Redis server Connection Refused problem, we need to update the Redis config file, which is typically located at /etc/redis/redis.conf.

In particular, we’ll change a default line:

bind 127.0.0.1

In place of the local 127.0.0.1, we can place a public or a catch-all IP address:

bind 0.0.0.0

The IP address 0.0.0.0 means that the server will now listen to all incoming IP addresses rather than only localhost.

5. Check Firewall Rules

Often, the firewall also blocks our connection attempts. Therefore, it’s essential to see whether the Redis default port 6379 is being allowed by our firewall.

For example, let’s use ufw, the Uncomplicated Firewall. In particular, to check if the Redis port is open, we can look at the output of ufw status:

$ sudo ufw status
Status: active

In this case, we can see that the firewall is active, but it doesn’t seem to be allowing access via the Redis ports.

So, we’ll allow inbound traffic through TCP port 6379:

$ sudo ufw allow 6379/tcp
Rule added
Rule added (v6)

Now, the rules have been added. Let’s check the ufw status again:

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
6379/tcp                   ALLOW       Anywhere                  
6379/tcp (v6)              ALLOW       Anywhere (v6)  

As we can see, the firewall should now allow all the traffic through our Redis port.

6. Restart Redis

After we make the above changes, it’s usually best to restart Redis using the systemctl command:

$ sudo systemctl restart redis.service
$ sudo systemctl is-active redis
active

Now, Redis is running with new settings.

We can repeat the remote connection check. We should now be able to ping-pong the remote server correctly.

7. Security Warning

The above steps make the Redis server open to all inbound connections. So, it’s essential to take measures to avoid security risks.

For that, we can enable password access and turn on protected mode in the Redis config file:

requirepass <your password>
protected-mode yes

The above options will restrict unauthorized access to the Redis server thus enhancing the security of our system. Importantly, protected-mode may actually restrict access more than desired in some cases.

8. Conclusion

In this article, we learned how to check the Redis connection to a remote server.

First, we studied the commands to diagnose Redis local and remote connections. After that, we looked at the Redis server bind IP address to fix the remote connections.

Then, we learned how to check if the firewall allows traffic through the Redis ports. Finally, we saw how to avoid security issues while enabling access.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.