diagram.mmd — sequence
Redis PubSub Flow sequence diagram

Redis PubSub is a lightweight in-memory publish-subscribe mechanism built into Redis that enables real-time message broadcasting to multiple subscribers with sub-millisecond latency but no message persistence.

Redis implements pub/sub via three commands: SUBSCRIBE channel, PUBLISH channel message, and UNSUBSCRIBE. Subscribers open a long-lived connection to Redis and issue a SUBSCRIBE command, putting the connection into a dedicated subscriber mode. In this mode the connection can only receive messages — it cannot issue normal Redis commands like GET or SET. Redis then acts as a pure relay: when a publisher calls PUBLISH, Redis immediately forwards the message to all active subscribers on that channel and returns the count of recipients.

The key characteristic of Redis pub/sub is its fire-and-forget nature. Redis does not persist messages. If a subscriber is offline when a message is published — even for a fraction of a second during a restart — it simply misses that message. There is no offset tracking, no replay, and no acknowledgment. This makes Redis pub/sub excellent for genuinely ephemeral data: live user presence indicators, real-time cursor positions in collaborative tools, chat messages where a brief gap is acceptable, or cache invalidation signals.

For use cases requiring durability, Redis Streams (XADD/XREADGROUP) provide consumer group semantics with message persistence and replay — combining the low-latency characteristics of Redis with the reliability guarantees more commonly associated with Kafka Producer Consumer Flow. Pattern subscriptions (PSUBSCRIBE) extend the basic model by matching channels using glob patterns (news.*), enabling a single subscriber to listen to families of channels.

Use Redis pub/sub for low-latency broadcast of transient data; use a durable broker for any message you cannot afford to lose.

Free online editor
Edit this diagram in Graphlet
Fork, modify, and export to SVG or PNG. No sign-up required.
Open in Graphlet →

Frequently asked questions

Redis PubSub is a lightweight in-memory publish-subscribe mechanism built into Redis that enables real-time message broadcasting to multiple subscribers with sub-millisecond latency. It has no message persistence — if a subscriber is offline when a message is published, that message is permanently lost.
A subscriber opens a persistent connection to Redis and issues a `SUBSCRIBE channel` command, entering a dedicated subscriber mode where it can only receive messages. A publisher calls `PUBLISH channel message` and Redis immediately forwards the message to all active subscribers on that channel, returning the recipient count. There are no offsets, no acknowledgments, and no replay.
Use Redis pub/sub for genuinely ephemeral, latency-sensitive broadcasts where message loss is acceptable: live presence indicators, real-time cursor positions in collaborative tools, chat messages where brief gaps are tolerable, and cache invalidation signals. If you need durability or replay, use Redis Streams or a durable broker like Kafka instead.
mermaid
sequenceDiagram participant Sub1 as Subscriber A participant Sub2 as Subscriber B participant Redis as Redis Server participant Pub as Publisher Sub1->>Redis: SUBSCRIBE news.sports Redis-->>Sub1: subscribe, news.sports, 1 Sub2->>Redis: SUBSCRIBE news.sports Redis-->>Sub2: subscribe, news.sports, 1 Sub2->>Redis: PSUBSCRIBE news.* Redis-->>Sub2: psubscribe, news.*, 2 Pub->>Redis: PUBLISH news.sports "Goal scored!" note">Note over Redis: Fan out to all channel subscribers Redis-->>Sub1: message, news.sports, "Goal scored!" Redis-->>Sub2: message, news.sports, "Goal scored!" Redis-->>Pub: (integer) 2 recipients Sub1->>Redis: UNSUBSCRIBE news.sports Redis-->>Sub1: unsubscribe, news.sports, 0 Pub->>Redis: PUBLISH news.sports "Final whistle" Redis-->>Sub2: message, news.sports, "Final whistle" note">Note over Sub1: Missed message — was unsubscribed
Copied to clipboard