Advertisement
Intermediate Redis, Pub/Sub

What is pattern based subscription using PSUBSCRIBE, and how does it differ from subscribing to specific individual channels?

Short answer

PSUBSCRIBE lets you subscribe to multiple channels at once that match a specified wildcard pattern, rather than needing to individually subscribe to each specific channel name, which is useful when you want to receive messages from a whole category of related channels without…

Save Redis mock interview
Advertisement
AModel answer
PSUBSCRIBE lets you subscribe to multiple channels at once that match a specified wildcard pattern, rather than needing to individually subscribe to each specific channel name, which is useful when you want to receive messages from a whole category of related channels without knowing their exact names in advance.
Example

PSUBSCRIBE news.* -- Receives messages published to news.sports, -- news.weather, news.technology, and any other matching channel

Real-world example
A news aggregation application subscribes to all channels matching the news wildcard pattern, automatically receiving updates from any new category channel that gets created later without needing code changes.
Likely follow-ups
  • What wildcard characters are supported in a pattern subscription?
  • Does pattern subscription have a performance cost compared to specific channel subscriptions?
Related topics