What is the difference between a message queue and a publish-subscribe system?
MediumConceptMessage Queues & Event Streaming
Answer
In a point-to-point queue, each message is processed by only one of several competing consumers, which spreads work across workers. In publish-subscribe, each message published to a topic is delivered to every subscriber, so several independent services can react to the same event.
- Queue example: image-resizing jobs shared among a pool of workers.
- Pub-sub example: an
OrderPlacedevent consumed separately by the billing, inventory and email services. - Kafka combines both: every consumer group receives all messages on a topic (pub-sub), while consumers within a group divide the partitions between them (queue).
- Pub-sub adds fan-out and loose coupling, but it becomes harder to trace which services act on an event and in what order.
More in Message Queues & Event Streaming
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is a message queue, and why would you use one? | Concept | Easy | Read the answer to What is a message queue, and why would you use one? | |||
| 2 | What is the difference between a message queue and a publish-subscribe system? | Concept | Medium | Read the answer to What is the difference between a message queue and a publish-subscribe system? | |||
| 3 | What do at-most-once, at-least-once and exactly-once delivery mean? | Concept | Medium | Read the answer to What do at-most-once, at-least-once and exactly-once delivery mean? | |||
| 4 | How does Kafka achieve high throughput, and how does it preserve message ordering? | Concept | Hard | Read the answer to How does Kafka achieve high throughput, and how does it preserve message ordering? |