Side by side
Message QueuevsPub/Sub
What is the difference between a message queue and pub/sub?
Updated 2 min read6 differences
In short
In a message queue each message goes to one consumer, so workers share the load, while pub/sub copies every message to each subscriber so many services react.
Message Queue
A message queue is a component that stores messages from one service until another is ready to process them, so parts of a system can work asynchronously.
Read the page on Message QueuePub/Sub
Publish-Subscribe
Pub/sub is a messaging pattern where publishers send messages to topics and every subscriber to a topic gets a copy, without either side knowing the other.
Read the page on Pub/SubMessage Queue and Pub/Sub compared
| Aspect | Message Queue | Pub/Sub |
|---|---|---|
| Who gets a message | Exactly one consumer | Every subscriber gets a copy |
| Main purpose | Share work between workers | Broadcast events to many services |
| Adding consumers | Spreads the same work over more workers | Adds another service that hears every event |
| When a consumer is offline | Messages wait in the queue | It may miss messages, unless its subscription is durable |
| Coupling | The producer knows which queue does the work | The publisher doesn't know who is listening |
| Examples | RabbitMQ queues, Amazon SQS, job queues like Sidekiq | Kafka topics, Redis Pub/Sub, Amazon SNS, Google Cloud Pub/Sub |
The difference, explained
Both let services talk asynchronously through a broker instead of calling each other directly: a producer sends a message and moves on, and consumers handle it when they can. In a message queue, messages wait in line and each one is taken and processed by a single consumer. In publish/subscribe, a publisher sends a message to a topic, and every subscriber of that topic receives its own copy.
The difference is one receiver versus many. A queue shares work: ten workers reading one queue each take different jobs, such as resizing images or sending emails, and adding workers adds throughput. Pub/sub broadcasts facts: when an order is placed, billing, shipping and analytics can each react without the ordering service knowing they exist.
Real systems often combine the two. With RabbitMQ, Amazon SNS feeding SQS, or Google Cloud Pub/Sub, a topic can fan out to one queue per subscribing service, and that queue is then shared by the service's workers. Kafka covers both with topics and consumer groups: every group sees every message, and within a group each message goes to one member.
A common misconception is that pub/sub always keeps messages until everyone has read them. In classic pub/sub, a subscriber that is offline simply misses messages unless the system adds durable subscriptions or a queue behind each subscriber; in a queue, messages stay until a consumer confirms it has handled them.
Which one should you use?
Choose Message Queue when…
- Each task must be done exactly once, such as sending an email or charging a card.
- You want to spread heavy work across a pool of workers.
- Messages must wait safely until a worker is free.
Choose Pub/Sub when…
- Several independent services need to react to the same event.
- You want to add new consumers without changing the producer.
- You broadcast changes, such as price updates or notifications.
One message: one worker, or every service
// queue stands for a broker client, such as RabbitMQ's
await queue.send("resize-image", { imageId: 7 });
// Three workers run this; each job is handled by only one of them
queue.consume("resize-image", async (job) => {
await resize(job.imageId);
job.ack(); // done: remove it from the queue
});// bus stands for a broker client, such as Google Cloud Pub/Sub's
await bus.publish("order.placed", { orderId: 1001 });
// Every subscriber gets its own copy of each event
bus.subscribe("order.placed", (e) => billing.charge(e.orderId));
bus.subscribe("order.placed", (e) => shipping.prepare(e.orderId));
bus.subscribe("order.placed", (e) => analytics.track(e));Readers ask
Is Kafka a message queue or pub/sub?
Kafka is an event streaming platform that can act as both. Every consumer group reads every message, as in pub/sub, while the members of one group split the messages between them, as in a queue.
Can a message queue have several consumers?
Yes, but they compete: each message still goes to only one of them. That is how a queue spreads work across many workers.
Which one suits microservices?
Both are used. Queues suit commands and background jobs that must run once; pub/sub suits events that several services care about. Many systems use pub/sub topics that feed one queue per service.