Python: How to Use Redis

Redis is an in-memory data structure store that is used for caching, real-time analytics, message brokering, and more. It is an open-source…

Python: How to Use Redis
Photo by Tobias Fischer on Unsplash

Redis is an in-memory data structure store that is used for caching, real-time analytics, message brokering, and more. It is an open-source software that is designed to support various data structures such as strings, hashes, lists, sets, and sorted sets, with optional durability.

One of the primary benefits of Redis is its high performance and ability to handle a large amount of data in real-time. It can support millions of operations per second and is often used as a cache to speed up data access in web applications.

Redis is also known for its versatility, as it can be used in a variety of use cases, including:

Caching

Redis can be used to cache frequently accessed data in memory, which reduces the need to hit a database or other slow storage system.

Real-time analytics

Redis can be used to process and analyze large volumes of data in real-time, making it useful for applications such as social media platforms or e-commerce websites.

Message brokering

Redis can be used as a message broker to transmit messages between different components of an application or between different applications.

Job queues

Redis can be used to queue up tasks or jobs to be processed asynchronously, which can be useful for tasks such as sending emails or processing images.

How to install Redis

If Redis is not allready installed you can install Redis in ubuntu with apt-get

$ sudo apt-get install redis

Once you have installed Redis, you can start the Redis server by running

$ sudo systemctl start redis

The server will start listening for connections on the default redis port (6379)

To stop the Redis server, you can use the following command

$ sudo systemctl stop redis

Install Redis Python modules

$ pip3 install redis

Using Redis as message broker: Sending messages

To send messages using Redis, we can use the rpush command to push messages onto a list in Redis. Let's create a Python script that sends a message every second to a Redis list called messages.

import time 
import redis 
# Create a connection to the Redis server 
r = redis.Redis() 
while True: 
    # Send a message to the 'messages' list 
    r.rpush('messages', 'Hello') 
    time.sleep(1)

This script will send a message to the messages list every second.

Using Redis as message broker: Receiving messages

To receive messages from Redis, we can use the blpop command to pop messages off the list. Let's create a Python script that receives messages from the messages list and prints them to the console.

import redis 
# Create a connection to the Redis server 
r = redis.Redis() 
while True: 
    # Wait for a message to be available in the 'messages' list 
    message = r.blpop('messages') 
    print(message)

This script will wait for a message to be available in the messages list, and then print the message to the console when it becomes available.

Full example

Here’s the complete example of two Python scripts communicating using Redis as a message broker:

sender.py:

import time 
import redis 
# Create a connection to the Redis server 
r = redis.Redis() 
while True: 
    # Send a message to the 'messages' list 
    r.rpush('messages', 'hello') 
    time.sleep(1)

receiver.py:

import redis 
# Create a connection to the Redis server 
r = redis.Redis() 
while True: 
    # Wait for a message to be available in the 'messages' list 
    message = r.blpop('messages') 
    print(message)

To run these scripts, open two terminal windows and run the sender.py script in one window and the receiver.py script in the other. You should see the receiver.py script printing out the messages that sender.py inserted to the queue.

I hope you found this article useful and make your first steps with Python and Redis

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…

In Plain English

Thank you for being a part of our community! Before you go: