PUBLISH to channels in Redis - Time & Space Complexity
When sending messages to channels in Redis, it's important to understand how the time taken grows as more clients listen.
We want to know how the cost changes when publishing to many subscribers.
Analyze the time complexity of the following Redis PUBLISH command.
PUBLISH channel_name "Hello subscribers!"
This command sends a message to all clients subscribed to the given channel.
Look for repeated actions inside the publish process.
- Primary operation: Sending the message to each subscribed client.
- How many times: Once for each subscriber connected to the channel.
The time to publish grows as the number of subscribers grows.
| Input Size (subscribers) | Approx. Operations |
|---|---|
| 10 | 10 message sends |
| 100 | 100 message sends |
| 1000 | 1000 message sends |
Pattern observation: The work increases directly with the number of subscribers.
Time Complexity: O(n)
This means the time to publish 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 receive the message, so more subscribers mean more work and longer time.
Understanding how message delivery scales helps you explain system behavior and design efficient real-time features.
What if Redis used batching to send messages to subscribers? How would that affect the time complexity?
Practice
What does the PUBLISH command do in Redis?
Solution
Step 1: Understand the purpose of PUBLISH
The PUBLISH command sends messages to clients currently subscribed to a channel.Step 2: Differentiate from storage commands
PUBLISH does not store messages permanently; it only broadcasts to active subscribers.Final Answer:
Sends a message to all clients subscribed to a specific channel -> Option BQuick Check:
PUBLISH broadcasts messages [OK]
- Thinking PUBLISH stores messages permanently
- Confusing PUBLISH with channel creation
- Assuming PUBLISH deletes messages
Which of the following is the correct syntax to publish a message 'Hello World' to a channel named news in Redis?
?Solution
Step 1: Recall Redis PUBLISH syntax
The syntax is: PUBLISH channel message. Quote the message if it contains spaces.Step 2: Identify correct option
PUBLISH news 'Hello World' matches the syntax exactly.Final Answer:
PUBLISH news 'Hello World' -> Option AQuick Check:
Correct Redis PUBLISH syntax = PUBLISH news 'Hello World' [OK]
- Not quoting messages with spaces
- Swapping channel and message order
- Using commas or extra symbols
Given the following Redis commands executed in order:
SUBSCRIBE sports
PUBLISH sports "Game tonight"
PUBLISH news "Weather update"What messages will the subscriber receive?
Solution
Step 1: Identify subscribed channel
The subscriber is subscribed to the 'sports' channel only.Step 2: Check published messages
Messages published to 'sports' are received; messages to 'news' are ignored by this subscriber.Final Answer:
Only "Game tonight" -> Option AQuick Check:
Subscribers get messages only from their channels [OK]
- Assuming subscriber gets all published messages
- Confusing channel names
- Ignoring subscription context
What is wrong with this Redis command to publish a message?
PUBLISHChoose the best explanation.
Solution
Step 1: Check required arguments for PUBLISH
PUBLISH requires two arguments: channel and message.Step 2: Identify missing parts in the command
The command only has PUBLISH with no arguments, so it is incomplete.Final Answer:
Missing channel and message arguments -> Option DQuick Check:
PUBLISH needs channel and message [OK]
- Thinking PUBLISH works without arguments
- Assuming message length causes error here
- Believing SUBSCRIBE is required before PUBLISH
You want to send a notification to multiple channels news and alerts at once. Which Redis approach is best?
Solution
Step 1: Understand Redis PUBLISH limitations
Redis PUBLISH sends messages to one channel at a time; no built-in multi-channel publish exists.Step 2: Choose correct method to notify multiple channels
Send separate PUBLISH commands for each channel to reach all subscribers.Final Answer:
Use multiple PUBLISH commands, one per channel -> Option CQuick Check:
Multiple channels need multiple PUBLISH calls [OK]
- Expecting one PUBLISH to reach multiple channels
- Combining channel names incorrectly
- Assuming Redis supports multi-channel publish natively
Ready to prove it?
Pass the test and this pattern moves unknown
