Publish once and every subscriber gets a copy.
Pub/Sub (publish/subscribe) is a way to send a message to many receivers at once. A publisher sends a message to a topic. Every subscriber of that topic gets its own copy.
In Pub/Sub, messages are grouped by topic (for example orders or alerts). Publishers send to a topic. Subscribers say which topics they care about. A broker in the middle delivers the copies.
| Part | Job |
|---|---|
| Publisher | Sends messages to a topic. Does not know the subscribers. |
| Topic | A named channel for one kind of message. |
| Subscription | A subscriberβs βI want this topicβ record. Often has its own backlog. |
| Subscriber | Receives its copy and processes it. |
| Broker | Stores messages and delivers copies (for example Google Pub/Sub, Amazon SNS, Kafka). |
| Style | How it works | Example (general idea) |
|---|---|---|
| Push | The broker calls the subscriber (for example an HTTPS endpoint). | SNS to an endpoint, push subscriptions |
| Pull | The subscriber asks the broker for messages at its own pace. | Pull subscriptions, Kafka consumers |
A common design: publish an event to a topic, and give each subscriber its own queue. Then every service gets a copy and can run many workers on its own queue.
country = IN). This avoids waking up services for messages they do not need.| Term | Meaning in easy words |
|---|---|
| Publisher | The sender of messages |
| Subscriber | A receiver that asked for certain topics |
| Topic | A named channel for one kind of event |
| Subscription | The link between a subscriber and a topic |
| Fan-out | One message copied to many receivers |
| Event | A fact that happened, like βorder placedβ |
| Push / Pull | Broker sends to you / you ask the broker |
| Filter | A rule so a subscriber only gets matching messages |
| Durable subscription | Messages wait for you even when you are offline |
| Event-driven | A design where parts react to events |
| Choice | We gain | We lose |
|---|---|---|
| Pub/Sub instead of direct calls | Loose coupling, easy fan-out | Less visibility, eventual results |
| A queue per subscriber | Each service scales alone | More queues to manage |
| Push delivery | Simple for the subscriber | Subscriber must handle bursts |
| Pull delivery | Subscriber controls its speed | Subscriber must poll |