You might have noticed that the dispatching still doesn't work exactly as we want. For example in a situation with two workers, when all odd messages are heavy and even messages are light, one worker will be constantly busy and the other one will do hardly any work. Well, RabbitMQ doesn't know anything about that and will still dispatch messages evenly.
This happens because RabbitMQ just dispatches a message when the message enters the queue. It doesn't look at the number of unacknowledged messages for a consumer. It just blindly dispatches every n-th message to the n-th consumer.
In order to defeat that we can use the basic.qos
method with the
prefetch_count=1 setting. This tells RabbitMQ not to give more than one message
to a worker at a time. Or, in other words, don't dispatch a new message to a
worker until it has processed and acknowledged the previous one. Instead, it
will dispatch it to the next worker that is not still busy:
(qos (make-basic.qos prefetch_count 1))
If all the workers are busy, your queue can fill up. You will want to keep an eye on that, and maybe add more workers, or have some other strategy.