Message Queue vs Pub/Sub
A message queue and pub/sub both pass messages between parts of a system, but they hand them out in two different ways. In a queue, each message goes to just one worker. So a job is done once. In pub/sub, every subscriber gets its own copy. So one event can start many jobs. Use a queue for work and pub/sub for news.
Message Queue
A line where messages wait for a free worker.
A message queue holds messages until a worker is ready for them. They come from one part of a system and go to another. The sender, or producer, drops a message in and moves on. A worker, or consumer, takes messages out one at a time and does the work. Each message is usually handled by just one worker. If workers are busy or down, messages wait safely in the queue.
Publish-Subscribe (Pub/Sub)
Send a message to a topic. All who listen get a copy.
Pub/sub is a way to send messages so that every listener gets a copy. Its full name is publish-subscribe. A publisher sends a message to a named topic. Any number of subscribers listen to that topic. Each of them gets its own copy of every message. The publisher does not know who, or how many, are listening.
In one line
- Message Queue
- A line where messages wait for a free worker.
- Publish-Subscribe (Pub/Sub)
- Send a message to a topic. All who listen get a copy.