Low Orbit Flux Logo 2 F

RabbitMQ How to Acknowledge Message

Messages need to be acknowledged by the consumer once they have been received and processed. This is the confirmation that the message has been delivered and sometimes also processed.

When using RabbitMQ do either of the following to acknowledge a message:

The easiest way to do this is by enabling “auto_ack”. This has the advantage of ensuring that you won’t forget to acknowledge the message which can lead to a build up of unacked messages. In this case it is considered to be acknowledged as soon as it is sent. This is faster but less safe. Messages could be lost. It could also lead to consumers being overloaded.

channel.basic_consume(queue='hello',
                      auto_ack=True,
                      on_message_callback=callback)

You can also call “basic_ack” at the end of the callback function. This has the advantage of allowing the consumer to finish any processing or other work that it needs to do before confirming that everything is OK. This is safer and also makes it easier to not overload a consumer.

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body.decode())
    time.sleep(2)
    print(" [x] Done")
    ch.basic_ack(delivery_tag = method.delivery_tag)

Acknowledgements are sent back to the broker from the consumer. Delivery tags are used to map an ack to a message. Notice the value passed to “basic_ack” in the example above.

Delivery tags have per channel scoping. They should be unique within a channel.

Messages need to be acknowledged on the same channel that they were received. If they are acked on the wrong channel it will result in an error and can cause the channel to be closed.

Another related concept is the idea of publisher confirms. These are confirms from the broker to the publisher confirming that the message was delivered. We aren’t going to cover this here but it is worth being aware of.

References