0
0
Redisquery~5 mins

Pub/sub limitations (no persistence) in Redis

Choose your learning style9 modes available
Introduction
Pub/sub in Redis lets programs send messages instantly, but it does not save messages for later. This means if a program is not listening when a message is sent, it will miss that message.
You want to send real-time chat messages between users.
You need to notify multiple services immediately about an event.
You want to update live dashboards with fresh data.
You have temporary alerts that don't need to be saved.
You want to broadcast messages to many clients at once.
Syntax
Redis
SUBSCRIBE channel_name
PUBLISH channel_name message
SUBSCRIBE listens to messages on a channel.
PUBLISH sends a message to all subscribers of that channel.
Examples
Start listening to messages sent to the 'news' channel.
Redis
SUBSCRIBE news
Send the message 'Hello subscribers!' to everyone listening on 'news'.
Redis
PUBLISH news "Hello subscribers!"
Subscribe to 'sports' channel and publish a message to it.
Redis
SUBSCRIBE sports
PUBLISH sports "Game starts now!"
Sample Program
One client listens to 'updates' channel. Another client sends a message. The listening client receives it only if it is connected at that moment.
Redis
# In one Redis client:
SUBSCRIBE updates

# In another Redis client:
PUBLISH updates "New update available!"
OutputSuccess
Important Notes
If no client is subscribed when a message is published, that message is lost forever.
Pub/sub does not store messages; it only delivers them live.
For message persistence, consider Redis Streams or other message queues.
Summary
Redis Pub/sub sends messages instantly but does not save them.
Subscribers must be connected to receive messages.
Use Pub/sub for real-time notifications without needing history.