This instructional demonstrates how to implement publish / subscribe (pub sub) functionality using Redis and Jedis (a Java client for the Redis real-time data platform).
Jedis PubSub Tutorial TOC
This Jedis PubSub Groovy script example is written in the Groovy programming language (Groovy) and ships as a single script and loads the required dependencies at runtime via the Groovy Grape dependency management system.
This article was updated on March 13, 2025.
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 is a high-performance, low-latency messaging system that is simple to use, but it has limitations in terms of reliability.
Redis Publish Subscribe does not store messages, meaning that if a subscriber is not connected at the time a message is published, the message is lost — there is also no delivery guarantee or acknowledgment of message receipt or processing.
The absence of message storage makes Redis Pub/Sub suitable for scenarios where immediate consumption of messages is important and the system can tolerate an occasional loss of messages.
Examples of applications that may be in this category include real-time analytics or live update solutions.
For applications requiring message persistence, guaranteed delivery, and/or acknowledgment mechanisms, alternative solutions will likely be necessary (see Redis Streams and Redis PubSub vs Redis Streams).
In summary, Redis Pub/Sub can provide a range of benefits for applications that require real-time messaging and data processing including scalability, real-time communication, decoupling, flexibility, and reliability in distributed systems (not message delivery, however).
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.
Below I’ve included a simple example diagram where a publisher publishes a message on a channel to the Redis Server which then propagates this message to the Jedis client via that same channel.
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.
Jedis 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.
Jedis PubSub 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.
Step One: Extend from the redis.clients.jedis.JedisPubSub class
The first step requires that we extend the redis.clients.jedis.JedisPubSub class.
The JedisPubSub class 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.
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.
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.
Conclusion
I hope this tutorial has been helpful insofar as working with Redis and Jedis PubSub.
If you have any questions or have encountered a mistake, please leave your thoughts in the comments.
Article References
- Redis Pub Sub documentation
- Redis Pub/Sub definition — see also the section entitled “Pub/Sub best Practices in Redis”











