At one of my clients we had a situation where messages got lost after sending them to RabbitMQ. This is quite bad as the whole point of having a message based solution was to improve the reliability of our solutions(even when of the involved systems is offline or unavailable). In this post I want to explain what got wrong and how we introduced a solution to prevent this from happening in the future.
To understand the problem I first have to explain the concept of an exchange. In RabbitMQ, exchanges are message routing agents that are responsible for routing messages to different queues with the help of header attributes, bindings, and routing keys. A producer never sends a message directly to a queue. Instead, it uses an exchange as a routing mediator. Therefore, the exchange decides if the message goes to one queue, to multiple queues, or is simply discarded.
Let me emphasize one sentence here:
In RabbitMQ , a producer never sends a message directly to a queue
Only queues provide persistence of messages. This means that when a message is send to an exchange and there is no queue linked to that exchange the message is disposed. And that was what was happening in our system, due to a misconfiguration the exchange was not correctly linked to a queue and the messages send to this exchange got lost.
MassTransit follows the default conventions of RabbitMQ. So when you Send or Publish a message and no queue is bound to the exchange, the message is discarded without a warning or error.
If you don’t want this, you can use an overload and specify that a message should mandatory be routed to a destination:
Now you get an exception when no queue is bound to the exchange:
MassTransit.MessageReturnedException : exchange:UnknownMessage => The message was returned by RabbitMQ: 312-NO_ROUTE
Happy coding!