Skip to main content

Broadcast

Redis Pub/Sub pushes a message to the subscriptions connected when it is published, and keeps nothing for anyone else.

Pub/Sub: a publish reaches the subscriptions connected at that moment, each through its client's bufferPublisherPUBLISH newsChannel newskeeps nothingSubscription AconnectedSubscription BconnectedSubscribeslater:never sees itClient buffer32 by defaultHandlerA handler that falls behind its bufferloses the oldest messages
The diagram as text
  1. Publisher PUBLISH news → Channel news keeps nothing: publish
  2. Channel news keeps nothing → Subscription A connected
  3. Channel news keeps nothing → Subscription B connected
  4. Subscription A connected → Client buffer 32 by default
  5. Client buffer 32 by default → Handler
  6. Subscribes later: never sees it
  7. 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.