Bird
Raised Fist0
Redisquery~20 mins

Pub/sub vs streams comparison in Redis - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Pub/Sub and Streams Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Difference in message delivery guarantees
Which statement correctly describes the message delivery guarantee difference between Redis Pub/Sub and Redis Streams?
APub/Sub guarantees message delivery to all subscribers even if they are offline; Streams do not.
BBoth Pub/Sub and Streams guarantee message delivery only if consumers are online.
CStreams guarantee message delivery to consumers even if they are offline; Pub/Sub does not.
DNeither Pub/Sub nor Streams guarantee message delivery under any circumstances.
Attempts:
2 left
💡 Hint
Think about what happens if a subscriber is offline when a message is published.
❓ query_result
intermediate
2:00remaining
Output of a Pub/Sub message reception
Given a Redis Pub/Sub subscriber listening on channel 'news', what will be the output if a publisher sends the message 'Hello' to 'news' while the subscriber is connected?
Redis
SUBSCRIBE news
PUBLISH news Hello
AThe subscriber receives and prints: 'Hello'
BThe subscriber receives and prints: 'message news Hello'
CThe subscriber receives no message because Pub/Sub does not deliver messages
DThe subscriber receives an error message
Attempts:
2 left
💡 Hint
Consider the format Redis uses to notify subscribers.
📝 Syntax
advanced
2:00remaining
Correct command to add an entry to a Redis Stream
Which Redis command correctly adds a new entry with field 'user' and value 'alice' to a stream named 'mystream'?
AAPPEND mystream user alice
BADD mystream user alice
CXADD mystream user alice *
DXADD mystream * user alice
Attempts:
2 left
💡 Hint
The XADD command uses '*' to auto-generate the entry ID.
❓ optimization
advanced
2:00remaining
Choosing between Pub/Sub and Streams for offline consumers
You want to build a chat application where users can receive messages sent while they were offline. Which Redis feature should you choose for message delivery and why?
AUse Streams because it allows consumers to read messages they missed while offline.
BUse Pub/Sub because it stores messages for offline users automatically.
CUse Pub/Sub because it supports message persistence by default.
DUse neither because Redis does not support offline message delivery.
Attempts:
2 left
💡 Hint
Think about which feature stores messages for later reading.
🔧 Debug
expert
2:00remaining
Why does a Redis Pub/Sub subscriber miss messages?
A Redis Pub/Sub subscriber sometimes misses messages published to a channel. What is the most likely reason?
AThe subscriber was disconnected or offline when the messages were published.
BRedis Pub/Sub queues messages for offline subscribers but the queue is full.
CThe subscriber did not acknowledge the messages, so Redis dropped them.
DRedis Pub/Sub requires explicit message commits to deliver messages.
Attempts:
2 left
💡 Hint
Consider how Pub/Sub handles offline subscribers.

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

  1. Step 1: Understand Pub/Sub purpose

    Pub/Sub delivers messages instantly to subscribers without saving them.
  2. Step 2: Compare with Streams

    Streams store messages for later processing, unlike Pub/Sub.
  3. Final Answer:

    Pub/Sub -> Option C
  4. 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

  1. Step 1: Identify publish command syntax

    The PUBLISH command sends a message to a channel immediately.
  2. Step 2: Check other commands

    XADD adds to streams, SUBSCRIBE listens, GET retrieves keys, so only PUBLISH fits.
  3. Final Answer:

    PUBLISH news "Hello" -> Option A
  4. 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

  1. Step 1: Understand XADD and XREAD

    XADD adds entries to a stream with unique IDs. XREAD reads entries from streams.
  2. Step 2: Analyze XREAD COUNT 2 STREAMS mystream 0

    This reads up to 2 entries from 'mystream' starting from ID 0 (the beginning).
  3. Final Answer:

    Two entries with their IDs and fields returned -> Option B
  4. 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

  1. Step 1: Verify SUBSCRIBE usage

    SUBSCRIBE must be called on the client to listen to messages; if not, no messages arrive.
  2. Step 2: Check other options

    PUBLISH syntax is correct; Streams and XREAD are unrelated to Pub/Sub listening.
  3. Final Answer:

    The client is not listening because it did not call SUBSCRIBE correctly -> Option D
  4. 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

  1. Step 1: Identify requirements for task queue

    Tasks must be stored reliably and processed even after worker restarts.
  2. Step 2: Compare Redis features

    Pub/Sub does not store messages, so tasks can be lost. Streams store messages and allow replay.
  3. Final Answer:

    Use Streams because they store messages and support replaying tasks -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard