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?