I have an API that can be scaled horizontally. The API will acknowledge the client requests and it needs to dispatch the work to a secondary system. The secondary system needs to process the work on a first come first serve basis, a queue. Also since the secondary system is accessing resources that can be shared only one instance of it can be active at a given time. The secondary system needs to have a fail over mechanism. If the 1st instance of the secondary system goes down another instance needs to take its place.
I was thinking of using RabbitMQ as the queuing mechanism and have multiple consumers connect but have only one active consumer that will process the work. One of the other instances will take up the job of processing messages if the previously active consumer fails to acknowledge messages. Is this possible with RabbitMQ ?
Also is it possible to acknowledge the message only after the job has been completed?
Thank you.
This is possible with new release of RabbitMQ: https://www.rabbitmq.com/consumers.html#single-active-consumer
This isn't directly possible with RabbitMQ, the way you are wanting.
You can have multiple consumers for a single queue. However, RabbitMQ will deliver messages to any of the consumers that are available to do work. In other words, if you have 3 consumers and you send 3 messages to the queue, each one of those consumers will likely get 1 of the messages.
If you truly need active / passive failover for the consumers, you will need to use another system to manage and monitor instances of the consumer.
Regarding acknowledgement of the work being done: yes. Put your queue into acknowledgement mode by setting no_ack
to true
. This will require you to acknowledge each message from the consumer. You can hold on to the message until the work is done and then acknowledge the message when the work is complete.
See the worker queue example in the RabbitMQ documentation for an example of using acknowledgements, and for a brief discussion of round-robin message dispatching to consumers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With