PSUBSCRIBE for pattern matching in Redis - Time & Space Complexity
When using PSUBSCRIBE in Redis, we want to understand how the time to process messages changes as more patterns or messages are involved.
We ask: How does Redis handle matching messages to patterns as the workload grows?
Analyze the time complexity of this Redis PSUBSCRIBE usage.
# Subscribe to channels matching patterns
PSUBSCRIBE news.* sports.* weather.*
# When a message is published to a channel
PUBLISH news.local "Breaking news!"
PUBLISH sports.football "Goal scored!"
This code subscribes to multiple channel patterns and receives messages published to matching channels.
Look at what repeats when messages arrive.
- Primary operation: Matching each published message's channel name against all subscribed patterns.
- How many times: For each message, Redis checks all patterns to find matches.
As the number of patterns grows, Redis must check more patterns for each message.
| Input Size (number of patterns) | Approx. Operations per message |
|---|---|
| 10 | 10 pattern checks |
| 100 | 100 pattern checks |
| 1000 | 1000 pattern checks |
Pattern observation: The work grows directly with the number of patterns; doubling patterns doubles checks.
Time Complexity: O(p)
This means the time to process each message grows linearly with the number of subscribed patterns.
[X] Wrong: "Matching a message to patterns is instant no matter how many patterns there are."
[OK] Correct: Redis must check each pattern to see if it matches, so more patterns mean more work and longer processing time.
Understanding how pattern matching scales helps you explain performance trade-offs in real systems using Redis Pub/Sub.
What if Redis used a more advanced data structure to index patterns? How might that change the time complexity?
Practice
PSUBSCRIBE do?Solution
Step 1: Understand the purpose of PSUBSCRIBE
PSUBSCRIBE listens to messages from channels that match a given pattern, using wildcards like '*'.Step 2: Compare with other commands
Unlike PUBLISH or UNSUBSCRIBE, PSUBSCRIBE focuses on pattern-based subscription.Final Answer:
It subscribes to channels matching a pattern with wildcards. -> Option AQuick Check:
PSUBSCRIBE = pattern subscription [OK]
- Confusing PSUBSCRIBE with PUBLISH
- Thinking PSUBSCRIBE deletes keys
- Mixing PSUBSCRIBE with UNSUBSCRIBE
Solution
Step 1: Identify the wildcard for pattern matching
In Redis PSUBSCRIBE, '*' matches any sequence of characters.Step 2: Match the pattern for channels starting with 'news.'
The pattern 'news.*' matches all channels beginning with 'news.' followed by anything.Final Answer:
PSUBSCRIBE news.* -> Option BQuick Check:
'*' wildcard = correct syntax [OK]
- Using '?' which matches exactly one character
- Using '#' or '+' which are not Redis wildcards
- Missing the dot after 'news'
PSUBSCRIBE sports.* PUBLISH sports.football "Goal!" PUBLISH sports.basketball "Score!" PUBLISH news.weather "Rain"
Which messages will the subscriber receive?
Solution
Step 1: Understand the subscription pattern
The subscriber listens to channels matching 'sports.*', meaning any channel starting with 'sports.'.Step 2: Check published messages against the pattern
Messages on 'sports.football' and 'sports.basketball' match the pattern; 'news.weather' does not.Final Answer:
"Goal!" and "Score!" -> Option AQuick Check:
Pattern matches sports.* channels only [OK]
- Assuming all published messages are received
- Confusing exact channel with pattern
- Ignoring the pattern prefix
PSUBSCRIBE sports.* but receive no messages when publishing to sports.football. What is the most likely mistake?Solution
Step 1: Check the channel names for exact pattern match
The pattern 'sports.*' matches channels starting with 'sports.' including the dot.Step 2: Identify mismatch in published channel
Publishing to 'sportsfootball' (no dot) does not match 'sports.*' pattern.Final Answer:
You subscribed to sports.* but published to sportsfootball without a dot. -> Option CQuick Check:
Pattern requires exact prefix including dot [OK]
- Confusing SUBSCRIBE with PSUBSCRIBE
- Ignoring dot in channel names
- Assuming order of commands affects delivery
Solution
Step 1: Understand the wildcard '*' usage
'*' matches any sequence of characters, including none.Step 2: Create pattern to match 'chat' anywhere
Using '*chat*' matches any channel with 'chat' anywhere in its name.Final Answer:
*chat* -> Option DQuick Check:
Use '*' before and after 'chat' to match anywhere [OK]
- Using 'chat' alone matches exact channel only
- Using 'chat*' matches only channels starting with 'chat'
- Using '*chat' matches only channels ending with 'chat'
Ready to prove it?
Pass the test and this pattern moves unknown
