Real-time notification pattern in Redis - Time & Space Complexity
When using Redis for real-time notifications, it is important to understand how the time to send messages grows as more users or notifications are involved.
We want to know how the system handles more notifications and users efficiently.
Analyze the time complexity of the following Redis code snippet.
# Publish a message to a channel
PUBLISH notifications_channel "New message arrived"
# Clients subscribe to the channel
SUBSCRIBE notifications_channel
# Server pushes notifications to all subscribers in real-time
This code sends a notification message to all clients subscribed to a channel instantly.
In this pattern, the main repeating operation is the message delivery to each subscriber.
- Primary operation: Delivering the published message to each subscriber.
- How many times: Once per subscriber connected to the channel.
The time to deliver notifications grows as the number of subscribers increases.
| Input Size (n subscribers) | Approx. Operations |
|---|---|
| 10 | 10 message deliveries |
| 100 | 100 message deliveries |
| 1000 | 1000 message deliveries |
Pattern observation: The work grows directly with the number of subscribers; more subscribers mean more message deliveries.
Time Complexity: O(n)
This means the time to send notifications 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 total delivery time.
Understanding how notification delivery scales helps you design systems that handle many users smoothly and shows you can think about real-world performance.
"What if we changed from using PUBLISH/SUBSCRIBE to storing notifications in a list for each user? How would the time complexity change?"
Practice
PUBLISH command in Redis real-time notification pattern?Solution
Step 1: Understand the role of PUBLISH
ThePUBLISHcommand is used to send messages to a channel in Redis.Step 2: Differentiate from SUBSCRIBE
SUBSCRIBElistens for messages, butPUBLISHsends them.Final Answer:
To send messages to a specific channel -> Option BQuick Check:
PUBLISH sends messages [OK]
- Confusing PUBLISH with SUBSCRIBE
- Thinking PUBLISH stores data
- Assuming PUBLISH deletes channels
news in Redis?Solution
Step 1: Recall Redis SUBSCRIBE syntax
The correct syntax isSUBSCRIBE channel_namewithout parentheses.Step 2: Check each option
SUBSCRIBE news matches the correct syntax. SUBSCRIBE('news') uses parentheses which is invalid in Redis CLI.Final Answer:
SUBSCRIBE news -> Option DQuick Check:
SUBSCRIBE channel_name [OK]
- Using parentheses like a function call
- Confusing PUBLISH with SUBSCRIBE
- Using LISTEN which is not a Redis command
SUBSCRIBE updates PUBLISH updates "New message arrived"What will the subscriber receive?
Solution
Step 1: Understand SUBSCRIBE behavior
SUBSCRIBE listens to messages on the specified channel and receives published messages.Step 2: Analyze PUBLISH effect
PUBLISH sends the message "New message arrived" to theupdateschannel, which the subscriber listens to.Final Answer:
"New message arrived" on the updates channel -> Option AQuick Check:
SUBSCRIBE receives PUBLISH messages [OK]
- Thinking SUBSCRIBE blocks all commands
- Believing PUBLISH causes errors after SUBSCRIBE
- Assuming messages go to wrong channels
SUBSCRIBE updates PUBLISH updates "Hello" UNSUBSCRIBE updates PUBLISH updates "Bye"Why does the subscriber not receive the "Bye" message?
Solution
Step 1: Understand UNSUBSCRIBE effect
UNSUBSCRIBE removes the subscriber from the channel, so it stops receiving messages.Step 2: Analyze message delivery after UNSUBSCRIBE
After unsubscribing, the subscriber no longer listens, so "Bye" is not received.Final Answer:
Because UNSUBSCRIBE stops listening to the channel -> Option CQuick Check:
UNSUBSCRIBE stops message reception [OK]
- Thinking PUBLISH only works before SUBSCRIBE
- Assuming messages are lost after first publish
- Believing SUBSCRIBE blocks PUBLISH commands
Solution
Step 1: Understand real-time delivery needs
Real-time notifications require instant delivery without polling delays.Step 2: Evaluate options for message delivery
PUBLISH/SUBSCRIBE keeps clients connected and delivers messages instantly to subscribed channels.Step 3: Compare with alternatives
Polling (Use Redis lists to push messages and clients poll periodically) causes delays; fetching keys manually (Use PUBLISH to send messages and clients fetch keys manually) is inefficient; transactions (Use Redis transactions to lock channels during message send) do not help with message delivery.Final Answer:
Use PUBLISH/SUBSCRIBE pattern and keep subscribers connected continuously -> Option AQuick Check:
Continuous SUBSCRIBE ensures real-time messages [OK]
- Using polling instead of real-time subscription
- Trying to fetch messages manually
- Misusing transactions for message delivery
Ready to prove it?
Pass the test and this pattern moves unknown
