Bird
Raised Fist0
Redisquery~20 mins

PSUBSCRIBE for pattern matching in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PSUBSCRIBE Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What messages does this PSUBSCRIBE receive?
Given the Redis command PSUBSCRIBE news.*, which published messages will the subscriber receive?
Redis
PSUBSCRIBE news.*

PUBLISH news.sports "Sports update"
PUBLISH news.weather "Weather update"
PUBLISH news "General news"
PUBLISH news.sports.football "Football news"
AReceives messages from channels: news only
BReceives messages from channels: news, news.sports, news.weather
CReceives messages from channels: news, news.sports.football
DReceives messages from channels: news.sports, news.weather, news.sports.football
Attempts:
2 left
💡 Hint
Remember that the asterisk (*) matches any characters in the channel name after the prefix.
📝 Syntax
intermediate
1:30remaining
Identify the correct PSUBSCRIBE command syntax
Which of the following Redis commands correctly subscribes to all channels starting with 'chat.'?
APSUBSCRIBE chat.*
BSUBSCRIBE chat.*
CPSUBSCRIBE chat
DSUBSCRIBE chat
Attempts:
2 left
💡 Hint
PSUBSCRIBE supports patterns, SUBSCRIBE does not.
❓ optimization
advanced
2:30remaining
Optimizing pattern subscriptions for multiple similar channels
You want to subscribe to all channels related to user notifications, which are named like 'user.123', 'user.456', 'user.789'. Which PSUBSCRIBE pattern is the most efficient and correct?
APSUBSCRIBE user.?
BPSUBSCRIBE user.*
CPSUBSCRIBE user.[0-9]*
DPSUBSCRIBE user
Attempts:
2 left
💡 Hint
Consider how Redis pattern matching works with wildcards.
🔧 Debug
advanced
2:30remaining
Why does this PSUBSCRIBE pattern not receive expected messages?
A developer runs PSUBSCRIBE user.*.notifications but does not receive messages published to 'user.123notifications'. What is the likely cause?
AThe PSUBSCRIBE command requires escaping dots, so the pattern should be 'user\. *.notifications'.
BRedis patterns treat '*' as matching only one character, so it does not match '123'.
CThe pattern 'user.*.notifications' does not match 'user.123notifications' because Redis patterns do not support multiple wildcards separated by dots.
DThe pattern is correct; the issue is that messages are not published to 'user.123notifications'.
Attempts:
2 left
💡 Hint
Check how Redis pattern matching treats the '*' wildcard.
🧠 Conceptual
expert
3:00remaining
Understanding PSUBSCRIBE pattern matching behavior
Which statement correctly describes how Redis PSUBSCRIBE pattern matching works?
AThe '*' wildcard matches any sequence of characters, including dots, and patterns are matched against the entire channel name.
BThe '*' wildcard matches only characters up to the first dot, so patterns cannot match across dots.
CPatterns in PSUBSCRIBE are matched only against the prefix of channel names, not the entire name.
DPSUBSCRIBE patterns require regular expressions syntax for matching channel names.
Attempts:
2 left
💡 Hint
Think about how the '*' wildcard behaves in Redis channel patterns.

Practice

(1/5)
1. What does the Redis command PSUBSCRIBE do?
easy
A. It subscribes to channels matching a pattern with wildcards.
B. It unsubscribes from all channels.
C. It publishes messages to a channel.
D. It deletes keys matching a pattern.

Solution

  1. Step 1: Understand the purpose of PSUBSCRIBE

    PSUBSCRIBE listens to messages from channels that match a given pattern, using wildcards like '*'.
  2. Step 2: Compare with other commands

    Unlike PUBLISH or UNSUBSCRIBE, PSUBSCRIBE focuses on pattern-based subscription.
  3. Final Answer:

    It subscribes to channels matching a pattern with wildcards. -> Option A
  4. Quick Check:

    PSUBSCRIBE = pattern subscription [OK]
