How to set the correct prefetch value in LavinMQ
Guide

How to set the correct prefetch value in LavinMQ


Written by: Lovisa Johansson


Prefetch controls how many messages a consumer can receive before acknowledging them. The optimal value depends on factors like latency, processing time, and consumer count. A good starting point is:

Prefetch = (Round-trip latency / Processing time) + 1

Too low, and consumers stay idle. Too high, and messages pile up at slow consumers. This blog provides examples of how to set the optimal prefetch value based on different setups and workload conditions.

LavinMQ prefetch

Optimizing the prefetch value involves balancing consumers, broker load, and system latency. A higher prefetch count can speed up message delivery, allowing consumers to process messages more quickly. This reduces the need for frequent acknowledgment checks and minimizes communication between the broker and consumers.

If only one consumer is active, a large prefetch count could send many messages to that single consumer, leaving the other consumers idle until they come online (as shown in the figure).

LavinMQ prefetch

On the other hand, a smaller prefetch value ensures more balanced message consumption, preventing consumers from being overwhelmed. It’s especially ideal for distributing messages evenly across large systems. A prefetch value of one helps maintain equal distribution. However, setting the prefetch count too low can negatively impact performance, as the broker might wait for acknowledgment before delivering more messages. In the example below, LavinMQ won’t send the following message until the round trip is completed (delivery, processing, acknowledgment).

LavinMQ prefetch

The total round-trip time is 125ms, with only 5ms spent processing the message. A recommended prefetch value would be around 6:

Prefetch = (125ms / 5ms) + 1 = 26

Example: Setting the Prefetch value in LavinMQ

Configure the basic_qos to apply a prefetch limit. The example below sets a prefetch count of 3, meaning consumers can process up to three unacknowledged messages before receiving more.

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

channel.basic_qos(prefetch_count=3, global=False)

LavinMQ prefetch

Summary

The following recommendations can help determine the best prefetch value for different scenarios:

  • Few consumers, fast processing: Set a high prefetch count to keep the consumer busy. If processing times and network conditions remain consistent, divide the total round-trip time by the processing time per message to estimate the optimal prefetch value.
  • Many consumers, fast processing: Set a lower prefetch value to avoid leaving consumers idle while they wait for messages.
  • Many consumers or long processing times: Set the prefetch count to 1 to ensure even message distribution across all consumers.

Read more about prefetch in the LavinMQ Prefetch documentation.