In various scenarios, such as after creating a new cluster or upgrading the cluster, it is highly advisable to verify client connectivity to the database.

To test client connectivity:

  1. Create a Redis database and get the database endpoint, which contains the cluster name (FQDN).
  2. Try to connect to the database endpoint from your client of choice, and execute commands against the database.
  3. If the database does not respond, try to connect to the database endpoint by using the IP address rather than the FQDN; if you succeed, it means that the DNS is not properly configured. For additional details, refer to DNS.

If any issues are encountered during the connectivity test, contact our support at [email protected].

Test connecting to your database

With the Redis database created, you are ready to connect to your database to store data. You can use one of the following ways to test connectivity to your database:

  • Connecting with redis-cli, the built-in command-line tool
  • Connecting with a Hello World application using Python.

Connecting using redis-cli

Run redis-cli, located in the /opt/redislabs/bin directory, to connect to port 12000 and store and retrieve a key in database1

# sudo /opt/redislabs/bin/redis-cli -p 12000
127.0.0.1:16653> set key1 123
OK
127.0.0.1:16653> get key1
"123"

Connect with a simple Python app

A simple python application running on the host machine can also connect to the database1.

Note:
The following section assumes you already have python and redis-py (python library for connecting to Redis) configured on the host machine running the container. You can find the instructions to configure redis-py on the github page for redis-py.

In the command-line Terminal, create a new file called redis_test.py

vi redis_test.py

Paste the following into a file named redis_test.py.

import redis

r = redis.StrictRedis(host='localhost', port=12000, db=0)
print ("set key1 123")
print (r.set('key1', '123'))
print ("get key1")
print(r.get('key1'))

Run “redis_test.py” application to connect to the database and store and retrieve a key using the command-line.

python redis_test.py

The output should look like the following screen if the connection is successful.

set key1 123
True
get key1
123

Test database connectivity with a simple application

You can also use a simple application to test connectivity to your database. Here is a simple python app that connects to the database by IP address. The app uses the discovery service that is compliant with Redis Sentinel API.

In the IP-based connection method, you only need the database name, not the port number. Here we simply use the discovery service that listens on port 8001 on all nodes of the cluster to discover the endpoint for the database named “db1”.

from redis.sentinel import Sentinel

# with IP based connections, a list of known node IP addresses is constructed
# to allow connection even if any one of the nodes in the list is unavailable.
sentinel_list = [
('10.0.0.44', 8001),
('10.0.0.45', 8001),
('10.0.0.45', 8001)
]

# change this to the db name you want to connect
db_name = 'db1'

sentinel = Sentinel(sentinel_list, socket_timeout=0.1)
r = sentinel.master_for(db_name, socket_timeout=0.1)

# set key "foo" to value "bar"
print r.set('foo', 'bar')
# set value for key "foo"
print r.get('foo')

In the URL-based connection method, you need to specify the endpoint and the port number for your database.

import redis

# the URL provided to redis. Redis method comes from the database configuration
# property called "Endpoint". The endpoint URL generated by the database is a
# combination of the cluster name (FQDN) and database port number.
r = redis.Redis(
host='redis-19836.c9.us-east-1-2.ec2.cloud.redislabs.com',
port=19836)

# set key "foo" to value “bar”
print(r.set('foo', 'bar'))
# set value for key "foo"
print(r.get('foo'))