Databases 14 min read

Mastering Redis Pub/Sub: Channels, Patterns, and Real-World Use Cases

This article explains Redis Pub/Sub fundamentals, demonstrates channel‑based and pattern‑based subscription with command‑line examples, dives into the internal data structures that power them, and shows how to integrate Redis Pub/Sub in Spring Boot using Redisson for lightweight messaging.

Sanyou's Java Diary
Sanyou's Java Diary
Sanyou's Java Diary
Mastering Redis Pub/Sub: Channels, Patterns, and Real-World Use Cases

Redis Pub/Sub Overview

Redis Pub/Sub provides a message communication pattern where publishers use PUBLISH to send messages and subscribers use SUBSCRIBE or UNSUBSCRIBE to receive or stop receiving messages. It supports two modes: channel‑based and pattern‑based subscription.

Pub/Sub Hands‑On

After understanding the basics, you can run the commands to see how they work.

Channel‑Based Implementation

Three steps: subscribe to a channel, publish a message to that channel, and all subscribers receive the message.

<code>SUBSCRIBE develop
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "develop"
3) (integer) 1
</code>

After subscribing, the client can only use SUBSCRIBE , UNSUBSCRIBE , PSUBSCRIBE , and PUNSUBSCRIBE commands.

The server returns three types of replies: subscribe (subscription success), message (incoming message), and unsubscribe (successful unsubscription).

<code>PUBLISH develop 'do job'
(integer) 1
</code>

Messages are not persisted; only clients subscribed at the time receive them.

Pattern‑Based Implementation

Clients can subscribe to patterns such as smile.girl.* . When a message is published to a matching channel, all pattern subscribers also receive it.

<code>PSUBSCRIBE smile.girls.*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "smile.girls.*"
3) (integer) 1
</code>

If a client subscribes to both a channel and a matching pattern, it will receive duplicate messages (one as message , another as pmessage ).

Redisson and Spring Boot Example

<code>public long sendMessage(String message) {
    RTopic topic = redissonClient.getTopic(CHANNEL);
    long publish = topic.publish(message);
    log.info("Producer sent message = {}", message);
    return publish;
}
</code>
<code>public void onMessage() {
    RTopic topic = redissonClient.getTopic(CHANNEL);
    topic.addListener(String.class, (channel, msg) -> {
        log.info("channel: {} received message {}", channel, msg);
    });
}
</code>

Publish and listen must run in different JVMs; using the same redissonClient will not receive its own messages.

Underlying Mechanism

Redis stores channel subscriptions in redisServer.pubsub_channels , a dictionary where the key is the channel name and the value is a list of subscribed client connections.

<code>struct redisServer {
    ...
    dict *pubsub_channels;
    ...
}
</code>

When PUBLISH is called, the server looks up the channel in this dictionary and forwards the message to every client in the list.

Pattern subscriptions are kept in redisServer.pubsub_patterns , also a dictionary whose key is the pattern and whose value is a list of clients.

<code>struct redisServer {
    ...
    dict *pubsub_patterns;
    ...
}
</code>

During PSUBSCRIBE , the server adds the client to the appropriate list; during PUNSUBSCRIBE , it removes the client.

Summary

SUBSCRIBE channel [channel ...] – subscribe to one or more channels.

UNSUBSCRIBE channel – unsubscribe from a channel.

PUBLISH channel message – send a message to a channel.

PSUBSCRIBE pattern – subscribe to a pattern.

PUNSUBSCRIBE pattern – unsubscribe from a pattern.

Pub/Sub works independently of the selected Redis database; a message published in DB0 is received by subscribers connected to DB1.

Channel subscriptions are stored in redisServer.pubsub_channels ; pattern subscriptions are stored in redisServer.pubsub_patterns . When a message is published, the server delivers it to both channel subscribers and any pattern subscribers whose pattern matches the channel.

Typical use cases include sentinel communication, lightweight message queues, and service decoupling where persistence and acknowledgment are not required.

backendRedisMessage QueuedatabasesPub/Sub
Sanyou's Java Diary
Written by

Sanyou's Java Diary

Passionate about technology, though not great at solving problems; eager to share, never tire of learning!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.