5

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?

Vadim Samokhin
  • 3,378
  • 4
  • 40
  • 68

4 Answers4

8

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.

matth
  • 6,112
  • 4
  • 37
  • 43
  • 2
    Another way of stating @making3's answer is that the default exchange is a direct exchange to which all queues are implicitly bound, with the queue name as the routing key. When you create a queue, it gets bound to the default exchange using its own name. – nitrogen Sep 19 '14 at 04:16
3

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.

CashIsClay
  • 2,182
  • 18
  • 18
0

As I see it, the default direct exchange give the possibility for the consumers and the producers to not know about each other, by binding a queue (used by a consumer) to an exchange (used by a producer) implicitly using the queue's name.

I use the default direct-exchange for a specific case: the consumer and producers don't know about each other. In my case, each consumer have its proper queue. From the producer, I cannot know by advance which queues are going to be declared and used, as it depends on the consumers. So it is impossible to define the bindings between a custom direct-exchange and the queues on the producer side. One way to solve it with a custom (user-defined) direct-exchange would be to define the binding-key on consumer side. But it would means to know about the producer from the consumer side as I need to know the exchange name used by the producer.

Therefore, automatically binding a queue by its name on the default direct-exchange makes it possible, in my case, to only declare a queue on consumer side and send message to it from producer by only knowing the queue's name.

Of course it implies to know the queue's name at runtime, when invoking the producer, as it would be required to know the binding-key of a custom direct-exchange (in my case, the queue's name is given by the application using the producer). But when configuring the broker, producers and consumers don't have to know about each other.

Francois Gergaud
  • 384
  • 2
  • 11
0

some description in rabbitmq web manager.
//default exchange 's Bindings

  • Default exchange

The default exchange is implicitly bound to every queue,
with a routing key equal to the queue name.

It is not possible to

  • explicitly bind to, or
  • unbind from the default exchange.
  • It also cannot be deleted.
yurenchen
  • 1,897
  • 19
  • 17