Search code examples
rabbitmqamqpreliable-message-delivery

What is the difference between immediate and mandatory flags in amqp/rabbitmq?


I am referring to rabbitmq AMQP-0.9 documentation for basic_publish method. It refers two flags for the message :- 1) Immediate, 2) Mandatory.

mandatory

This flag tells the server how to react if the message cannot be routed to a queue. If this flag is set, the server will return an unroutable message with a Return method. If this flag is zero, the server silently drops the message.

The server SHOULD implement the mandatory flag.

For Immediate:-

immediate

This flag tells the server how to react if the message cannot be routed to a queue consumer immediately. If this flag is set, the server will return an undeliverable message with a Return method. If this flag is zero, the server will queue the message, but with no guarantee that it will ever be consumed.

The server SHOULD implement the immediate flag.

What's the difference between both flags, as both looks same ? Moreover, what is the difference between undelivered and unrouted message, from the perspective of rabbitmq server ?


Solution

  • Based on the documentation extracts you provided:

    mandatory: a message that is sent to an exchange without a matching binding to a destination (mostly queue, you'd need to check further what would happen in the case of bindings between exchange) would be returned to the publisher, so the publisher will know the broker is unable to route the message

    immediate: here it's one step further, the publisher is notified if there is no available consumer for the message.

    So if for example you send a message to an exchange that can direct it to a queue, but there is currently no consumer on the queue:

    • from the mandatory point of view everything is ok (your message has been routed to a queue. it's not "lost"
    • from an immediate point of view it's not ok because at the time of publishing there is no consumer for the message.