What if you could never lose a message again while still chatting live with friends?
Pub/sub vs streams comparison in Redis - When to Use Which
Imagine you have a group chat where everyone talks at once, and you try to remember every message manually on paper.
Writing down every message by hand is slow, easy to miss, and you can't go back to see what was said before without flipping through messy notes.
Pub/sub lets messages flow instantly to everyone listening, while streams keep a neat, ordered history you can review anytime without losing anything.
Listen to messages and write them down manually for each user.
Use Pub/sub to broadcast messages live; use Streams to store and read message history.You can build real-time apps that deliver instant updates and keep a reliable record of all events effortlessly.
A chat app where users see messages live (Pub/sub) and can scroll back to read old messages anytime (Streams).
Pub/sub is great for instant, live message delivery.
Streams store messages so you can read them later or replay them.
Choosing between them depends on whether you need history or just live updates.
Practice
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 CQuick Check:
Instant delivery without storage = Pub/Sub [OK]
- Confusing Streams with Pub/Sub for instant delivery
- Thinking Streams do not store messages
- Choosing data structures unrelated to messaging
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 AQuick Check:
Publish message = PUBLISH command [OK]
- Using XADD instead of PUBLISH for Pub/Sub
- Confusing SUBSCRIBE with PUBLISH
- Using GET which is for keys, not messages
XREAD COUNT 2 STREAMS mystream 0 after adding two entries with XADD mystream * field1 val1 and XADD mystream * field2 val2?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 BQuick Check:
XREAD with COUNT 2 returns up to 2 entries [OK]
- Thinking COUNT is invalid in XREAD
- Assuming 0 means no messages returned
- Expecting only one entry despite COUNT 2
SUBSCRIBE mychannel. But your client never receives messages published with PUBLISH mychannel "Hello". What is the likely problem?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 DQuick Check:
Client must SUBSCRIBE to receive messages [OK]
- Thinking PUBLISH syntax is wrong
- Confusing Streams with Pub/Sub for message delivery
- Using XREAD to get Pub/Sub messages
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 AQuick Check:
Reliable storage and replay = Streams [OK]
- Choosing Pub/Sub for reliable storage
- Confusing Sorted Sets with message storage
- Using Hashes which are not for message queues
Ready to prove it?
Pass the test and this pattern moves unknown
