Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ same message to each consumer

Tags:

rabbitmq

I have implemented the example from the RabbitMQ website: RabbitMQ Example

I have expanded it to have an application with a button to send a message. Now I started two consumer on two different computers. When I send the message the first message is sent to computer1, then the second message is sent to computer2, the thrid to computer1 and so on.

Why is this, and how can I change the behavior to send each message to each consumer?

like image 939
GreenEyedAndy Avatar asked Dec 15 '16 09:12

GreenEyedAndy


1 Answers

Why is this

As noted by Yazan, messages are consumed from a single queue in a round-robin manner. The behavior your are seeing is by design, making it easy to scale up the number of consumers for a given queue.

how can I change the behavior to send each message to each consumer?

To have each consumer receive the same message, you need to create a queue for each consumer and deliver the same message to each queue.

The easiest way to do this is to use a fanout exchange. This will send every message to every queue that is bound to the exchange, completely ignoring the routing key.

If you need more control over the routing, you can use a topic or direct exchange and manage the routing keys.

Whatever type of exchange you choose, though, you will need to have a queue per consumer and have each message routed to each queue.

like image 189
Derick Bailey Avatar answered Dec 23 '22 02:12

Derick Bailey