SUBSCRIBE to channels in Redis - Time & Space Complexity
When using Redis SUBSCRIBE, we want to understand how the time to receive messages changes as more messages arrive.
We ask: How does the work grow when more messages are published to subscribed channels?
Analyze the time complexity of subscribing and receiving messages from channels.
SUBSCRIBE channel1 channel2
# Server sends messages to subscribed clients
PUBLISH channel1 "Hello"
PUBLISH channel2 "World"
# Client receives messages as they arrive
This code subscribes a client to two channels and shows messages being published and received.
Look for repeated actions that affect time.
- Primary operation: Receiving each published message on subscribed channels.
- How many times: Once per message published to any subscribed channel.
As more messages are published, the client receives more messages one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 messages | 10 receive operations |
| 100 messages | 100 receive operations |
| 1000 messages | 1000 receive operations |
Pattern observation: The work grows directly with the number of messages received.
Time Complexity: O(n)
This means the time to process messages grows linearly with the number of messages received.
[X] Wrong: "Subscribing to more channels makes message processing slower per message."
[OK] Correct: Each message is handled individually; subscribing to more channels only increases the variety of messages, not the time per message.
Understanding how message handling scales helps you explain real-time data flow and responsiveness in applications.
"What if we subscribed to 100 channels instead of 2? How would the time complexity of receiving messages change?"
Practice
SUBSCRIBE command do?Solution
Step 1: Understand the purpose of SUBSCRIBE
The SUBSCRIBE command is used to listen to messages published on channels in Redis.Step 2: Identify the correct function
Only Connects the client to one or more channels to receive messages describes connecting to channels to receive live messages, which matches SUBSCRIBE's purpose.Final Answer:
Connects the client to one or more channels to receive messages -> Option AQuick Check:
SUBSCRIBE = Connect to channels [OK]
- Confusing SUBSCRIBE with deleting or saving data
- Thinking SUBSCRIBE lists keys
- Assuming SUBSCRIBE modifies data
Solution
Step 1: Recall Redis CLI command format
Redis commands are typed in uppercase followed by arguments separated by spaces.Step 2: Match syntax with options
SUBSCRIBE news sports matches the correct Redis CLI syntax:SUBSCRIBE news sports. Other options use incorrect syntax or punctuation.Final Answer:
SUBSCRIBE news sports -> Option BQuick Check:
Correct Redis CLI syntax = SUBSCRIBE news sports [OK]
- Using parentheses like a function call
- Adding commas between channels
- Using extra keywords like TO or AND
Solution
Step 1: Recall how to subscribe to multiple channels
Redis SUBSCRIBE accepts multiple channel names separated by spaces in one command.Step 2: Evaluate options for correctness
SUBSCRIBE chat alerts updates uses correct syntax:SUBSCRIBE chat alerts updates. Other options use invalid syntax or multiple commands unnecessarily.Final Answer:
SUBSCRIBE chat alerts updates -> Option DQuick Check:
One SUBSCRIBE with multiple channels = SUBSCRIBE chat alerts updates [OK]
- Using commas or brackets in channel list
- Trying multiple SUBSCRIBE commands instead of one
- Adding extra keywords like TO
SUBSCRIBE sports PUBLISH sports "Game tonight!"
Solution
Step 1: Understand SUBSCRIBE effect
After SUBSCRIBE sports, the client enters pub/sub mode where it can receive messages published to subscribed channels.Step 2: Check PUBLISH behavior
PUBLISH can be executed by any client, including the one subscribed. The client will receive the message "Game tonight!" on the 'sports' channel.Final Answer:
The client receives the message "Game tonight!" on the 'sports' channel -> Option CQuick Check:
SUBSCRIBE then PUBLISH = client receives message [OK]
- Thinking PUBLISH works and client receives own message
- Assuming PUBLISH succeeds but client doesn't receive
- Believing PUBLISH causes unsubscribe
SUBSCRIBE without any channel names. What is the result?Solution
Step 1: Check SUBSCRIBE command requirements
SUBSCRIBE must be followed by at least one channel name to subscribe to.Step 2: Identify error from missing arguments
Running SUBSCRIBE alone causes a syntax error because no channels are specified.Final Answer:
Syntax error: SUBSCRIBE requires at least one channel -> Option AQuick Check:
SUBSCRIBE needs channels = syntax error if missing [OK]
- Assuming SUBSCRIBE with no args subscribes to all channels
- Thinking it unsubscribes instead
- Believing command is ignored silently
Ready to prove it?
Pass the test and this pattern moves unknown
