What the heck does Pub/Sub mean?
How do we implement the Publish-Subscribe pattern in Redis?
Prerequisites:
- You don't need to know anything before reading this post.
- A Redis Instance.
What is Publish-Subscribe?
Publish-Subscribe, or also commonly known as Pub/Sub, is a pattern to implement data streaming. It is super fast and asynchronous in nature.
How does it work?
You'd basically have a queue called Channel
where messages are transmitted.
Then there are Publishers
who publish/send the message and Subscribers
who receive the message.
The Publisher
would send a message into the channel and whoever's (Subscribers
) subscribed to the Channel
would receive that message.
The Publisher
would not know about who is subscribing to the channel and the Subscriber
would not know about who is publishing a particular message.
This removes all the work of programming the subscription mechanism.
A big problem with this pattern is, the Channel
is a SPOF(Single Point Of Failure) if the Channel
somehow crashes or breaks down.
To understand this pattern more, let us get our hands dirty by using the Pub/Sub feature of Redis.
Pub/Sub in Redis?
We all know that Redis is an In-Memory Key-Value Database. But Redis also has the feature of Pub/Sub.
If you have installed Redis, start the server and we are good to go.
Let us create a Pub/Sub for publishing a list of favorite TV Series.
To publish a message in the
tv_series
channel, we can use thePUBLISH
command.127.0.0.1:6379> PUBLISH tv_series the_big_bang_theory (integer) 0
This sends the
the_big_bang_theory
message into thetv_series
channel.
This command returns an integer denoting the number of subscribers that received the message. There are no subscribers as of now, so we should get the return value of 0.To subscribe to the
tv_series
channel, we can use theSUBSCRIBE
command.127.0.0.1:6379> SUBSCRIBE tv_series Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "tv_series" 3) (integer) 1
We can subscribe to multiple channels at a time by just adding more channel names with space separation.
We are now subscribed to thetv_series
channel. The integer return is denoting the ith channel number we have subscribed to.But we can't see the message
the_big_bang_theory
we have published in the channel. It is because we have subscribed to the channel after we have published the message. To be able to read a message. we have to have a subscriber in the channel while publishing the message.
So let us publish the message again.127.0.0.1:6379> PUBLISH tv_series the_big_bang_theory (integer) 1
This time we can see the return value is
1
as there is a subscriber currently in the channel, and also the message is printed in the output of the subscriber.127.0.0.1:6379> SUBSCRIBE tv_series Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "tv_series" 3) (integer) 1 1) "message" 2) "tv_series" 3) "the_big_bang_theory"
The output says we have received a message from the
tv_series
channel withthe_big_bang_theory
as the message.To see the state of the Pub/Sub system, we can use the
PUBSUB
command.127.0.0.1:6379> PUBSUB CHANNELS 1) "tv_series"
PUBSUB CHANNELS
gives us what channels that are currently there.127.0.0.1:6379> PUBSUB NUMSUB tv_series 1) "tv_series" 2) (integer) 1
PUBSUB NUMSUB <channel>
gives us the number of subscribers that a channel has.To stop receiving messages, we can unsubscribe from the channel by using the
UNSUBSCRIBE
command or by pressingCTRL + C
if you are using the CLI.127.0.0.1:6379> UNSUBSCRIBE tv_series
So, this is a basic demonstration of Pub/Sub in Redis. We'll look at another concept called Patterned Subscriptions in another blog post.
What can I try?
- Try subscribing to multiple channels at the same time and observe the output.
- Try doing the same thing from Python or JavaScript clients instead of CLI.
Reference Docs:
Thanks for reading the post, have a good day, and stay safe.