Redis client

Redis client#

You’ll interact with Redis through a special Python object called a Redis client. This page focuses on the various methods of creating and monitoring Redis clients.

import redis

Invalid connection#

During creation redis absolutely doen’t give reasons to think that it has some problems with connection to the redis server. It happens because it actually connects only during requests.


The following cell shows creation of the redis client to the unexisting host with unexisting port.

redis_client = redis.Redis(host="this host doesn't exist", port=666)
redis_client
Redis<ConnectionPool<Connection<host=this host doesn't exist,port=666,db=0>>>

There are no errors, and we even receive a Redis client object in return.

A legitimate way to check if the connection is fine is to use the ping method of the Redis client object. It raises a redis.exceptions.ConnectionError if the connection is incorrectly specified.

try: redis_client.ping()
except Exception as e: print(type(e))
<class 'redis.exceptions.ConnectionError'>