📖 Scenario: You are building a simple notification system using Redis. You want to learn how to use Redis Pub/Sub and Streams to send and receive messages.
🎯 Goal: Build two small Redis data structures: one using Pub/Sub channels and one using Streams. Learn how to publish messages and read them using both methods.
📋 What You'll Learn
Create a Pub/Sub channel named notifications
Publish a message 'Hello Pub/Sub!' to the notifications channel
Create a Stream named mystream
Add a message with field message and value 'Hello Streams!' to mystream
💡 Why This Matters
🌍 Real World
Redis Pub/Sub is great for real-time notifications like chat messages or live updates. Streams are useful for event sourcing, logging, and reliable message processing.
💼 Career
Understanding Redis Pub/Sub and Streams is valuable for backend developers working on scalable, real-time applications and data pipelines.
Progress0 / 4 steps
1
Create a Pub/Sub channel and publish a message
Use the Redis command PUBLISH to send the message 'Hello Pub/Sub!' to the channel named notifications.
Redis
Hint
Use the PUBLISH command followed by the channel name and the message in single quotes.
2
Create a Stream and add a message
Use the Redis command XADD to add a message to the Stream named mystream. The message should have the field message with the value 'Hello Streams!'. Use * for the ID to let Redis generate it.
Redis
Hint
The XADD command adds entries to a stream. Use * to auto-generate the ID.
3
Subscribe to the Pub/Sub channel
Use the Redis command SUBSCRIBE to listen to messages on the channel named notifications.
Redis
Hint
The SUBSCRIBE command listens for messages on a channel.
4
Read messages from the Stream
Use the Redis command XREAD to read new messages from the Stream named mystream. Use STREAMS mystream 0 to read all messages from the beginning.
Redis
Hint
The XREAD command reads messages from streams. Use 0 to read from the start.
Practice
(1/5)
1. Which Redis feature is best suited for instant message delivery without storing messages?
easy
A. Hashes
B. Streams
C. Pub/Sub
D. Sorted Sets
Solution
Step 1: Understand Pub/Sub purpose
Pub/Sub delivers messages instantly to subscribers without saving them.
Step 2: Compare with Streams
Streams store messages for later processing, unlike Pub/Sub.
Final Answer:
Pub/Sub -> Option C
Quick Check:
Instant delivery without storage = Pub/Sub [OK]
Hint: Instant delivery without storage means Pub/Sub [OK]
Common Mistakes:
Confusing Streams with Pub/Sub for instant delivery
Thinking Streams do not store messages
Choosing data structures unrelated to messaging
2. Which of the following is the correct Redis command to publish a message to a channel named news?
easy
A. PUBLISH news "Hello"
B. XADD news * message "Hello"
C. SUBSCRIBE news
D. GET news
Solution
Step 1: Identify publish command syntax
The PUBLISH command sends a message to a channel immediately.
Step 2: Check other commands
XADD adds to streams, SUBSCRIBE listens, GET retrieves keys, so only PUBLISH fits.
Final Answer:
PUBLISH news "Hello" -> Option A
Quick Check:
Publish message = PUBLISH command [OK]
Hint: Use PUBLISH to send messages to channels [OK]
Common Mistakes:
Using XADD instead of PUBLISH for Pub/Sub
Confusing SUBSCRIBE with PUBLISH
Using GET which is for keys, not messages
3. Given the following Redis commands, what will be the output of XREAD COUNT 2 STREAMS mystream 0 after adding two entries with XADD mystream * field1 val1 and XADD mystream * field2 val2?
medium
A. An error because COUNT is invalid here
B. Two entries with their IDs and fields returned
C. No entries returned because 0 means no messages
D. Only the first entry returned ignoring COUNT
Solution
Step 1: Understand XADD and XREAD
XADD adds entries to a stream with unique IDs. XREAD reads entries from streams.
Step 2: Analyze XREAD COUNT 2 STREAMS mystream 0
This reads up to 2 entries from 'mystream' starting from ID 0 (the beginning).
Final Answer:
Two entries with their IDs and fields returned -> Option B
Quick Check:
XREAD with COUNT 2 returns up to 2 entries [OK]
Hint: XREAD with COUNT returns that many entries from stream start [OK]
Common Mistakes:
Thinking COUNT is invalid in XREAD
Assuming 0 means no messages returned
Expecting only one entry despite COUNT 2
4. You wrote this Redis command to subscribe to a channel: SUBSCRIBE mychannel. But your client never receives messages published with PUBLISH mychannel "Hello". What is the likely problem?
medium
A. The client must use XREAD to receive Pub/Sub messages
B. PUBLISH command syntax is wrong
C. Streams must be used instead of Pub/Sub for messages
D. The client is not listening because it did not call SUBSCRIBE correctly
Solution
Step 1: Verify SUBSCRIBE usage
SUBSCRIBE must be called on the client to listen to messages; if not, no messages arrive.
Step 2: Check other options
PUBLISH syntax is correct; Streams and XREAD are unrelated to Pub/Sub listening.
Final Answer:
The client is not listening because it did not call SUBSCRIBE correctly -> Option D
Quick Check:
Client must SUBSCRIBE to receive messages [OK]
Hint: Client must SUBSCRIBE to receive published messages [OK]
Common Mistakes:
Thinking PUBLISH syntax is wrong
Confusing Streams with Pub/Sub for message delivery
Using XREAD to get Pub/Sub messages
5. You want to build a task queue in Redis where tasks must be stored reliably and processed even if the worker restarts. Which Redis feature should you use and why?
hard
A. Use Streams because they store messages and support replaying tasks
B. Use Sorted Sets because they keep tasks ordered by score
C. Use Pub/Sub because it delivers messages instantly without storage
D. Use Hashes because they store key-value pairs efficiently
Solution
Step 1: Identify requirements for task queue
Tasks must be stored reliably and processed even after worker restarts.
Step 2: Compare Redis features
Pub/Sub does not store messages, so tasks can be lost. Streams store messages and allow replay.
Final Answer:
Use Streams because they store messages and support replaying tasks -> Option A
Quick Check:
Reliable storage and replay = Streams [OK]
Hint: Reliable task queues need Streams, not Pub/Sub [OK]
Common Mistakes:
Choosing Pub/Sub for reliable storage
Confusing Sorted Sets with message storage
Using Hashes which are not for message queues
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.