Why pub/sub enables real-time messaging in Redis - Performance Analysis
We want to understand how the time it takes to send messages changes as more messages or subscribers are involved in Redis pub/sub.
How does Redis handle sending messages quickly to many listeners?
Analyze the time complexity of the following Redis pub/sub commands.
# Subscriber subscribes to a channel
SUBSCRIBE news
# Publisher sends a message to the channel
PUBLISH news "Hello subscribers!"
# Redis delivers the message to all subscribers of 'news'
This code shows how Redis sends a message to all clients subscribed to a channel.
Look at what repeats when a message is published.
- Primary operation: Redis sends the message to each subscriber of the channel.
- How many times: Once for each subscriber connected to that channel.
As the number of subscribers grows, the work Redis does grows too.
| Input Size (subscribers) | Approx. Operations |
|---|---|
| 10 | 10 message sends |
| 100 | 100 message sends |
| 1000 | 1000 message sends |
Pattern observation: The work grows directly with the number of subscribers. More subscribers mean more message sends.
Time Complexity: O(n)
This means the time to deliver a message grows linearly with the number of subscribers listening to the channel.
[X] Wrong: "Publishing a message takes the same time no matter how many subscribers there are."
[OK] Correct: Redis must send the message to each subscriber, so more subscribers mean more work and more time.
Understanding how message delivery time grows helps you explain real-time systems clearly and shows you know how scaling affects performance.
"What if Redis used a different method that sends one message to all subscribers at once? How would that change the time complexity?"
Practice
Solution
Step 1: Understand Pub/Sub message delivery
Redis Pub/Sub sends messages immediately to all clients subscribed to a channel.Step 2: Compare with other messaging methods
Unlike storing messages or batch sending, Pub/Sub delivers instantly without delay.Final Answer:
Messages are sent instantly to all subscribers as soon as they are published. -> Option CQuick Check:
Instant delivery = Real-time messaging [OK]
- Thinking messages are stored permanently
- Believing subscribers pull messages manually
- Confusing batch sending with real-time
Solution
Step 1: Identify subscription command
The SUBSCRIBE command tells Redis the client wants to listen to messages on a channel.Step 2: Differentiate from other commands
PUBLISH sends messages; GET and SET are for key-value operations, not subscriptions.Final Answer:
SUBSCRIBE -> Option DQuick Check:
SUBSCRIBE = listen to messages [OK]
- Using PUBLISH to subscribe
- Confusing GET/SET with Pub/Sub commands
- Thinking SUBSCRIBE stores messages
Client A: SUBSCRIBE news Client B: PUBLISH news "Hello World!"
What will Client A receive?
Solution
Step 1: Analyze subscription timing
Client A subscribes before Client B publishes, so Client A is listening when message is sent.Step 2: Understand Pub/Sub delivery
Messages are delivered instantly to all current subscribers on the channel.Final Answer:
"Hello World!" message immediately -> Option AQuick Check:
Subscribed before publish = immediate message [OK]
- Thinking message is delayed
- Assuming message is lost if published after subscribe
- Expecting error on publish
SUBSCRIBE sports PUBLISH sports "Game starts now!"
But your subscriber never receives the message. What is the likely problem?
Solution
Step 1: Understand Pub/Sub client behavior
SUBSCRIBE blocks the client to listen continuously; mixing it with PUBLISH in same client causes no message received.Step 2: Identify correct usage
Separate clients should SUBSCRIBE and PUBLISH; subscriber must stay connected to receive messages.Final Answer:
The subscriber must run SUBSCRIBE and listen continuously; mixing commands causes issues. -> Option BQuick Check:
Separate clients for SUBSCRIBE and PUBLISH [OK]
- Running SUBSCRIBE and PUBLISH in same client
- Expecting Redis to store messages
- Assuming misspelled commands cause silent failure
Solution
Step 1: Understand Pub/Sub strengths and limits
Pub/Sub delivers instantly but does not store messages for offline clients.Step 2: Combine real-time and reliability
Storing notifications in a list allows clients to fetch missed messages if disconnected.Step 3: Evaluate other options
Polling causes delay; email is not real-time; relying only on Pub/Sub risks lost messages.Final Answer:
Use Pub/Sub for instant delivery and also store notifications in a list for missed messages. -> Option AQuick Check:
Combine Pub/Sub + storage = instant + reliable [OK]
- Relying only on Pub/Sub without storage
- Using polling causing delays
- Confusing email with real-time messaging
Ready to prove it?
Pass the test and this pattern moves unknown
