I am wondering why do we need routing key
to route message from exchange
to queue. Can't we use the simply 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, queue name won't be suffice.
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.