Redis pub sub feature is used to send messages to multiple subscribers without any delay. This falls under the publish and subscribe message paradigm on which multiple technologies are built like

  • Kafka
  • RabbitMQ
  • MQTT

In Redis Pub Sub we have three important parts

  • Publisher
  • Channel
  • Subscriber

Publisher

  • The publisher has the responsibility to publish the message to a channel.

Subscriber

  • The subscriber will subscribe to one or more channels and then it will receive the message if any message is published to the channel

Channel

  • Channel is the bridge between publisher and subscriber

Command available in Redis Pub-Sub

SUBSCRIBE channel [channel …]

  • This is to subscribe to a particular client.
  • You can subscribe to one channel or multiple channels in a single command
  • If you want to subscribe to multiple channels then you need to provide the space-separated channel names

UNSUBSCRIBE [channel [channel …]]

  • This is used to unsubscribe the client from the given list of channel
  • If no channel name is given then it unsubscribes from all the connected channel

PSUBSCRIBE pattern [pattern …]

  • Subscribes to the client where the client name is given as a pattern
redis-cli> PSUBSCRIBE h?llo

This will subscribe to channel names where the second character can be anything. Examples are below

  • halo
  • helo
  • htlo
redis-cli> PSUBSCRIBE h*llo

This command will subscribe to helo, heeello, herdllo.

PUNSUBSCRIBE pattern [pattern …]

  • This is used to unsubscribe all the clients where the client name matches the patterns
  • You can provide multiple space-separated patterns
  • If no pattern is given then it will unsubscribe all the connected clients.

Other Advanced commands are

  • SSUBSCRIBE
  • SUNSUBSCRIBE
  • QUIT
  • RESET

References

Next: Install Redis Using Docker compose

Leave a Reply

Your email address will not be published. Required fields are marked *