Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need routing key in RabbitMQ?

Why do we need routing key to route messages from exchange to queue? Can't we simply use the queue name to route the message? Also, in case of publishing to multiple queues, we can use multiple queue names. Can anyone point out the scenario where we actually need routing key and queue name won't be suffice?

like image 925
Naresh Avatar asked Mar 30 '16 07:03

Naresh


People also ask

What is binding key in RabbitMQ?

The binding-key is used with the queue. It is the key with which the queue is registered in the exchange. The routing-key is used with the message. It is the key which will decide which queue(s) does this message should be routed to.

Why do we need Exchange in RabbitMQ?

In RabbitMQ, a producer never sends a message directly to a queue. Instead, it uses an exchange as a routing mediator. Therefore, the exchange decides if the message goes to one queue, to multiple queues, or is simply discarded.

How do you bind Exchange to queue in RabbitMQ?

RabbitMQ Bind Queue to Exchange To bind a queue to exchange, Go to Queues tab and then click on the queue (demoqueue) which is created like as shown below.

What is fanout in RabbitMQ?

A fanout exchange routes messages to all of the queues that are bound to it and the routing key is ignored. If N queues are bound to a fanout exchange, when a new message is published to that exchange a copy of the message is delivered to all N queues. Fanout exchanges are ideal for the broadcast routing of messages.


1 Answers

There are several types of exchanges. The fanout exchange ignores the routing key and sends messages to all queues. But pretty much all other exchange types use the routing key to determine which queue, if any, will receive a message.

The tutorials on the RabbitMQ website describes several usecases where different exchange types are useful and where the routing key is relevant.

For instance, tutorial 5 demonstrates how to use a topic exchange to route log messages to different queues depending on the log level of each message.

If you want to target multiple queues, you need to bind them to a fanout exchange and use that exchange in your publisher.

You can't specify multiple queue names in your publisher. In AMQP, you do not publish a message to queues, you publish a message to an exchange. It's the exchange responsability to determine the relevant queues. It's possible that a message is routed to no queue at all and just dropped.

like image 132
Jean-Sébastien Pédron Avatar answered Sep 23 '22 19:09

Jean-Sébastien Pédron