Hint: PSUBSCRIBE listens to patterns, not single channels [OK]
Common Mistakes:
  • Confusing PSUBSCRIBE with PUBLISH
  • Thinking PSUBSCRIBE deletes keys
  • Mixing PSUBSCRIBE with UNSUBSCRIBE
2. Which of the following is the correct syntax to subscribe to all channels starting with 'news.' using PSUBSCRIBE?
easy
A. PSUBSCRIBE news?
B. PSUBSCRIBE news.*
C. PSUBSCRIBE news#
D. PSUBSCRIBE news+

Solution

  1. Step 1: Identify the wildcard for pattern matching

    In Redis PSUBSCRIBE, '*' matches any sequence of characters.
  2. Step 2: Match the pattern for channels starting with 'news.'

    The pattern 'news.*' matches all channels beginning with 'news.' followed by anything.
  3. Final Answer:

    PSUBSCRIBE news.* -> Option B
  4. Quick Check:

    '*' wildcard = correct syntax [OK]
Hint: Use '*' to match any characters in PSUBSCRIBE patterns [OK]
Common Mistakes:
  • Using '?' which matches exactly one character
  • Using '#' or '+' which are not Redis wildcards
  • Missing the dot after 'news'
3. Given the following Redis commands executed in order:
PSUBSCRIBE sports.*
PUBLISH sports.football "Goal!"
PUBLISH sports.basketball "Score!"
PUBLISH news.weather "Rain"

Which messages will the subscriber receive?
medium
A. "Goal!" and "Score!"
B. "Goal!" only
C. "Rain" only
D. All three messages

Solution

  1. Step 1: Understand the subscription pattern

    The subscriber listens to channels matching 'sports.*', meaning any channel starting with 'sports.'.
  2. Step 2: Check published messages against the pattern

    Messages on 'sports.football' and 'sports.basketball' match the pattern; 'news.weather' does not.
  3. Final Answer:

    "Goal!" and "Score!" -> Option A
  4. Quick Check:

    Pattern matches sports.* channels only [OK]
Hint: Only messages on matching channels are received [OK]
Common Mistakes:
  • Assuming all published messages are received
  • Confusing exact channel with pattern
  • Ignoring the pattern prefix
4. You run the command PSUBSCRIBE sports.* but receive no messages when publishing to sports.football. What is the most likely mistake?
medium
A. You published messages before subscribing.
B. You used SUBSCRIBE instead of PSUBSCRIBE.
C. You subscribed to sports.* but published to sportsfootball without a dot.
D. You used the wrong Redis server version.

Solution

  1. Step 1: Check the channel names for exact pattern match

    The pattern 'sports.*' matches channels starting with 'sports.' including the dot.
  2. Step 2: Identify mismatch in published channel

    Publishing to 'sportsfootball' (no dot) does not match 'sports.*' pattern.
  3. Final Answer:

    You subscribed to sports.* but published to sportsfootball without a dot. -> Option C
  4. Quick Check:

    Pattern requires exact prefix including dot [OK]
Hint: Check exact channel names match pattern including dots [OK]
Common Mistakes:
  • Confusing SUBSCRIBE with PSUBSCRIBE
  • Ignoring dot in channel names
  • Assuming order of commands affects delivery
5. You want to listen to all channels that contain the word 'chat' anywhere in their name using PSUBSCRIBE. Which pattern should you use?
hard
A. chat
B. chat*
C. *chat
D. *chat*

Solution

  1. Step 1: Understand the wildcard '*' usage

    '*' matches any sequence of characters, including none.
  2. Step 2: Create pattern to match 'chat' anywhere

    Using '*chat*' matches any channel with 'chat' anywhere in its name.
  3. Final Answer:

    *chat* -> Option D
  4. Quick Check:

    Use '*' before and after 'chat' to match anywhere [OK]
Hint: Use '*' before and after word to match anywhere [OK]
Common Mistakes:
  • Using 'chat' alone matches exact channel only
  • Using 'chat*' matches only channels starting with 'chat'
  • Using '*chat' matches only channels ending with 'chat'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard