Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ exchanges: default vs. direct

Tags:

rabbitmq

I have exactly 2 types of messages that I want to be sent via RabbitMQ. So I have 2 options how I can do this:

  • sent a message to default empty-named exchange with routing_key corresponding to the queue name
  • use direct exchange's routing_key parameter corresponding to consumer's routing_key parameter in queue binding

So which option is preferable and why?

like image 478
Vadim Samokhin Avatar asked Feb 18 '23 18:02

Vadim Samokhin


2 Answers

A default exchange is a direct exchange. RabbitMQ creates the default exchange by default, but it uses an empty string for the name. If you look at the RabbitMQ AMQP concepts page, under Default Exchange:

The default exchange is a direct exchange with no name (empty string) pre-declared by the broker.

You can see this by running rabbitmqctl list_exchanges as well:

                direct
Foo             direct    < Same thing as the above
amq.direct      direct
amq.fanout      fanout
...and so on

As far as I'm aware, there isn't any benefits of using one over the other. I would stick with the default exchange if you only need to route based on routing keys.

like image 111
matth Avatar answered Mar 18 '23 09:03

matth


Let's say you direct-bind to an exchange broadcasting logs to routing keys of "info", "warn", and "error". Using the default exchange, you would need to create three different queues with those names to receive all logs; and adjustments to which log levels you receive would require changing your queue declarations. By using a named exchange, you can simply change your queue's bindings and continue processing things as normal.

In short, it provides one extra level of abstraction.

like image 26
CashIsClay Avatar answered Mar 18 '23 10:03

CashIsClay