What is the publish/subscribe pattern in RabbitMQ?
Learn how RabbitMQ publish/subscribe works with fanout exchanges to broadcast one message to many consumers, with code, analogies and interview tips.
Expected Interview Answer
The publish/subscribe pattern in RabbitMQ broadcasts a single message to many consumers at once by sending it to a fanout exchange, which copies the message to every queue bound to it.
Publishers never send directly to queues; they publish to an exchange. A fanout exchange ignores routing keys and delivers a copy of each message to all bound queues, so every consumer with its own queue receives every message. This decouples producers from consumers — the publisher does not know or care how many subscribers exist, and subscribers can come and go without the publisher changing.
- One message reaches many independent consumers
- Producers and consumers are fully decoupled
- Easy to add new subscribers without changing publishers
- Each consumer processes at its own pace via its own queue
- Ideal for event broadcasting and notifications
AI Mentor Explanation
A stadium announcer calling a wicket over the public-address system is publish/subscribe: the announcer speaks once into the mic, and every stand — members, families, away fans — hears the same call simultaneously. The announcer never addresses anyone individually; the PA system fans the message out to all sections at once, exactly as a fanout exchange copies one publish to every bound queue.
Step-by-Step Explanation
Step 1
Declare a fanout exchange
Create an exchange of type 'fanout' that will copy every message it receives to all bound queues.
Step 2
Each subscriber declares a queue
Every consumer creates its own queue, often exclusive and auto-delete so it disappears when the consumer leaves.
Step 3
Bind queues to the exchange
Bind each queue to the fanout exchange; routing keys are ignored for this exchange type.
Step 4
Publisher publishes to the exchange
The producer publishes messages to the exchange name, never to a queue directly.
Step 5
Consumers receive copies
RabbitMQ places a copy of each message into every bound queue so all subscribers process it independently.
What Interviewer Expects
- Knowing publishers send to exchanges, not queues
- Understanding fanout exchange behaviour
- Explaining producer/consumer decoupling
- Awareness that each subscriber needs its own queue
- A concrete broadcast use case such as notifications
Common Mistakes
- Saying the publisher sends directly to consumers or queues
- Confusing fanout with direct or topic routing keys
- Assuming all consumers share one queue in pub/sub
- Forgetting that copies are independent per queue
- Thinking the publisher must know its subscribers
Best Answer (HR Friendly)
“Publish/subscribe in RabbitMQ is like a broadcast: one message is sent to a fanout exchange, which copies it to everyone who is listening. The sender doesn't need to know who the receivers are, so you can add or remove listeners freely.”
Code Example
const ch = await conn.createChannel()
await ch.assertExchange('logs', 'fanout', { durable: false })
ch.publish('logs', '', Buffer.from('user.signed_up'))
console.log('Broadcast sent')const ch = await conn.createChannel()
await ch.assertExchange('logs', 'fanout', { durable: false })
const q = await ch.assertQueue('', { exclusive: true })
await ch.bindQueue(q.queue, 'logs', '')
ch.consume(q.queue, (msg) => console.log('Got', msg.content.toString()), { noAck: true })Follow-up Questions
- How does a fanout exchange differ from a direct exchange?
- What happens to messages if no queue is bound to the exchange?
- How would you scale one subscriber into a competing-consumers group?
- Why use exclusive auto-delete queues for subscribers?
- How does topic routing extend the pub/sub model?
MCQ Practice
1. Which exchange type implements classic publish/subscribe in RabbitMQ?
A fanout exchange ignores routing keys and copies each message to every bound queue, giving broadcast pub/sub behaviour.
2. In pub/sub, where does a producer send messages?
Producers always publish to an exchange; the exchange decides which queues receive copies.
3. How do multiple subscribers each receive their own copy of a message?
Each subscriber declares its own queue and binds it to the exchange, so the exchange delivers an independent copy to each.
Flash Cards
What exchange type gives pub/sub? — A fanout exchange, which copies every message to all bound queues.
Where do publishers send in RabbitMQ? — To an exchange, never directly to a queue.
How many copies does a fanout exchange make? — One per bound queue — each subscriber gets an independent copy.
Does a fanout exchange use routing keys? — No, it ignores the routing key and broadcasts to all bound queues.