Search code examples
javaspringmicroservices

spring microservices multiple tasks handle


I've a question related to microservices world. Let's assume I've two microservices, microservice A does a request to microservice B. Microservice B perform some "long" operations and makes a request to another microservice C. In result microservice A waits for result from microservice B. How to handle such a long operation if the microservice A is "connected" to front end service, where user initiates the operation and waits for the result of that operation.

Is there any good way to handle long-running tasks in those microservices? What would you suggest to me with that case to be the most efficient and scalable in Spring boot? Thank you in advance.


Solution

  • On a high-level, let's segregate the API calls in two major types

    1. Synchronous
    2. Asynchronous

    Synchronous

    This is an ideal situation implemented using RestTemplate, WebClient , FeignClient etc where in your Microservice A would wait for response from Microservice B.

    Ideal for quick operations and covers majority use case

    Asynchronous

    As @taleodor mentioned, the other way would be to implement an async call to other Microservice, which would only return a Run id or Acknowledgement id to indicate the long running process started. To get status of that process, you may think of implementing it through a Message queue such as ActiveMQ or Apache Kafka which works on the concept of producer/consumer or Publisher/subscriber (pub/sub) where in your long running microservice will publish the event once completed, and Microservice A or B will subscribe to that event and read the response !

    Apart of message queues, your Microservice A can also implement a scheduled task to check for progress completion or resource updation based on how to implement it !