Pub/Sub in Redis
Share:
Consider this the start of our journey into the fascinating world of Redis Pub/Sub. This architectural pattern, also known as publish/subscribe, is widely used for its efficiency in message distribution between various systems. In a typical Pub/Sub system, the publisher does not send messages directly to subscribers. Instead, messages are characterized into classes, without the knowledge of what, if any, subscribers there might be. Similarly, subscribers express an interest in one or more classes, and only receive messages that are of interest, without knowledge of what, if any, publishers there might be. Redis' implementation of the Pub/Sub model provides a simple and effective messaging system.
Let's start exploring the use of Redis Pub/Sub with a film-themed example. We'll develop a scenario where a film production studio uses Pub/Sub to inform its staff about various aspects such as new movie releases or upcoming auditions.
Let's initialize Redis in the terminal:
redis-cli
Publishing to Channels
Imagine that we are a production team planning to launch a new movie. We'd like to notify different departments about new updates. For this, we will use Redis' PUBLISH
command.
Here's a simple command to send a message:
PUBLISH new-releases "The new Space Adventure movie is releasing on May 15th!"
In this command, new-releases
is our channel name, and the "The new Space Adventure movie is releasing on May 15th!" is our message.
Subscribing to Channels
Now, imagine we are part of the marketing team eagerly waiting to know about new movie launches. We can subscribe to the new-releases
channel to receive updates:
SUBSCRIBE new-releases
Once we execute this command in Redis, we'll start listening for new messages published to the new-releases
channel. Any message published to this channel will instantly be received by the subscriber.
Unsubscribing from Channels
There might be scenarios wherein a specific department no longer needs updates from a particular channel. For instance, if the VFX team is done with all its tasks for a movie-themed Space Adventure
, they might wish to unsubscribe from its channel:
UNSUBSCRIBE new-releases
With this command, the VFX team will no longer receive any messages published to the new-releases
channel.
Pattern-based Subscriptions
Redis Pub/Sub also allows pattern-based subscriptions which can be a powerful tool for filtering messages based on patterns of interest. For example, you might be interested in any updates about auditions. You can subscribe to all channels that match the 'auditions-*' pattern like below:
PSUBSCRIBE auditions-*
Using this pattern, the production team can easily categorize messages to different audition channels like auditions-actors
, auditions-stunts
, auditions-extras
and the subscribers will receive relevant updates.
Redis Pub/Sub in Python
If you want to further automate the process of publishing and subscribing to Redis, it can be done using various programming languages like Python. Here, we will look into executing the Pub/Sub commands using Python's redis-py
library.
Start by importing the redis module and establishing a connection:
import redis
r = redis.Redis()
To publish a message through Python, you use the publish
method. Let's inform departments about a new audition:
r.publish('auditions-actors', 'Auditions for a new hero role will be held on June 20.')
To subscribe to a channel using python, we will need to create a pubsub object and then subscribe to a channel.
p = r.pubsub()
p.subscribe('new-releases')
In python, the get_message
method can be used to check for new messages:
message = p.get_message()
print(message)
This code will print the latest message received by the subscribed channel - if there is one.
In conclusion, the Pub/Sub model in Redis offers a fantastic way to develop a messaging system within your applications. It can help decouple your applications while allowing efficient communication between them. It is simple to set up, easy to use, and scales well with your needs. Don't forget to explore more with different channels, patterns, and possibilities. Happy coding!
0 Comment
Sign up or Log in to leave a comment