Bird
Raised Fist0
Redisquery~20 mins

UNSUBSCRIBE behavior in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis UNSUBSCRIBE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What happens after UNSUBSCRIBE without arguments?
Consider a Redis client subscribed to channels 'news' and 'sports'. What is the output after executing UNSUBSCRIBE with no arguments?
Redis
SUBSCRIBE news sports
UNSUBSCRIBE
A1) unsubscribed from 'news' only, subscription count is 1
B1) unsubscribed from 'news', 2) unsubscribed from 'sports', 3) subscription count is 0
CError: UNSUBSCRIBE requires at least one channel
DNo output; client remains subscribed to all channels
Attempts:
2 left
💡 Hint
UNSUBSCRIBE with no arguments unsubscribes from all channels.
❓ query_result
intermediate
2:00remaining
What is the output of UNSUBSCRIBE for a non-subscribed channel?
A Redis client is subscribed to channel 'chat'. What is the output after executing UNSUBSCRIBE news where 'news' is not subscribed?
Redis
SUBSCRIBE chat
UNSUBSCRIBE news
AError: Cannot unsubscribe from a channel not subscribed
BNo unsubscribe message; subscription count remains 1
CUnsubscribe message for 'news' with subscription count 1
DUnsubscribe message for 'chat' with subscription count 0
Attempts:
2 left
💡 Hint
UNSUBSCRIBE sends an unsubscribe message even if the client was not subscribed to the channel.
🧠 Conceptual
advanced
2:00remaining
How does UNSUBSCRIBE affect pattern subscriptions?
If a client is subscribed to channels 'a' and 'b' and also to pattern 'p*', what happens when UNSUBSCRIBE b is executed?
AUnsubscribes only from channel 'b'; pattern subscription remains active
BUnsubscribes from all channels and patterns
CUnsubscribes from channel 'b' and all pattern subscriptions
DNo effect; pattern subscriptions block UNSUBSCRIBE
Attempts:
2 left
💡 Hint
UNSUBSCRIBE affects only channel subscriptions, not pattern subscriptions.
📝 Syntax
advanced
2:00remaining
What error occurs with incorrect UNSUBSCRIBE syntax?
What error does Redis return when executing UNSUBSCRIBE 123 where 123 is a number instead of a string channel name?
ASyntax error: wrong number of arguments for 'UNSUBSCRIBE' command
BRuntime error: channel not found
CError: invalid channel name type
DNo error; Redis converts 123 to string and unsubscribes
Attempts:
2 left
💡 Hint
Redis treats all channel names as strings internally.
🔧 Debug
expert
3:00remaining
Why does UNSUBSCRIBE not reduce subscription count to zero?
A client subscribed to channels 'x', 'y' and pattern 'z*' executes UNSUBSCRIBE x y. Why does the subscription count reported remain 1 instead of 0?
ABecause pattern subscriptions are not affected by UNSUBSCRIBE and remain active
BBecause UNSUBSCRIBE only marks channels for removal but does not remove them immediately
CBecause the client must call PUNSUBSCRIBE to remove all subscriptions
DBecause UNSUBSCRIBE requires a delay before updating subscription count
Attempts:
2 left
💡 Hint
Pattern subscriptions are managed separately from channel subscriptions.

Practice

(1/5)
1. What does the UNSUBSCRIBE command do in Redis Pub/Sub?
easy
A. Creates a new subscription to a channel
B. Deletes the specified channels permanently
C. Sends a message to all subscribers of a channel
D. Stops receiving messages from specified channels

Solution

  1. Step 1: Understand the purpose of UNSUBSCRIBE

    The UNSUBSCRIBE command is used to stop receiving messages from channels you previously subscribed to.
  2. Step 2: Differentiate from other commands

    It does not delete channels or send messages; it only stops message delivery to the client.
  3. Final Answer:

    Stops receiving messages from specified channels -> Option D
  4. Quick Check:

    UNSUBSCRIBE stops message delivery [OK]
Hint: UNSUBSCRIBE stops messages, it does not delete channels [OK]
Common Mistakes:
  • Thinking UNSUBSCRIBE deletes channels
  • Confusing UNSUBSCRIBE with PUBLISH
  • Assuming UNSUBSCRIBE creates subscriptions
