Broadcast
Redis Pub/Sub pushes a message to the subscriptions connected when it is published, and keeps nothing for anyone else.
The diagram as text
- Publisher PUBLISH news → Channel news keeps nothing: publish
- Channel news keeps nothing → Subscription A connected
- Channel news keeps nothing → Subscription B connected
- Subscription A connected → Client buffer 32 by default
- Client buffer 32 by default → Handler
- Subscribes later: never sees it
- A handler that falls behind its buffer loses the oldest messages
A channel is a name, not a key: Redis stores nothing under it. PUBLISH pushes the message to
every client subscribed to the channel at that moment. Redis calls this at-most-once delivery: a
client that is not subscribed, or loses its connection, never gets the message
(Pub/Sub).
Each RedisPubSub subscription opens a client of its
own and subscribes it. At startup, the subscribe step of a classic subscription finishes once a
PING sent after SUBSCRIBE on the same connection has been answered, so a message published after
the subscription opened reaches it.
RedisPubSubPattern subscribes to a glob with
PSUBSCRIBE and receives every channel that matches it.
A message waits in the client's buffer until the handler takes it. The buffer holds 32 messages,
fred's default, unless buffer sets
another size. A subscription that falls behind loses the oldest messages and logs a warning with
the channel and the count.
Classic Pub/Sub sends every message to every node of a cluster. Sharded Pub/Sub, SSUBSCRIBE and
SPUBLISH since Redis 7.0, assigns a channel to a hash slot and keeps its messages inside that
shard. PubSubMode picks one, and a subscription in
one mode does not receive what is published in the other.
A Pub/Sub delivery has nothing to settle: ack and nack answer AckError::Unsupported.