Redis Pub/Sub
13. Redis Pub/Sub
Section titled “13. Redis Pub/Sub”What is Pub/Sub?
Section titled “What is Pub/Sub?”Pub/Sub (Publish/Subscribe) is a real-time messaging pattern where:
- Publishers send messages to a channel
- Subscribers listen to channels and receive messages instantly
Publishers and subscribers are completely decoupled — they do not know about each other.
Analogy: Think of a radio station. The station (publisher) broadcasts on FM 98.5 (channel). Anyone tuning into that frequency (subscriber) receives the broadcast in real time.
Core Commands
Section titled “Core Commands”Terminal 1 — Subscriber:
# Subscribe to a channelSUBSCRIBE notifications
# Subscribe to multiple channelsSUBSCRIBE notifications alerts updates
# Subscribe using pattern matchingPSUBSCRIBE news:* # matches: news:tech, news:sports, etc.Terminal 2 — Publisher:
# Publish a message to a channelPUBLISH notifications "New order received: #1042"
# Publisher sees how many subscribers received it# Output: (integer) 1Subscriber Terminal output:
1) "message"2) "notifications"3) "New order received: #1042"Pub/Sub Communication Diagram
Section titled “Pub/Sub Communication Diagram”flowchart TB Pub1["📢 Publisher 1<br/>PUBLISH notifications 'New order!'"] --> Channel["Channel: notificationsMessages broadcast to all subscribers"] Pub2["📢 Publisher 2<br/>PUBLISH alerts 'Server down!'"] --> Channel
Channel --> Sub1["👂 Subscriber 1<br/>SUBSCRIBE notifications alertsReceives: notifications + alerts"] Channel --> Sub2["👂 Subscriber 2<br/>SUBSCRIBE notificationsReceives: notifications only"] Channel --> Sub3["👂 Subscriber 3<br/>PSUBSCRIBE news:*<br/>Receives: news:tech, news:sports, ..."]
style Pub1 fill:#7c3aed,color:#fff style Pub2 fill:#7c3aed,color:#fff style Channel fill:#f59e0b,color:#fff style Sub1 fill:#3b82f6,color:#fff style Sub2 fill:#3b82f6,color:#fff style Sub3 fill:#3b82f6,color:#fffKey insight: Publishers and subscribers are completely decoupled — they have no knowledge of each other. A publisher sends to a channel, and Redis delivers to every subscriber listening on that channel. If no one is subscribed, the message is simply lost.
Real-World Examples
Section titled “Real-World Examples”Real-World Examples
Section titled “Real-World Examples”Chat Application:
# User joins room channelSUBSCRIBE chat:room:general
# User sends a messagePUBLISH chat:room:general '{"user":"Alice","msg":"Hello everyone!"}'Live Notifications:
# Notification service publishesPUBLISH user:notifications:42 '{"type":"order","message":"Your order shipped!"}'
# Frontend WebSocket server subscribed to this channel pushes to browser