Why do we need routing key in RabbitMQ?
Asked Answered
K

2

33

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?

Kilby answered 30/3, 2016 at 7:30 Comment(2)
The routing-key is a not optional for AMQP basic.publish. So the trite answer is that "AMQP requires it".Jylland
It is similar to asking why can not we use localhost addresses to host out websites on internet. The routing key is required for when you are testing some application which uses RabbitMQ for passing the messages. And the environment is a cluster environment where exchange contains other applications also. There you can not use Fanout Exchange (for sending the same message to all the queues) or Topic Exchange(matching with binding-key.) You have a single publisher(test publisher) and one or 2 subscribers (test subscriber(s)).Enyo
E
11

Decoupling queue names from applications is useful for flexibility.

  • You could establish multiple queues to consume the same message, but queues can't have the same name.

  • In some cases, message's originator doesn't know the names of queues. (like when you have randomly generated queue names when horizontally scaling a server)

  • An exchange may be routing messages for more than just one type of consumer. Then you would need some wildcards in your routing keys to route messages to concerned consumers.

Evensong answered 10/1, 2019 at 11:0 Comment(0)
K
24

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.

Kinkajou answered 30/3, 2016 at 8:55 Comment(3)
ok. Why do we need routing key in between exchange and queue. We can use fan out exchange to send the messsages to all the queues. Similarly if we want to send to a particular or multiple queues. Can't we use the name of queue itself. Why do we need special "routing key" conceptKilby
You can put the queue name in the routing key and publish to the "" default exchange (an echange named with the empty string): your message will end up in the queue you want. Routing keys and different types of exchanges give more flexibility in how you can route messages to queues. It also allows you to further decouple publishers and consumers. One more advantage: it moves the routing knowledge from the publisher to the bus where it belongs.Cooky
RMQ web interface lets you publish to queues, so I guess it is possible, but seldom used anyway.Evensong
E
11

Decoupling queue names from applications is useful for flexibility.

  • You could establish multiple queues to consume the same message, but queues can't have the same name.

  • In some cases, message's originator doesn't know the names of queues. (like when you have randomly generated queue names when horizontally scaling a server)

  • An exchange may be routing messages for more than just one type of consumer. Then you would need some wildcards in your routing keys to route messages to concerned consumers.

Evensong answered 10/1, 2019 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.