Redis Python

Redis python | How to interact with redis using python in 2022

Redis is one of the most popular open-source key-value type storage. A user can interact with redis using multiple languages. In this tutorial, we will learn about the redis python client. We will understand how to interact with redis using python. so let’s get started.

Redis python client

redis-py is the python client a user can use to interact with redis. redis-py requires a running Redis instance so please make sure redis is running in your system. You can follow my tutorial to install redis on your system.

To install the redis-py type the below command

pip install redis

Now, connect to the python shell and type the below command to verify if the installation is successful.

import redis

If you are able to import the packages without any error it means the installation is successful.

Redis python example

Redis Python example

In this session, we will understand how to interact with redis using python and perform various operations on the redis data.

Python redis auth

python support authentication to redis using basic auth, user, and pass auth as well as auth in the SSL-enabled environment.

python redis basis auth

To authenticate to redis we need to pass the redis host, port, and database to connect to. In most cases, the database is db0. You can follow the below tutorial to understand the redis databases.

In this case, the redis runs on host 127.0.0.1 and port 6379. Now type the below command to authenticate to redis.

import redis
r = redis.Redis(host='localhost', port=6379, db=0)

python redis auth using username and password

If the redis is configured to connect using username and password, then type the below command to connect to redis

redis.Redis(host='localhost', port=6380, username='<user>', password='<>pswd')

python redis SSL enabled auth

If the redis instance is configured with SSL, then we need to pass the ca certificate along with the host and port to connect to redis.

r = redis.Redis(host='127.0.0.1', port=6379, db=0,
                    ssl=True,
                    ssl_ca_certs='<path to ca certs>')

Redis python insert a key

To insert a key into the redis using python a user needs to use the set command

r.set('key1', 'value1')

If you are getting TRUE as output, it means the key is successfully inserted.

Redis python insert a key

Redis python get a key

get command is used to retrieve a particular key from the redis database.

r.get('key1')
Redis python get a key

Redis python get all keys

A user can also get all the keys present in redis using the get command.No need to pass any argument in the get command to get all the keys

r.keys()
Redis python get all keys

Redis python scan

The scan is another powerful command that exists in redis. For SCAN/SSCAN/HSCAN/ZSCAN command, py-redis has implemented similar commands which are scan_iter/sscan_iter/hscan_iter/zscan_iter.

Now let’s use the scan_iter command to retrieve all the keys from the redis database.

for key, value in (('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')):
  r.set(key, value)

for key in r.scan_iter():
  print(key, r.get(key))
Redis python scan

Redis python pipeline

Redis Python pipeline

The pipeline is a method using which we can run multiple commands at once without waiting for the response to each command. This improves the redis performance.

The same can be achieved using the redis python client. Below is a simple example of the redis pipeline.

pipe = r.pipeline()
pipe.set('key_1', "value1")
pipe.set('key_2', "value2")
pipe.set('key_3', "value3")
pipe.execute()
Redis python pipeline

Redis python additional resources

You can check the below additional resources to learn more about the redis python client.

Python redis github

Check the redis python GitHub page to understand how the redis python library is written and check the readme for a quick overview of the redis client.

Python redis pypi

Check the Python redis PyPI official doc for some advanced redis python client library usage.

Conclusion

I hope you have liked this brief tutorial on the redis python client. Please do let me know if you are facing any issues while following along

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top