Python Redis

Make Python Redis super easy with Redis Labs

Redis Labs’ products and services enable running Redis datasets in a highly available and auto-scalable manner, with predictable top performance.

The Redise Pack lets you install an enterprise grade Redis cluster in your environment of choice, whether an on-premises data-center or your preferred cloud platform. It gives you full control of your data and configuration – no clustering or sharding knowledge required!

Redise Cloud is a fully-managed cloud service for hosting and running Redis dataset without dealing with nodes, clusters, failure recovery or performance stabilization. Our technology does all that in a fully automated manner. Redise Cloud is available on all popular clouds and platforms-as-a-service.

Using Redis with Python

In order to use Redis with Python you will need a Python Redis client. In following sections, we will demonstrate the use of redis-py, a Redis Python Client. Additional Python clients for Redis can be found under the Python section of the Redis Clients page.

Installing redis-py

redis-py’s installation instructions are given in the “Installation” section of its README file. Use pip to install redis-py:

$ sudo pip install redis

You can also download the latest redis-py release from the GitHub repository. To install it, extract the source and run the following command:

$ cd redis-py
~/redis-py$ sudo python setup.py install

Opening a Connection to Redis Using redis-py

The following code creates a connection to Redis using redis-py:

import redis

r = redis.Redis(
    host='hostname',
    port=port, 
    password='password')

To adapt this example to your code, make sure that you replace the following values with those of your database:

  • In line 4, host should be set to your database’s hostname or IP address
  • In line 5, port should be set to your database’s port
  • In line 6, password should be set to your database’s password

Connection Pooling with redis-py

redis-py provides a connection pooling mechanism as explained in the Connection Pools section of its README file. Since connection pooling is enabled by default, no special actions are required to use it.

Using SSL and redis-py

redis-py is the second Redis client that natively supported SSL. Use the SSLConnection class or simply instantiate your connection pool using a rediss://-URL and the from_url method, like so:

r = redis.Redis( url='rediss://:password@hostname:port/0',
    password='password',
    ssl_keyfile='path_to_keyfile',
    ssl_certfile='path_to_certfile',
    ssl_cert_reqs='required',
    ssl_ca_certs='path_to_ca_certfile')

Reading and Writing Data with redis-py

Once connected to Redis, you can start reading and writing data. The following code snippet writes the value bar to the Redis key foo, reads it back, and prints it:

# open a connection to Redis
...

r.set('foo', 'bar')
value = r.get('foo')
print(value)

The output of the above code should be:

$ python example_redis-py.py
bar

For more information on using Redis Labs’ products and services with Python please see the Howto page.