0
0
Redisquery~10 mins

PSUBSCRIBE for pattern matching in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - PSUBSCRIBE for pattern matching
Client sends PSUBSCRIBE with pattern
Server registers pattern subscription
Messages published to channels
Server matches published channel names to patterns
If match, server sends message to client
Client receives messages matching pattern
The client subscribes to channel name patterns. The server sends messages for any published channel matching those patterns.
Execution Sample
Redis
PSUBSCRIBE news.*

# Server sends messages published to channels like 'news.sports', 'news.weather' to client
Client subscribes to all channels starting with 'news.' and receives messages published to those channels.
Execution Table
StepActionPattern SubscribedPublished ChannelPattern Match?Message Sent to Client
1Client sends PSUBSCRIBEnews.*N/ANo
2Message publishednews.*news.sportsYesYes
3Message publishednews.*news.weatherYesYes
4Message publishednews.*sports.newsNoNo
5Message publishednews.*newsNoNo
6Message publishednews.*news.sports.footballYesYes
7Client unsubscribesnews.*N/ANo
8Message publishednews.*news.sportsNo (unsubscribed)No
💡 Client unsubscribed from pattern; no further messages sent.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 7Final
Subscribed Patterns[]["news.*"]["news.*"][][]
Messages Received[][]["news.sports"]["news.sports", "news.weather", "news.sports.football"]["news.sports", "news.weather", "news.sports.football"]
Key Moments - 3 Insights
Why does 'news.sports.football' match the pattern 'news.*'?
Because the pattern 'news.*' matches any channel starting with 'news.' followed by any characters, including dots. This is shown in execution_table row 6 where the match is Yes.
Why doesn't 'sports.news' match the pattern 'news.*'?
Because 'sports.news' does not start with 'news.', so it does not match the pattern. This is shown in execution_table row 4 where the match is No.
What happens when the client unsubscribes from the pattern?
After unsubscribing (step 7), the client no longer receives messages even if channels match the pattern, as shown in step 8 where no message is sent.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, which channel was published and did it match the pattern?
A"news.sports", Yes
B"sports.news", No
C"news.weather", Yes
D"news", No
💡 Hint
Check row 3 under Published Channel and Pattern Match? columns.
At which step does the client stop receiving messages?
AStep 8
BStep 7
CStep 5
DStep 6
💡 Hint
Look at the exit_note and step 8 in execution_table where no message is sent after unsubscribe.
If the pattern was changed to 'news.sports*', which published channel would NOT send a message?
A"news.sports.football"
B"news.weather"
C"news.sports"
D"news.sports.basketball"
💡 Hint
Consider which channels start exactly with 'news.sports' as per pattern matching rules.
Concept Snapshot
PSUBSCRIBE lets clients subscribe to channel name patterns.
Patterns use '*' as wildcard for any characters.
Server sends messages for any published channel matching the pattern.
Useful to listen to groups of related channels.
Unsubscribe stops receiving matching messages.
Pattern matching is prefix-based with wildcards.
Full Transcript
PSUBSCRIBE in Redis allows a client to subscribe to channels using patterns with wildcards. When the client sends PSUBSCRIBE with a pattern like 'news.*', the server registers this subscription. Whenever a message is published to any channel matching this pattern, such as 'news.sports' or 'news.weather', the server sends the message to the client. Channels that do not match the pattern, like 'sports.news', do not trigger messages. If the client unsubscribes from the pattern, no further messages are sent even if channels match. This visual trace shows each step: subscribing, messages published, pattern matching, messages sent, and unsubscribing. It helps beginners see how pattern matching works in real time and understand why some messages are received and others are not.