Pub Sub Messaging
Publish-subscribe (pub/sub) messaging is a pattern in which message publishers send messages to named topics or channels without knowing who will receive them, and subscribers declare interest in specific topics to receive all messages published to them.
Publish-subscribe (pub/sub) messaging is a pattern in which message publishers send messages to named topics or channels without knowing who will receive them, and subscribers declare interest in specific topics to receive all messages published to them.
Pub/sub decouples producers from consumers at the identity level. A publisher addresses a topic, not a service. Any number of subscribers can listen to that topic and each receives an independent copy of every message. Adding or removing subscribers requires no changes to publishers — the system is open for extension without modification.
The matching between publishers and subscribers is managed by a broker or message bus. In RabbitMQ, a fanout exchange implements pure pub/sub. In Kafka, multiple Kafka Consumer Groups reading the same topic achieve pub/sub semantics with replay capability. In Redis, the native PUBLISH/SUBSCRIBE commands (explored in Redis PubSub Flow) offer a lightweight, in-memory option.
The critical distinction from a simple message queue is fan-out: in a queue, a message is consumed by exactly one worker (the Competing Consumers Pattern). In pub/sub, the same message is delivered to every subscriber independently. This makes pub/sub ideal for broadcasting state changes — a price update delivered to all connected clients, or an OrderPlaced event delivered to both the inventory service and the analytics service simultaneously, as shown in Fan Out Messaging.
The trade-off is that subscribers must be active to receive messages in many implementations (Redis pub/sub is entirely in-memory with no persistence). Durable pub/sub — where messages survive subscriber downtime and can be replayed — requires a log-structured broker like Kafka or a managed service like Google Cloud Pub/Sub.