Search code examples
ruby-on-railsrubyrabbitmqbunny

RabbitMQ subscription limit the number of messages to pre fetch


I am using rabbitmq to communicate between microservices written in ruby on rails. Each service subscribes to a topic. All services are scaled and run as multiple instances based on need.

During subscription bunny moves all the messages from the queue into unacked state. This makes other scaled instances to be just idle, since there is no message in ready state.

Is there a way to limit the number of messages a subscription can fetch, so that other instances can take the remaining messages from the queue.


Solution

  • Based on the information you made available, I'm assuming you're using rubybunny. If this assumption is incorrect (there are other ruby clients available for rabbitmq) let me know and/or check the documentation related to your client.

    Back to rubybunny, link provided points to necessary information, quoting it:

    For cases when multiple consumers share a queue, it is useful to be able to specify how many messages each consumer can be sent at once before sending the next acknowledgement.

    In AMQP 0.9.1 parlance this is known as QoS or message prefetching. Prefetching is configured on a per-channel basis. To configure prefetching use the Bunny::Channel#prefetch method like so:

    ch1 = connection1.create_channel
    ch1.prefetch(10)