Skip to content
BENIBIX Learn

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.

Development🏗️ Architecture

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.

Example
Jack's site lets people upload videos. Each upload adds a message to a queue. Four workers take messages out and convert the videos, while the site stays fast.
Why it matters
Slow jobs move out of the web request, so users are not kept waiting. Spikes of traffic just make the line longer.
Full entry
Development🏗️ Architecture

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.

Example
Lucy's app publishes user signed up to a topic. The email service, the stats service and the welcome gift service each get a copy and act on it.
Why it matters
One event can start many jobs. New listeners can join without a change to the sender.
Full entry

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.