Connecting to Redis remotely

Within a server farm Redis can have multiple uses such as a fast cache or publish-subscribe system. In this guide we'll show how you can set up multiple servers to connect to a single central Redis instance. We will simulate a server farm using virtual machines.

Note: On production level instances we recommend using Linux, however for testing/development Windows can be used as well. For this guide we'll be using Windows Server 2016.

This guide assumes that you already have Redis installed. If not please have a look at our knowledge base article, which provides detailed instructions including a download link to the correct version of Redis. Let's get started!

Allowing remote connections

In order for our remote servers to connect to our central Redis instance we need to set 2 things:

  1. Add an exception in the firewall to allow traffic directed at the port Redis is using (in our case 6379)
  2. Change Redis configuration to only listen to connections made to the IP of the server it's being hosted on.

Adding firewall exception

Adding a firewall exception follows the same process that we covered in a previous guide, except this time we're going to add an inbound rule to allow incoming connection to port 6379 (the default port Redis uses).

Changing Redis configuration

To change Redis configuration, let's locate the Redis installation folder on the server it was installed on. When installed on Windows Redis automatically starts a service which uses 'redis.windows-service.conf' configuration file, therefore we're going to add changes to it:

Locate Redis configurtion file

Once opened, we're going to find and edit the bind statement from using 127.0.0.1 to using the IP address of the server we're currently on. In our case our server IP is 10.0.1.20, thus we're going to change it to that. Be sure that your IP address is static and doesn't change over time, otherwise you'd have to constantly keep changing the configuration when sending and receiving data.

Note: On Windows you can quickly check your IP address by running ipconfig command in the command line.

Change the bind configuration

Now, we'll restart Redis service for our changes to take place:

  1. Click Ctrl + R.
  2. Type in 'services.msc', which should show you the services console.
  3. Locate Redis in the list of services, right click it and select 'Restart':

Restart Redis service

Testing remote connection

Now it's time to test if we can send messages to our Redis instance remotely. For this we're going to copy the 'redis-cli.exe' file from our Redis installation folder to our second server. This file will provide an interface to test our remote connection by sending a 'ping'.

We're going to open a command prompt and simply drag and drop the 'redis-cli.exe' file on it to get the executable file path, which we'll execute with the following commands -h 10.0.1.20 -p 6379 ping. Note, how we're using -h followed by the IP address of our Redis server and -p with the port number we wish to send message through. This should result in a 'PONG' response from our Redis instance:

'Pong' response from Redis instance

And now we have remote connection to our Redis server!