Bird
Raised Fist0
Redisquery~10 mins

UNSUBSCRIBE behavior in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - UNSUBSCRIBE behavior
Client SUBSCRIBED to channels
↓
Client sends UNSUBSCRIBE command
↓
Server removes client from specified channels
↓
Server sends confirmation message for each channel unsubscribed
↓
If no channels specified, unsubscribe from all
↓
Client no longer receives messages from those channels
This flow shows how a client unsubscribes from channels in Redis Pub/Sub, stopping message delivery from those channels.
Execution Sample
Redis
SUBSCRIBE news sports
UNSUBSCRIBE news
UNSUBSCRIBE
Client subscribes to 'news' and 'sports', then unsubscribes from 'news', then unsubscribes from all remaining channels.
Execution Table
StepClient CommandChannels AffectedServer ActionServer ResponseClient Subscription State
1SUBSCRIBE news sportsnews, sportsAdd client to 'news' and 'sports'[subscribe, news, 1], [subscribe, sports, 2]Subscribed to news, sports
2UNSUBSCRIBE newsnewsRemove client from 'news'[unsubscribe, news, 1]Subscribed to sports
3UNSUBSCRIBEall remainingRemove client from all channels[unsubscribe, sports, 0]Subscribed to none
4No more subscriptionsnoneNo actionNo responseSubscribed to none
💡 Client has unsubscribed from all channels, so no more messages will be received.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Subscribed Channels[][news, sports][sports][][]
Key Moments - 3 Insights
Why does the server send a confirmation message after UNSUBSCRIBE?
The server sends a confirmation message (see execution_table step 2 and 3) to inform the client which channel it has unsubscribed from and how many channels remain subscribed.
What happens if UNSUBSCRIBE is called without specifying channels?
If no channels are specified (execution_table step 3), the server unsubscribes the client from all subscribed channels, clearing the subscription list.
Does the client receive messages from unsubscribed channels?
No, after unsubscribing (execution_table step 2 and 3), the client no longer receives messages from those channels.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the client's subscription state after step 2?
ASubscribed to none
BSubscribed to news and sports
CSubscribed to sports only
DSubscribed to news only
💡 Hint
Check the 'Client Subscription State' column in execution_table row for step 2.
At which step does the client unsubscribe from all channels?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Channels Affected' and 'Server Action' columns in execution_table.
If the client only subscribed to 'news' and then called UNSUBSCRIBE without arguments, what would be the subscription state after?
ASubscribed to none
BSubscribed to news
CSubscribed to all channels
DSubscribed to sports
💡 Hint
Refer to the behavior in execution_table step 3 where UNSUBSCRIBE without channels clears all subscriptions.
Concept Snapshot
UNSUBSCRIBE command removes client subscriptions.
Specify channels to unsubscribe from those only.
No channels means unsubscribe from all.
Server sends confirmation messages.
Client stops receiving messages from unsubscribed channels.
Full Transcript
The UNSUBSCRIBE command in Redis Pub/Sub lets a client stop receiving messages from channels. When a client sends UNSUBSCRIBE with channel names, the server removes the client from those channels and sends confirmation messages. If no channels are specified, the client is unsubscribed from all channels. This stops message delivery from those channels to the client. The execution table shows a client subscribing to 'news' and 'sports', then unsubscribing from 'news', and finally unsubscribing from all remaining channels. The variable tracker shows how the subscription list changes after each command. Key moments clarify why confirmation messages are sent and what happens when no channels are specified. The visual quiz tests understanding of subscription states at each step.

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