Pub/sub vs streams comparison in Redis - Performance Comparison
When using Redis for messaging, it is important to understand how the time to process messages grows as more messages or subscribers are involved.
We want to see how the cost changes when using Pub/Sub versus Streams as the number of messages or consumers increases.
Analyze the time complexity of these Redis commands for message delivery.
# Pub/Sub publish
PUBLISH channel message
# Streams add and read
XADD mystream * message value
XREAD COUNT 10 STREAMS mystream last_id
Pub/Sub sends messages directly to all subscribers. Streams store messages and let consumers read at their own pace.
Look at what repeats when messages are sent or read.
- Primary operation: For Pub/Sub, sending message to each subscriber; for Streams, appending message and reading batches.
- How many times: Pub/Sub sends once per subscriber; Streams append once and consumers read independently.
Consider how work grows as subscribers or messages increase.
| Input Size (n) | Pub/Sub Operations | Streams Operations |
|---|---|---|
| 10 subscribers | 10 sends per message | 1 append + 10 independent reads |
| 100 subscribers | 100 sends per message | 1 append + 100 independent reads |
| 1000 subscribers | 1000 sends per message | 1 append + 1000 independent reads |
Pub/Sub cost grows directly with number of subscribers per message. Streams cost grows with number of consumers reading messages separately.
Time Complexity: O(s) where s is number of subscribers or consumers.
This means the work to deliver messages grows linearly with how many clients receive or read them.
[X] Wrong: "Pub/Sub and Streams have the same cost regardless of subscribers."
[OK] Correct: Pub/Sub sends messages to every subscriber immediately, so cost grows with subscribers. Streams store once but each consumer reads separately, so cost depends on consumers reading.
Understanding how message delivery scales helps you choose the right Redis feature and explain your reasoning clearly in conversations.
What if we batch multiple messages in one XADD call for Streams? How would that affect the time complexity?
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