2. Which of the following is the correct syntax to unsubscribe from all channels in Redis?
easy
A. UNSUBSCRIBE *
B. UNSUBSCRIBE ALL
C. UNSUBSCRIBE
D. UNSUBSCRIBE ()

Solution

  1. Step 1: Recall UNSUBSCRIBE syntax

    To unsubscribe from all channels, you call UNSUBSCRIBE without any arguments.
  2. Step 2: Check each option

    UNSUBSCRIBE * uses '*', which is invalid syntax. UNSUBSCRIBE ALL and D are not valid Redis commands.
  3. Final Answer:

    UNSUBSCRIBE -> Option C
  4. Quick Check:

    UNSUBSCRIBE with no args unsubscribes all [OK]
Hint: No arguments means unsubscribe from all channels [OK]
Common Mistakes:
  • Using wildcard '*' with UNSUBSCRIBE
  • Adding extra keywords like ALL
  • Using parentheses in command
3. Given the following Redis Pub/Sub commands executed by a client:
SUBSCRIBE news sports
UNSUBSCRIBE news
What channels will the client still receive messages from?
medium
A. news only
B. sports only
C. news and sports
D. none

Solution

  1. Step 1: Analyze the subscription commands

    The client subscribes to 'news' and 'sports' channels initially.
  2. Step 2: Apply UNSUBSCRIBE command

    UNSUBSCRIBE news removes subscription to 'news' only, so 'sports' remains subscribed.
  3. Final Answer:

    sports only -> Option B
  4. Quick Check:

    UNSUBSCRIBE removes specified channels only [OK]
Hint: UNSUBSCRIBE removes only listed channels, others stay subscribed [OK]
Common Mistakes:
  • Assuming UNSUBSCRIBE removes all subscriptions
  • Confusing subscribed channels after UNSUBSCRIBE
  • Thinking UNSUBSCRIBE adds subscriptions
4. You run the command UNSUBSCRIBE sports but still receive messages from the sports channel. What is the most likely cause?
medium
A. You have multiple subscriptions to sports and only one was removed
B. UNSUBSCRIBE command has a syntax error
C. You never subscribed to the sports channel
D. Redis server does not support UNSUBSCRIBE

Solution

  1. Step 1: Understand multiple subscriptions

    Redis allows multiple subscriptions to the same channel from the same client connection.
  2. Step 2: Effect of UNSUBSCRIBE on multiple subscriptions

    UNSUBSCRIBE removes one subscription per channel per call; if subscribed multiple times, some remain.
  3. Final Answer:

    You have multiple subscriptions to sports and only one was removed -> Option A
  4. Quick Check:

    Multiple subscriptions require multiple UNSUBSCRIBE calls [OK]
Hint: Multiple subscriptions need multiple UNSUBSCRIBE calls [OK]
Common Mistakes:
  • Assuming one UNSUBSCRIBE removes all duplicates
  • Thinking UNSUBSCRIBE syntax is wrong
  • Believing Redis lacks UNSUBSCRIBE support
5. A client subscribed to channels news, sports, and weather. It wants to stop receiving messages only from news and weather but keep sports. Which command should it use?
hard
A. UNSUBSCRIBE news weather
B. UNSUBSCRIBE sports
C. UNSUBSCRIBE
D. UNSUBSCRIBE all

Solution

  1. Step 1: Identify channels to unsubscribe

    The client wants to stop messages from 'news' and 'weather' only.
  2. Step 2: Use UNSUBSCRIBE with specific channels

    UNSUBSCRIBE followed by channel names unsubscribes only those channels, keeping others subscribed.
  3. Final Answer:

    UNSUBSCRIBE news weather -> Option A
  4. Quick Check:

    UNSUBSCRIBE with channels removes only those [OK]
Hint: List channels in UNSUBSCRIBE to remove only those [OK]
Common Mistakes:
  • Using UNSUBSCRIBE with no args removes all
  • Trying UNSUBSCRIBE all which is invalid
  • Unsubscribing wrong channels
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