'Rabbitmq Sending message to only one service
I am building my application on a microservice model.
Lets says
- There is one database on server D1
- There is a user service hosted on server U1
- There is a notification service on server N1
- There is a rabbitmq service on server R1
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?
Solution 1:[1]
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."
- Register U1 and U2 on the same queue
- U1 will be registered as Single Active Consumer
- If U1 dies/canceled, it will be replaced by U2 as Single Active Consumer, and vice versa
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Brando J |