Pub/Sub Pattern
TL;DR
Pub/Sub (Publish-Subscribe) decouples senders from receivers — publishers emit events to topics, all interested subscribers receive them without knowing each other.
Explanation
Publisher sends to a Topic/Channel — it doesn't know who's listening. Subscribers declare interest in topics. Broker delivers to all active subscribers. Implementations: Redis PUBLISH/SUBSCRIBE, Kafka topics, RabbitMQ fanout exchange, SNS+SQS (AWS), Google Pub/Sub. Difference from point-to-point queue: queue delivers to ONE consumer; pub/sub delivers to ALL subscribers. Use cases: event broadcasting (user registered → email + analytics + audit + recommendation all receive it), real-time feeds (dashboard updates). Fan-out: one event → many independent handlers. Redis pub/sub doesn't persist — subscribers miss messages while offline; use Redis Streams for durability.
Common Misconception
✗ Pub/Sub and message queues are the same — pub/sub broadcasts to all subscribers; queues deliver to one consumer. Redis SUBSCRIBE delivers to active subscribers only; Kafka delivers to all consumer groups including future ones.
Why It Matters
Pub/Sub enables truly decoupled event-driven architecture — a new subscriber can be added without changing the publisher, enabling independent feature development.
Common Mistakes
- Using Redis SUBSCRIBE for durable events — messages are lost if subscriber is offline.
- Publishing events that are too granular — hundreds of tiny events create consumer overhead.
- Not considering message ordering when multiple publishers write to the same topic.
Code Examples
✗ Vulnerable
// Redis SUBSCRIBE — volatile, lost if offline:
$redis->subscribe(['user.registered'], function($redis, $channel, $msg) {
sendWelcomeEmail($msg); // Missed if worker was down during publish
});
✓ Fixed
// Redis Streams — durable pub/sub:
$redis->xadd('user.events', '*', ['event' => 'registered', 'user_id' => $id]);
// Consumer group reads with acknowledgement:
$msgs = $redis->xreadgroup('email-group', 'worker-1', ['user.events' => '>']);
foreach ($msgs['user.events'] as $id => $fields) {
sendWelcomeEmail($fields['user_id']);
$redis->xack('user.events', 'email-group', $id);
}
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
28
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 7
Amazonbot 6
Unknown AI 3
Google 3
SEMrush 2
ChatGPT 1
Ahrefs 1
Also referenced
How they use it
crawler 21
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: Medium
⚡ Quick Fix
Use Redis Streams (not SUBSCRIBE) for durable pub/sub. Use Kafka for high-throughput multi-subscriber events. Design events to be self-contained and idempotent.
📦 Applies To
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
publish|subscribe|dispatch
Auto-detectable:
✗ No
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Medium
Context: File