Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consume multiple messages at a time

I am using an external service (Service) to process some particular type of objects. The Service works faster if I send objects in batches of 10. My current architecture is as follows. A producer broadcasts objects one-by-one, and a bunch of consumers pull them (one-by-one) from a queue and send them to The Service. This is obviously suboptimal.

I don't want to modify producer code as it can be used in different cases. I can modify consumer code but only with the cost of additional complexity. I'm also aware of the prefetch_count option but I think it only works on the network level -- the client library (pika) does not allow fetching multiple messages at once in the consumer callback.

So, can RabbitMQ create batches of messages before sending them to consumers? I'm looking for an option like "consume n messages at a time".

like image 384
Eser Aygün Avatar asked May 29 '14 12:05

Eser Aygün


1 Answers

You cannot batch messages in the consumer callback, but you could use a thread safe library and use multiple threads to consume data. The advantage here is that you can fetch five messages on five different threads and combine the data if needed.

As an example you can take a look on how I would implement this using my AMQP library. https://github.com/eandersson/amqpstorm/blob/master/examples/scalable_consumer.py

like image 61
eandersson Avatar answered Sep 16 '22 21:09

eandersson