While testing some Groovy code that uses the Redis pub/sub functionality I wrote the script below. This is a simple example written in Groovy, it ships as a single file and loads the required dependencies at runtime via the Groovy Grape dependency management functionality.
Benefits of using Redis Pub Sub Functionality
Redis Pub Sub functionality can provide a number of benefits for applications that require real-time communication and data processing. Redis Pub Sub functionality allows for asynchronous messaging between different components of a system, enabling the creation of responsive and scalable applications and is a form of event driven programming.
Some benefits of using Redis Pub Sub include:
Scalability
Redis Pub Sub functionality enables the creation of distributed systems that can handle high volumes of data and messages — this can help improve system performance and scalability.
Real-time communication
Redis Pub Sub functionality allows for real-time messaging between different components of a system thus enabling real-time updates and communication between different parts of an application.
Decoupling
Redis Pub Sub functionality enables decoupling between different components of a system thereby allowing the components to operate independently while reducing dependencies between different parts of an application.
Flexibility
Redis Pub/Sub is a flexible messaging system that can be used for a wide range of use cases, including real-time updates, event-driven architectures, and distributed systems.
Reliability
Redis Pub Sub functionality is designed for high availability and reliability, with support for data replication, failover, and recovery.
In summary, Redis Pub/Sub can provide a range of benefits for applications that require real-time messaging and data processing. Redis Pub Sub enables scalability, real-time communication, decoupling, flexibility, and reliability in distributed systems.
How Does Redis Pub Sub Work?
Redis Pub/Sub works by allowing multiple clients to subscribe to one or more channels and receive messages published to those channels by other clients. The Pub/Sub functionality is built into Redis and uses a publish/subscribe model to enable real-time messaging between different parts of a system.
Here’s how Redis Pub/Sub works:
- A client subscribes to a channel using the SUBSCRIBE command.
- When another client publishes a message to that channel using the PUBLISH command, Redis delivers the message to all clients that are subscribed to the channel.
- Clients can unsubscribe from channels using the UNSUBSCRIBE command.
- Redis maintains a list of subscribed channels for each client and delivers messages in real-time to all subscribed clients.
Table of Contents
What is Event Driven Programming?
đ¤ Event Driven Programming (EDP) is a programming paradigm in which the flow of a program is determined by events, such as user actions, messages from other programs or the system, or other occurrences. In event-driven programming, the program is designed to respond to these events by triggering actions, rather than following a sequential execution path.
In event-driven programming, the program is designed around a set of event handlers that respond to specific events. When an event occurs, the corresponding event handler is called to process the event. The event handler can then trigger additional events or execute other program logic.
Event-driven programming is often used in graphical user interfaces (GUIs) where user actions, such as clicking on a button or selecting a menu item, trigger events that the program responds to. It is also used in networking, where messages received from other computers or devices trigger events that the program responds to.
Overall, Event Driven Programming allows for more responsive and interactive programs since the program can react quickly to user input or other events without needing to wait for a sequential program flow to complete. đ¤
Redis Pub/Sub is designed for high throughput and low latency, making it well-suited for real-time messaging and event-driven architectures.
What is Jedis PubSub?
Jedis PubSub is a feature of the Jedis Java library that provides an implementation of the publish/subscribe messaging paradigm for Redis, an open-source in-memory data structure store.
Pub/Sub allows messages to be broadcast to multiple clients that have subscribed to a particular channel. With Jedis Pub/Sub, clients can subscribe to one or more channels and receive notifications of messages published to those channels.
Jedis PubSub can be used for a variety of purposes, such as real-time messaging, event notification, and task queuing. It is commonly used in web applications, chat systems, and distributed systems that require message broadcasting.
In summary, Jedis Pub/Sub is a feature of the Jedis Java library that enables clients to subscribe to Redis channels and receive real-time notifications of messages published to those channels.
The following example consists of three steps, which we’ll cover next.
Redis Pub Sub Example Step One: Implement the redis.clients.jedis.JedisPubSub Interface
The first step requires that we implement the redis.clients.jedis.JedisPubSub interface. The JedisPubSub specification provides method definitions for subscribing and unsubscribing to channels, and for receiving messages — see lines #6 through #27 in the full example below.
The source code for JedisPubSub can also be found on GitHub.
Redis Pub Sub Example Step Two: Subscribe the JedisPubSub implementation to Jedis
In the second step we need to subscribe the implementation of JedisPubSub to receive messages from Jedis on the updates channel — how this is done can be seen on lines #29 and #34.
Redis Pub Sub Example Step Three: Publish An Example Message On The Updates Channel
In the last step, we send a message to the updates channel via the Redis command line interface (Redis CLI). If all of the components have been configured properly we should see the update appear in the Groovy Console output as demonstrated in the image below.
@GrabResolver(name='Maven Central', root='http://repo1.maven.org/')
@Grab(group='redis.clients', module='jedis', version='3.5.2')
import redis.clients.jedis.Jedis
import redis.clients.jedis.JedisPubSub
class DefaultPubSubImpl extends JedisPubSub {
@Override
public void onMessage(String channel, String message) {
println "channel: $channel, message: $message"
}
@Override
public void onPMessage(String pattern, String channel, String message) {}
@Override
public void onSubscribe(String channel, int subscribedChannels) {}
@Override
public void onUnsubscribe(String channel, int subscribedChannels) {}
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {}
}
def subscriber = new DefaultPubSubImpl ()
Jedis jedis = new Jedis("localhost", 6379, 0)
jedis.connect();
jedis.subscribe (subscriber, "updates")
Example Groovy Script execution demonstrating Redis Pub/Sub with Jedis
In the image below we can see the Groovy script running and printing to the console an update of “Hello world!” which was sent on the updates channel.
On the right-hand side of this image we can also see the “Hello world!” message being sent from the console.

Redis PubSub Example Article Conclusion
I hope this article has been helpful insofar as working with Redis PubSub as well as Jedis PubSub. If you have any question or have encountered a mistake, please leave a your thoughts in the comments.
I also write an article entitled Hidden Gems: Event-Driven Change Notifications in Relational Databases which covers a similar topic to this article and that is event driven programming with several relational databases.
Redis Pub Sub Example Article References
- Redis Pub Sub documentation
- Redis Pub/Sub definition — see also the section entitled “Pub/Sub best Practices in Redis”
- This article was written with assistance from ChatGPT.
- ThosPFuller
- April 11, 2021
- 11:17 pm
You must log in to post a comment.