41

I'm using rabbitMQ, I take every message from queue with basic_get without automatically acking procedure, which means the message remain in queue until I ack or nack the message.

Sometimes I've messages that can't be processed because of some exception thrown, which prevented them from being fully processed.

Question is what does it matter if I both ack the messages in success and exception thrown, I mean in terms of result messages will always get out of the queue, so what does it matter if I use ack or nack in this scenario? Maybe I miss something about when using each opration?

Roman Cheplyaka
  • 37,738
  • 7
  • 72
  • 121
JavaSa
  • 5,813
  • 16
  • 71
  • 121

2 Answers2

95

The basic.nack command is apparently a RabbitMQ extension, which extends the functionality of basic.reject to include a bulk processing mode. Both include a "bit" (i.e. boolean) flag of requeue, so you actually have several choices:

  • nack/reject with requeue=1: the message will be returned to the queue it came from as though it were a new message; this might be useful in case of a temporary failure on the consumer side
  • nack/reject with requeue=0 and a configured Dead Letter Exchange (DLX), will publish the message to that exchange, allowing it to be picked up by another queue
  • nack/reject with requeue=0 and no DLX will simply discard the message
  • ack will remove the message from the queue even if a DLX is configured

If you have no DLX configured, always using ack will be the same as nack/reject with requeue=0; however, using the logically correct function from the start will give you more flexibility to configure things differently later.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Spectacular answer! However can you clarify a bit if the DLX is already considered as a queue.,and what the difference between queue and exchange? – JavaSa Mar 03 '15 at 09:25
  • 1
    @JavaSa One common analogy (which may or may not make sense to you) is that an exchange is like a Post Office, and a queue is like a PO Box. You only ever publish a new message to an exchange (even if it's the default exchange); the exchange will then route it to zero, one, or multiple queues, generally based on its "routing key". So the simplest DLX would be one which routed everything into a single queue, but it could be as complex as you needed. – IMSoP Mar 03 '15 at 10:08
  • `nack` stands for negative acknowledgement. – cs_pupil Apr 05 '22 at 20:17
6

Ack and Nack both remove the message from the queue, the difference is that when you Nack a message it goes into the DLX (dead letter queue) if there is one defined for that queue.

jhilden
  • 12,207
  • 5
  • 53
  • 76
  • 2
    Not if you use Nack with the requeue option set to true. Setting it to true will remove the message from the consumer's fetch and added it back to the original queue for reprocessing. – code5 Mar 30 '22 at 16:23