A Guide to Redis Pub/Sub

A Guide to Redis Pub/Sub

Jul 26, 2023 by Robert Gravelle

More than a mere database, Redis can also act as a message broker that supports both publishing and subscribing (pub/sub) operations. This blog will provide a brief overview of Redis’s Pub/Sub capabilities using Navicat for Redis.

Pub/Sub is a pattern whereby a sender (broadcaster) cannot send messages to specific receivers (subscribers). Instead, published messages are sent over channels, without any knowledge of how many (if any) subscribers are tuning in. Subscribers then sign up for one or more channels so that they only receive messages that are of interest to them. Decoupling publishers and subscribers in this way allows for greater scalability and makes it easier to manage the flow of information in a complex system.

Redis Pub/Sub provides a lightweight, fast, and scalable messaging solution that can be used for various purposes, such as real-time notifications, sending messages between microservices, or communicating between different parts of a single application.

Redis employs an at-most-once message delivery system. As the name suggests, it means that a message will be delivered only once, if at all. As such, once the message is sent by the Redis server, it’s never sent again. If the subscriber is unable to receive the message (for example, due to an error or a network outage) the message is forever lost. Much like catching your favorite show on the radio, if you happen to miss it, you’re out of luck. If your application requires stronger delivery guarantees, you should use Redis Streams instead.

Moreover, Pub/Sub has no relation to the key space. This means that a message published on database 10 will be heard by a subscriber on database 1. If you need scoping, Redis suggests prefixing the channel name (i.e., prod_mychannel, test_mychannel).

In Navicat for Redis (or Navicat Premium), we can access the Pub/Sub screen via the Pub/Sub button on the main toolbar. From there, we can publish messages using the Publish Pane:

pub-sub_screen (106K)

In Redis, channels are not explicitly created by the user. The channels are created automatically when either the first message is published or a client subscribes to them. To demonstrate, we’ll open two connections with the same Redis server. Each connection will act as a different client. The first connection will subscribe to the “test_channel”, while the second one will publish a message to the same channel. By doing so, we would expect our message to be delivered to the subscriber as soon as it’s published.

To subscribe to a channel in Navicat, we simply need to click the Subscribe button. That will open the Subscribe Dialog:

subscribe_dialog (117K)

There, we would enter the channel name – “test_channel” – and then click Subscribe. After the dialog closes, the channel will appear in the Channels list, along with a record of the subscribe action:

channel_added (110K)

To publish a message in Navicat, we would select it in the Channels list (it is the default since we only have one channel at this point), enter out message in the Message text field, and click on Publish. At that point, we should see a notification that the message was received:

message_received (75K)

In this blog, we explored Redis’s Pub/Sub capabilities using Navicat for Redis.

Interested in giving Navicat for Redis a try. Download it here. The trial version is fully functional for 14 days.