Introduction To Pub Sub in Redis
Posted By : Navin Purohit | 28-Jun-2018
Introduction of pub/sub in Redis
Redis is usually known for key/value store. But it not true that it is used only for that it has so many little utilities that make it a joy to explore and implement. One of them publish/subscribe commands which help to do messages and communication between different processes. There are so many messaging systems out there, but Redis introduce pub/sub as a feature after 2.0 version. Implementation of pub/sub in Redis is lean and simple and it easy to use where Redis is installed.
Pub/sub (or publish/subscribe) is a software pattern that has been used from so many years but still required powerful messaging servers. Redis comes out as an easy solution for pub/sub in software engineering world. In Redis pub/sub, messages are sent by senders (we called senders as publishers here) and received by subscribers. Messages are sent to a channel from where subscribers received the messages.
Understand Channels
In Redis, we can create any number of channels. A channel is a name used to differentiate messages published on the pub/. Any subscriber needs that data from the channel, can listen, and new publishers and subscribers are easily added when the system grows.
To find out active channels on Redis-server, we have command “ channels” for that, which gives an empty list or set, a system is not used pub/sub. Let's check out in the screenshot below.
There is no need to create or remove a channel, a channel exists only when the subscriber is receiving messages on it. There is no channel when the subscriber is not listening. Let's see this on the console, in the 1st console, we subscribe a channel “DemoChannel” through the command “subscribe DemoChannel”, as shown in below console:-
Console 1
while in another console run channels, as shown in below screenshots:-
Console 2
If you disconnect console 1, then there is no more channel.
Publish Messages
The publish command is used to send messages, let's check out below command and screenshot,
PUBLISH DemoChannel
Console 1
Console 2
In next blog, we will explore more use cases of pub/sub.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Navin Purohit
Navin has an experience of 4 years in Java and NodeJS. He is eager to learn new technologies and want to explore his inner strengths.