Understanding Redis Pub/Sub Limitations: No Persistence
📖 Scenario: You are building a simple chat application using Redis Pub/Sub. You want to understand how messages are delivered and what happens if a user is offline.
🎯 Goal: Create a Redis Pub/Sub setup that demonstrates message publishing and subscribing, and observe the limitation that messages are not stored if the subscriber is offline.
📋 What You'll Learn
Create a Redis channel named chatroom
Subscribe to the chatroom channel
Publish messages to the chatroom channel
Demonstrate that messages sent while subscriber is offline are not received
💡 Why This Matters
🌍 Real World
Many chat apps and real-time systems use Redis Pub/Sub for instant messaging but must handle the limitation that messages are not saved if users are offline.
💼 Career
Understanding Redis Pub/Sub limitations is important for backend developers and system architects designing real-time communication systems.
Progress0 / 4 steps
1
Create a Redis channel named chatroom
Write the Redis command to create a channel named chatroom by subscribing to it using the command SUBSCRIBE chatroom.
Redis
Hint
Use the SUBSCRIBE command followed by the channel name chatroom.
2
Publish a message to the chatroom channel
Write the Redis command to publish the message Hello everyone! to the chatroom channel using PUBLISH chatroom "Hello everyone!".
Redis
Hint
Use the PUBLISH command with the channel name and the message in quotes.
3
Simulate subscriber offline and publish a message
Write the Redis command to publish the message Are you there? to the chatroom channel while no subscriber is connected, using PUBLISH chatroom "Are you there?".
Redis
Hint
Use the PUBLISH command again with the new message.
4
Explain the limitation of Redis Pub/Sub with no persistence
Add a comment explaining that Redis Pub/Sub does not store messages, so if a subscriber is offline, it will miss messages published during that time.
Redis
Hint
Write a comment starting with # explaining the no persistence limitation.
Practice
(1/5)
1. What happens to messages sent via Redis Pub/sub if no clients are subscribed at that moment?
easy
A. Messages are lost and not stored anywhere.
B. Messages are saved and delivered later when clients subscribe.
C. Messages are queued until a client connects.
D. Messages are stored in a Redis list automatically.
Solution
Step 1: Understand Redis Pub/sub behavior
Redis Pub/sub delivers messages only to clients currently subscribed.
Step 2: Analyze message persistence
Since Pub/sub does not save messages, if no client listens, messages are lost.
Final Answer:
Messages are lost and not stored anywhere. -> Option A
Quick Check:
Pub/sub = no persistence [OK]
Hint: Pub/sub delivers live only, no message storage [OK]
Common Mistakes:
Assuming messages queue up for later delivery
Thinking messages are saved automatically
Confusing Pub/sub with Redis streams
2. Which of the following is the correct Redis command to subscribe to a channel named news?
easy
A. SUBSCRIBE TO news
B. PUBLISH news
C. SUBSCRIBE news
D. LISTEN news
Solution
Step 1: Recall Redis Pub/sub subscribe syntax
The correct command to subscribe is SUBSCRIBE <channel>.
Step 2: Match command to channel name
Using SUBSCRIBE news subscribes to the channel named 'news'.