CodePark/ questions / RabbitMQ: acks, prefetch & dead-lettering
START PRACTICINGPRACTICE
Backend question · Message queues & streaming › RabbitMQ: acks, prefetch & dead-lettering

What happens with this consumer when a message contains invalid JSON?

channel.consume("orders", async (msg) => {
  try {
    const order = JSON.parse(msg.content.toString());
    await handle(order);
    channel.ack(msg);
  } catch (err) {
    channel.nack(msg, false, true);
  }
});
The message is redelivered over and over in a tight loop, because a parse error will never succeed on retry

The message is redelivered over and over in a tight loop. The third argument true means "requeue". So the bad message goes back to the queue and is delivered again right away. The JSON is still invalid, so it fails again. On a classic queue, this loop never ends and wastes CPU and log space.

"RabbitMQ moves it to a dead-letter queue automatically after the first nack" is wrong. Dead-lettering happens only when requeue is set to
false, or when a quorum queue's delivery limit is reached.

Permanent errors like bad JSON should be nacked without requeue, so they go to a dead-letter exchange.