Reputation: 2249
I am building my application on a microservice model.
Lets says
The notification service publishes a message to the rabbitmq service, which then dispatches the message to the user service, the user service that listens to the rabbitmq service, receives the message and writes some data to the database.
This works perfectly!
The problem is if I scale the user service to another server say U2, meaning the user service is now running on both servers i.e. U1 and U2 (backed by an nginx load balancer) and both the user service now listening to the rabbitmq service, will receive the message dispatched from the notification service and both will write to the database (which I don't want). I want only one of the user service i.e. either U1 or U2 to write the data to the database.
So, i was wondering if rabbitmq can dispatch the message to any one user service instead of both? or am I using a wrong approach altogether?
Upvotes: 0
Views: 408
Reputation: 31
I think you can use Single Active Consumer option
"Single active consumer allows to have only one consumer at a time consuming from a queue and to fail over to another registered consumer in case the active one is cancelled or dies."
To use this option pass x-single-active-consumer
argument when declaring the queue
Please read the documentation for more information.
Reference: RabbitMQ Consumers Single Active Consumer
Upvotes: 1