Command Introduction
Pub/Sub commands implement the publish/subscribe messaging pattern, establishing a real-time message channel between producers and consumers. This model is suitable for scenarios such as real-time notifications, chat message broadcasting, and configuration change push. Based on their features, the core commands can be categorized into the following three types:
Channel Subscription and Unsubscription
SUBSCRIBE / UNSUBSCRIBE: Subscribe to or unsubscribe from one or more channels precisely by channel name.
PSUBSCRIBE / PUNSUBSCRIBE: Batch subscribe to or unsubscribe from matching channels using wildcard patterns, such as news.*.
Message Publishing
PUBLISH: Publishes a message to a specified channel. All clients subscribed to that channel will receive the push notification immediately.
Channel Status Query
PUBSUB CHANNELS / PUBSUB NUMSUB / PUBSUB NUMPAT: Query the list of currently active channels, the number of subscribers for a specified channel, and the number of active pattern subscriptions. These commands are used for monitoring and Ops.
Note:
Pub/Sub messages are not persisted, and offline clients will lose messages that have not been received. If message persistence and consumption acknowledgment mechanisms are required, use the Stream family of commands. Command Support Overview
Basic Commands: psubscribe, pubsub, publish, punsubscribe, subscribe, and unsubscribe are fully supported in the standard architecture of Redis versions 2.8, 4.0, 5.0, 6.2, and 7.0, as well as in the standard and cluster architectures of Valkey versions 8.0 and 9.0.
Cross-Slot Restriction: Pub/Sub family commands do not involve cross-slot access scenarios.
Command Compatibility Matrix
The support status of Pub/Sub family commands across different versions and architectures is shown in the table below (✓ indicates supported, ✗ indicates not supported, and — indicates that the command does not involve a cross-slot access scenario).
|
psubscribe | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
pubsub | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
publish | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
punsubscribe | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
subscribe | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
unsubscribe | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |