Pub/Sub
Redis · Reference cheat sheet
Pub/Sub
Redis · Reference cheat sheet
📋 Overview
Pub/Sub broadcasts messages to subscribers on channels. Fire-and-forget: if no subscriber is listening, messages are not queued (unlike Streams/lists).
🔧 Core concepts
| Command | Role |
|---|---|
PUBLISH channel message | Send message |
SUBSCRIBE channel… | Subscribe (enters sub mode) |
PSUBSCRIBE pattern | Pattern subscribe |
UNSUBSCRIBE | Leave channels |
PUBSUB CHANNELS | List active channels |
| vs alternatives | When |
|---|---|
| Pub/Sub | Live fan-out, presence |
| Lists | Work queues with persistence in memory |
| Streams | Consumer groups, replay |
💡 Examples
Publisher:
PUBLISH news:alerts "deploy started"Subscriber (blocks):
SUBSCRIBE news:alertsPattern:
PSUBSCRIBE news:*⚠️ Pitfalls
- Subscribers must be connected at publish time — no backlog.
- A connection in subscribe mode cannot run normal commands until unsubscribed.
- For reliable messaging, prefer Streams or an external broker.