Basic Redis Usage Example - Part 1: Exploring PUB-SUB with Redis & Python

Introduction

In this article, I’m going to show you how you can utilize Redis and combine it with python to implement the Publish-Subscribe Pattern. We’ll learn and use this pattern with a simple Redis usage example. I’m assuming you are familiar with Redis, and also have a good understanding of Python basics. Before, we begin I would like to outline the prerequisites.

Prerequisites

This article assumes you already have redis-server, python 3 and Python Redis client installed on your computer. As the code examples of this article are written and tested using Python 3. Hence, you will need to have python installed to play around with these codes. If you haven’t installed any of those yet please install: Python 3 (If you are a Mac or Linux user then you can skip this step as python is already installed!) Redis-server * Python Redis client ( We will be using andymccurdy’s redis-py which is a very popular Redis client for python 3)

Once, you have installed these successfully. Proceed on the next section!

What is the PUB/SUB Pattern?

Pub/Sub aka Publish-Subscribe pattern is a pattern in which there are three main components, sender, receiver & a broker. The communication is processed by the broker, it helps the sender or publisher to publish information and deliver that information to the receiver or subscriber. According to Wikipedia:

In software architecture, publish-subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead categorize published messages into classes without knowledge of which subscribers, if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.

Let me explain it with an example. Let’s assume, Joe is the owner of a Music Shop where he sells music of different Genres. Alice is a Musician who publishes/sells her music at Joe’s Shop. And, Bob is Joe’s Customer who buys music from Joe. Joe keeps a list of his customers and their interests, hence he knows Bob likes Classical Music. Whenever Alice composes a Classical Music album in Joe’s shop, Joe delivers it to Bob. The interesting part is Bob doesn’t necessarily have to know who created the Music and Alice also doesn’t have to know who listens to her music.

As you can guess here Joe is the Broker, Alice is the Publisher, and Bob plays the role of Subscriber.

Test Environment

Now that we know what the Pub/sub pattern is let’s implement it using python! We will try to implement the Alice-Bob scenario that I’ve explained above. Before we begin let’s test our redis server and python are working properly. Go ahead, and open up a terminal and type:

redis-server

It will fire up the redis-server keep that terminal running. Now open up another terminal/command prompt and type:

python3

It will open python3 interpreter in the terminal and return a prompt. Inside the prompt type:

>>>import redis
>>>r = redis.Redis(host='localhost', port=6379, db=0)
>>>r.set('test_result', 'It is working!')

It should run without any error. Now from a new terminal try the following command:

redis-cli get test_result

It will print the message It is working!

If you face any issue there is a high chance that you have installation issues. So, Go back to prerequisites and install the necessary dependencies again.

Implement Pub/Sub Pattern

The goal of this script is to create a pub/sub pattern that helps users to subscribe to publisher channels and get notified whenever there are updates. First, we will create a python script with the following code:

import redis
# connect with redis server as Bob
bob_r = redis.Redis(host='localhost', port=6379, db=0)
bob_p = bob_r.pubsub()
# subscribe to classical music
bob_p.subscribe('classical_music')

The code is pretty self-explanatory. As you can see we have connected with Redis Server as Bob and we also subscribed to the channel classical_music. Now let’s write the code for Alice as well:

# connect with redis server as Alice
alice_r = redis.Redis(host='localhost', port=6379, db=0)
# publish new music in the channel epic_music
alice_r.publish('classical_music', 'Alice Music')

Just like Bob, we have created a new connection to the server to interact with it as Alice. By using the ‘publish’ method Alice can now publish music on the classical_music Channel. Bob, on the other hand, can easily fetch publish music using the get_message method:

# ignore Bob subscribed message
bob_p.get_message()
# now bob can find alice’s music by simply using get_message()
new_music = bob_p.get_message()['data']
print(new_music)

If Alice publish another music Bob can grab it by using the get_message method again

# Alice published another
alice_r.publish('classical_music', 'Alice 2nd Music')
#Lets grab it!
another_music = bob_p.get_message()['data']
print(another_music)

Conclusion

The above example is very basic, demonstrates how publish subscribe pattern actually works. Any publisher can publish through the channel and, the subscriber will receive all the new messages that are published on that particular channel. Note that, Redis is playing a major role as Joe (message broker) here. Redis handles the communication between publisher and subscriber without any hassle.

There are plenty of other things that you can achieve using Redis and Python combination. To get a list of things that you can do with the Redis’s python client check out the readme of the repository.

Pilot the ObjectRocket platform free for 30 Days

It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!

PILOT FREE FOR 30 DAYS

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.