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

What does prefetch (basic.qos) do in RabbitMQ, and how do you choose a value?

Prefetch limits how many unacknowledged messages the broker pushes to a consumer at once. An unacknowledged (unacked) message is one the consumer has received but not yet confirmed as done.

With prefetch 10, the consumer holds at most 10 unacked messages. The broker sends the 11th only after the consumer acks one. Prefetch has no effect with automatic acks.

Without a limit, the broker sends everything it can. This causes problems:

  • One consumer can grab thousands of messages and keep them in memory.
  • Other consumers sit idle with nothing to do.
  • If that consumer crashes, all its messages return to the queue at once.

Choose the value from the kind of work. Start with about the number of messages the consumer processes at the same time. Add a little extra to cover network round trips.

  • Slow, heavy jobs need a low value (1 to 10), so work is shared fairly between consumers.
  • Fast, light messages work better with higher values (tens to a few hundred), for more throughput.

Measure to find the best value. Note that in amqplib, channel.prefetch(n) applies per consumer by default.

  • Prefetch caps how many unacked messages each consumer can hold at once.
  • With no limit, one consumer can grab too many messages and use too much memory.
  • Use a low prefetch for slow jobs and a higher one for fast jobs.
  • Set it to about the consumer's concurrency, plus a little extra.
  • Prefetch is ignored with auto-ack.