0
0
RabbitMQdevops~5 mins

Fanout exchange (broadcast) in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to send the same message to many receivers at once. A fanout exchange in RabbitMQ lets you broadcast messages to all queues bound to it, so every receiver gets a copy.
When you want to send notifications to multiple services at the same time.
When you have logs that multiple systems need to process independently.
When you want to update many clients with the same data instantly.
When you want to distribute tasks to multiple workers equally.
When you want to broadcast events without caring about routing keys.
Config File - fanout_exchange_setup.sh
fanout_exchange_setup.sh
#!/bin/bash

# Declare a fanout exchange named 'logs'
rabbitmqadmin declare exchange name=logs type=fanout durable=true

# Declare two queues
rabbitmqadmin declare queue name=queue1 durable=true
rabbitmqadmin declare queue name=queue2 durable=true

# Bind queues to the fanout exchange
rabbitmqadmin declare binding source=logs destination=queue1
rabbitmqadmin declare binding source=logs destination=queue2

This script creates a fanout exchange called logs. It then creates two queues named queue1 and queue2. Finally, it binds both queues to the logs exchange so that any message sent to logs will be copied to both queues.

Commands
This command sends a message 'Hello all queues!' to the fanout exchange named 'logs'. Because it's a fanout exchange, the message will be copied to all queues bound to it.
Terminal
rabbitmqadmin publish exchange=logs payload='Hello all queues!'
Expected OutputExpected
Message published
exchange=logs - Specifies the fanout exchange to send the message to
payload='Hello all queues!' - The actual message content to broadcast
This command retrieves the next message from 'queue1' to verify it received the broadcast message.
Terminal
rabbitmqadmin get queue=queue1 requeue=false
Expected OutputExpected
Message 1: { "payload": "Hello all queues!", "payload_bytes": 17, "redelivered": false, "exchange": "logs", "routing_key": "" }
queue=queue1 - Specifies which queue to get the message from
requeue=false - Prevents the message from being put back into the queue after retrieval
This command retrieves the next message from 'queue2' to verify it also received the broadcast message.
Terminal
rabbitmqadmin get queue=queue2 requeue=false
Expected OutputExpected
Message 1: { "payload": "Hello all queues!", "payload_bytes": 17, "redelivered": false, "exchange": "logs", "routing_key": "" }
queue=queue2 - Specifies which queue to get the message from
requeue=false - Prevents the message from being put back into the queue after retrieval
Key Concept

If you remember nothing else from this pattern, remember: a fanout exchange sends every message to all queues bound to it, ignoring routing keys.

Common Mistakes
Trying to use routing keys with a fanout exchange.
Fanout exchanges ignore routing keys, so messages won't be filtered or routed based on them.
Bind the queues to the fanout exchange and send messages without expecting routing key effects.
Not binding queues to the fanout exchange before publishing messages.
If queues are not bound, they won't receive any messages from the exchange.
Always bind your queues to the fanout exchange before sending messages.
Using a direct or topic exchange configuration instead of fanout for broadcast needs.
Direct and topic exchanges route messages based on keys, so not all queues get the message.
Use a fanout exchange when you want to broadcast messages to all bound queues.
Summary
Create a fanout exchange and bind multiple queues to it.
Publish messages to the fanout exchange to broadcast to all bound queues.
Retrieve messages from each queue to verify they all received the broadcast.