Redis and Python

Installing the Driver

redis-py is the recommended driver for Python. It can be installed as a stand-alone package through pip:
pip install redis

Connecting

The redis-py driver will parse a connection string for you if you supply the url. Specifically, the information from the Connection Strings provided with your deployment. The default unencrypted connection will be prefixed with redis:// and can be passed as a url to the client constructor.

redis_url = 'redis://admin:{$PASSWORD}@aws-us-east-1-portal.9.dblayer.com:24026'
r = redis.StrictRedis.from_url(redis_url)

SSL/TLS

Compose Redis deployments can be SSL/TLS enabled by provisioning an SSL enabled HAProxy portal on the Security panel of your deployment. These connections are secured using a Let's Encrypt certificate, or in rare cases, a self-signed certificate. If you want to make use of an SSL/TLS connection, you will want to slightly modify your connection set-up by passing your connection string as arguments directly to the client constructor.

r = redis.StrictRedis(
    host='portal1037-29.adept-redis-42.compose-34.composedb.com',
    port=24026,
    password='{$PASSWORD}',
    ssl=True,
)
r = redis.StrictRedis(
    host='portal1037-29.adept-redis-42.compose-34.composedb.com',
    port=24026,
    password='{$PASSWORD}',
    ssl=True,
    ssl_cert_reqs=required,
    ssl_ca_certs=/path/to/redis.crt,  
)

Full-example Code

There is an example for a Let's Encrypt SSL/TLS connection, an default/unencrypted connection, and a self-signed certificate SSL connection.

import redis

# connection string and initialization
r = redis.StrictRedis(
    host='portal1037-29.adept-redis-42.compose-34.composedb.com',
    port=24026,
    password='{$PASSWORD}',
    ssl=True,
)

#test the connection
r.set('hello', 'world')
value = r.get('hello')
print(value)
import redis

# connection string and initialization
redis_url = 'redis://admin:{$PASSWORD}@aws-us-east-1-portal.9.dblayer.com:24026'
r = redis.StrictRedis.from_url(redis_url)

#test the connection
r.set('hello', 'world')
value = r.get('hello')
print(value)
import redis

# connection string and initialization
r = redis.StrictRedis(
    host='portal1037-29.adept-redis-42.compose-34.composedb.com',
    port=24026,
    password='{$PASSWORD}',
    ssl=True,
    ssl_cert_reqs=required,
    ssl_ca_certs=/path/to/redis.crt,
)

#test the connection
r.set('hello', 'world')
value = r.get('hello')
print(value)

More Information


Still Need Help?

If this article didn't solve things, summon a human and get some help!