Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Command

SUBSCRIBE

Subscribes the client to one or more exact channel names. Redis then pushes matching published messages and subscription confirmations through that connection.

Since
Redis Open Source 2.0.0

How it works

SUBSCRIBE registers the current client for one or more exact channel names. After the client enters subscribed mode, Redis pushes matching published messages over the connection and sends a subscription confirmation for each channel; with RESP2, only a limited set of commands remains available until the client leaves that mode.

Syntax

SUBSCRIBE channel [channel ...]

Parameters

NameTypeDescription
channel [channel ...]stringOne or more exact channel names to which the client should subscribe.

Returns

On success, Redis does not return a conventional value. Instead, it pushes one subscription confirmation message for each requested channel, with subscribe as the first element.

Key points

  • The command creates exact-match subscriptions for every channel named in the request.
  • At least one channel is required, and multiple channels can be supplied in the same command.
  • Redis sends one subscription-confirmation push message per channel after successful registration.
  • Published messages are delivered to every client subscribed to the corresponding channel.
  • The operation scales linearly with the number of channels in the request.
  • The command has been available since Redis Open Source 2.0.0.

Common mistakes

  • In RESP2 subscribed mode, do not send ordinary commands; use the permitted subscription, unsubscription, PING, RESET, or QUIT commands. RESP3 permits any commands while subscribed.
  • SUBSCRIBE provides exact channel matching, not pattern matching; use PSUBSCRIBE when a glob-style channel pattern is required.

Example: Subscribe to an exact channel

SUBSCRIBE cache:events

Run this command in redis-cli to enter Pub/Sub mode and listen for messages published to the exact channel cache:events. The client remains waiting for pushed messages until it unsubscribes or exits.

When to use it

Exact-topic event delivery

A service needs real-time notifications for a known topic such as orders:created.

Multiple channel subscriptions

Several known application topics should share one subscriber connection.

Interactive Pub/Sub monitoring

A lightweight client needs to observe live channel traffic during development or operations.

Interview questions

QWhat does SUBSCRIBE do?

SUBSCRIBE takes one or more channel names and switches the connection into subscribed mode. Redis pushes a confirmation for each subscription, followed by messages published to those channels.

Follow-up: How would you subscribe to a family of channels? Use PSUBSCRIBE with a pattern, because SUBSCRIBE matches only the explicitly named channels.

QWhat is the time complexity of SUBSCRIBE?

The command's complexity is O(N), where N is the number of channels being subscribed to. Each specified channel contributes to the subscription operation.

Follow-up: What is the practical implication of subscribing to many channels? The command processes each channel, so the work grows linearly with the number supplied.

QWhich commands are allowed after entering subscribed state?

A RESP2 client in subscribed state is restricted to subscription-management commands and a few connection commands such as PING and QUIT. A RESP3 client can issue any commands while remaining subscribed.

Follow-up: How can a RESP2 client return to normal command use? It must unsubscribe from its channels and leave subscribed mode.

  • PSUBSCRIBE
  • UNSUBSCRIBE
  • PUNSUBSCRIBE
  • PUBLISH
  • SSUBSCRIBE
  • SUNSUBSCRIBE
  • PING
  • RESET

Source: Subscribe | Redis. Checked 2026-10-16.