24

As far as I can tell, there is no proper use case for a direct exchange, as anything you can do with it you can do with a fanout exchange, only more expandably.

More specifically, in reading RabbitMQ in Action, the authors numerously refer to the use case that goes something like - "Suppose when a user uploads a picture you need to generate a thumbnail. But then later marketing also tells you to award points for uploading a photo. With RabbitMQ you just have to create another queue and do no work on the producer side!"

But that's only true if you've had the foresight to create a fanout exchange on the producer side. To my understanding a direct exchange cannot accomplish this and is only appropriate when you actually want tight coupling between exchange and queue, (which you don't, because that's the point of messaging systems.)

Is this correct or is there an actual use case?

3 Answers 3

28

Compared to the fanout exchange, the direct exchange allows some filtering based on the message's routing key to determine which queue(s) receive(s) the message. With a fanout exchange, there is no such filtering and all messages go to all bound queues.

So if you have a direct exchange with several queues bound with the same routing key, and all messages have this key, then you have the same behavior as the fanout exchange. This is better explained in tutorial 4 on the RabbitMQ website.

In the image upload use case, you can use:

  • a fanout exchange with two queues (one for the thumbnail worker, one for the score computation worker). The routing key is ignored.

    fanout-exchange
    |--> queue --> thumbnail-worker
    `--> queue --> score-worker
    
  • a direct exchange with again two queues. Queues are bound with the image-processing key for instance, and messages with this key will be queued to both queues.

    direct-exchange
    |--["image-processing"]--> queue --> thumbnail-worker
    `--["image-processing"]--> queue --> score-worker
    

    Of course, in this situation, if the message's routing key doesn't match the binding key, none of the queues will receive the message.

You can't put the two workers on the same queue, because messages will be load balanced between them: one worker will see half of the messages.

1
  • 1
    Just getting into rabbitMQ - it seems like if the consuming processes are limited and understood, that direct exchanges are a more readable and explicit option, than having to track down with workers are bound to a flyout exchange. Apr 12, 2018 at 15:00
0

Do you mean a fanout exchange or a topic exchange? a fanout exchange is very different from a direct exchange. I presume that sending the photo to the exchange is sent with a routing key that specifies that there is a photo. In which case you have a consumer that generates the thumbnail and when you want to add a new consumer you can just add it and get the same message but do something different with it, ie award points.

The use case holds up. I think the point is that the exchange is originally created as a direct exchange.

0

This answer echoes the previousone and if you refer to this page, I believe you'll that one particular use case described is:

Direct exchanges are often used to distribute tasks between multiple workers (instances of the same application) in a round robin manner.

2
  • 2
    This sentence in the documentation is confusing. In fact, if you bind several queues with the same routing key to a direct exchange, they will all receive the message if the routing key matches. This is better explained in tutorial 4. Mar 24, 2016 at 10:31
  • @Jean-SébastienPédron - I felt the same confusion as you, however reading the next statement actually removes that confusion - "When doing so, it is important to understand that, in AMQP 0-9-1, messages are load balanced between consumers and not between queues." It essentially means that the load-balancing happens at the consuming ends of queues.
    – bPratik
    Jan 10, 2019 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.