Reputation: 410
Our application consists of microservices. In my side, before deleting an something, it may be used in another microservice . Therefore I need to ask another microservice that the something is used in your side. To do that, I send an event with spring-cloud-event-bus and I need to wait until the response is coming so I cannot return any response tu UI. Also, I can take response in an listener class. How can I forward the message to controler. How can I manage the process.
I have an idea but i think it is not sensible, When a delete request comes, I send an event to another microservice and I am waiting to response. I save the response of other microservice to mongodb with the id of object which will be deleted. By the way, controller is checked the database to getting the response
Upvotes: 3
Views: 2514
Reputation: 17673
You have a bigger problem: one microservice must make a decision based on a piece of data that it doesn't own.
That data is eventually consistent. For example, microservice A would get permission from microservice B to delete something but in the meantime the situation would change on microservice B: you will have deleted something based on expired information.
I suggest to have a look at your design as a whole, maybe you've missed some important concept or have split the monolith in the wrong microservices.
If you still want to keep the current architecture then I suggest to keep a local cache of the remote resource usage. You would interrogate this local cache before deleting the resource without the need to synchronously communicate with the other microservice. This will make your system simpler and also more resilient.
The local cache can be keep up-to-date using events. Whenever the microservice B starts using the resource it would inform microservice A about it; the same when it no longer uses the resource. Using the events is the preferred way. Here you can use your event bus.
Or you could use a background task that runs periodically; this has the advantage that the microservice B remains untouched but it has the disadvantage that there could be a big time-gap between synchronizations.
Upvotes: 4