This is article 42 in the Big Data series. Deep analysis of Redis Pub/Sub mechanism and its applicable boundaries in distributed systems.
Complete illustrated version: CSDN Original | Juejin
Core Concepts
Redis Pub/Sub is a message communication mode. Publishers and Subscribers are both Redis clients, Channels exist on server. The three are decoupled—publishers don’t need to know who’s subscribing, subscribers don’t need to care who’s publishing.
Basic Commands
# Subscribe to one or more channels
SUBSCRIBE channel1 channel2
# Pattern subscription (supports wildcards)
PSUBSCRIBE news.*
# Publish message to channel
PUBLISH channel message
# Unsubscribe
UNSUBSCRIBE channel1
PUNSUBSCRIBE news.*
Working Mechanism
Pub/Sub follows these principles:
- Messages not persisted: Redis doesn’t save messages, only pushes to currently connected subscribers
- Fire-and-Forget: No acknowledgment after sending, no retry
- Auto unsubscribe: Subscription relationship automatically invalidates after client disconnects
Three Weak Transaction Defects
1. Unreliable Delivery
If no active subscribers when publishing, message is immediately discarded.
2. No Acknowledgment
After publisher sends message, cannot know which subscribers received it or if processing succeeded.
3. No Retry
No retry mechanism for failed message delivery.