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

Redis · Command

PUBLISH

PUBLISH sends a message to a channel for delivery to its current subscribers. It returns the number of clients that received the message.

Since
Redis Open Source 2.0.0

How it works

PUBLISH sends a message to every client currently subscribed to the specified channel. In a Redis Cluster, Redis forwards the publication as needed, but the returned count includes only clients connected to the publisher's node.

Syntax

PUBLISH channel message

Parameters

NameTypeDescription
channelchannelChannel to publish the message to.
messagemessageMessage to publish.

Returns

Integer reply: the number of clients that the message was sent to.

Key points

  • PUBLISH broadcasts the supplied payload on one named channel.
  • Cluster clients may issue PUBLISH against any node.
  • Redis Cluster routes publications so subscribers can connect through any node.
  • The integer response reports how many clients received the publication.
  • Its cost grows with direct subscribers and subscribed patterns.
  • PUBLISH has been available since Redis Open Source 2.0.0.

Common mistakes

  • In a Redis Cluster, the integer reply does not count subscribers connected to other nodes.

Example: Publish to a channel

redis-cli SUBSCRIBE news
redis-cli PUBLISH news "Redis update"

Run the subscriber in one terminal and the publisher in another; the publisher receives the number of clients that received the message.

When to use it

Real-time notifications

Broadcast an event to all currently connected consumers of a topic.

Job completion signaling

Signal a waiting service that an operation completed without polling.

Event fan-out

Fan out a serialized application event to active listeners.

Interview questions

QWhat does PUBLISH do, and what does it return?

PUBLISH takes a channel and a message, then returns an integer representing the number of clients that received it. The command does not require subscribers to exist.

Follow-up: How does this count behave in a cluster? It includes only clients connected to the same node as the publishing client.

QWhat are PUBLISH's arguments?

The command uses the syntax PUBLISH channel message. The channel identifies the publication topic, while the message is the payload delivered to subscribers.

Follow-up: How can clients receive pattern-based publications? They can use PSUBSCRIBE; Redis sends those deliveries as pattern-matching messages.

QWhat is the time complexity of PUBLISH?

PUBLISH has time complexity O(N+M), where N is the number of clients subscribed to the receiving channel and M is the total number of subscribed patterns.

Follow-up: What delivery guarantee does Redis Pub/Sub provide? At-most-once delivery: a message is delivered once if at all.

  • PUBLISH
  • SUBSCRIBE
  • UNSUBSCRIBE
  • PSUBSCRIBE
  • SPUBLISH

Source: Publish. Checked 2026-10-16.