Pub/sub limitations (no persistence) in Redis - Time & Space Complexity
When using Redis Pub/Sub, messages are sent to subscribers instantly without saving them.
We want to understand how the time to deliver messages grows as more subscribers join.
Analyze the time complexity of this Redis Pub/Sub publish operation.
# Subscribe clients to a channel
SUBSCRIBE channel1
# Publish a message to all subscribers
PUBLISH channel1 "Hello subscribers!"
This code sends a message to all clients subscribed to channel1 immediately without storing it.
Look for repeated actions in the publish process.
- Primary operation: Sending the message to each subscriber.
- How many times: Once for each subscriber connected to the channel.
As the number of subscribers grows, the work to send messages grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 subscribers | 10 sends |
| 100 subscribers | 100 sends |
| 1000 subscribers | 1000 sends |
Pattern observation: The number of sends grows directly with the number of subscribers.
Time Complexity: O(n)
This means the time to deliver a message grows linearly with the number of subscribers.
[X] Wrong: "Publishing a message takes the same time no matter how many subscribers there are."
[OK] Correct: Each subscriber must get the message separately, so more subscribers mean more work.
Understanding how message delivery time grows helps you design systems that handle many users efficiently.
"What if Redis stored messages for offline subscribers? How would that change the time complexity of publishing?"
Practice
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 AQuick Check:
Pub/sub = no persistence [OK]
- Assuming messages queue up for later delivery
- Thinking messages are saved automatically
- Confusing Pub/sub with Redis streams
news?Solution
Step 1: Recall Redis Pub/sub subscribe syntax
The correct command to subscribe isSUBSCRIBE <channel>.Step 2: Match command to channel name
UsingSUBSCRIBE newssubscribes to the channel named 'news'.Final Answer:
SUBSCRIBE news -> Option CQuick Check:
Subscribe syntax = SUBSCRIBE channel [OK]
- Using PUBLISH instead of SUBSCRIBE
- Adding extra words like TO
- Using LISTEN which is not a Redis command
Client A subscribes to channel 'chat'. Client B publishes message 'Hello' to 'chat'. Client C subscribes to 'chat' after the message is published.
Which clients receive the message 'Hello'?
Solution
Step 1: Identify subscribers at publish time
Only Client A was subscribed when Client B published the message.Step 2: Understand Pub/sub delivery
Messages are sent only to clients subscribed at publish time; Client C subscribed later and misses the message.Final Answer:
Only Client A -> Option AQuick Check:
Subscribers at publish = receivers [OK]
- Assuming late subscribers get past messages
- Including publisher as receiver
- Thinking all clients get all messages
SUBSCRIBE news PUBLISH news "Update 1"
But your subscriber never receives the message. What is the likely problem?
Solution
Step 1: Check Pub/sub message delivery timing
Subscribers must be listening before messages are published to receive them.Step 2: Analyze the code order
If PUBLISH runs before SUBSCRIBE completes, subscriber misses the message.Final Answer:
The subscriber must be subscribed before the message is published. -> Option BQuick Check:
Subscribe first, then publish [OK]
- Thinking PUBLISH syntax is wrong
- Believing Redis lacks Pub/sub support
- Assuming channel names are restricted
Solution
Step 1: Understand Pub/sub limitation
Pub/sub does not store messages; they are lost if no subscriber is present.Step 2: Identify Redis feature for persistence
Redis Streams allow storing messages so clients can read them later.Step 3: Evaluate other options
Using multiple channels or transactions does not provide message persistence; increasing memory does not save messages automatically.Final Answer:
Redis Streams to store and consume messages later. -> Option DQuick Check:
Use Streams for persistence, not Pub/sub [OK]
- Trying to fix Pub/sub by adding channels
- Thinking transactions save Pub/sub messages
- Assuming more memory stores messages
Ready to prove it?
Pass the test and this pattern moves unknown
