wishiwasabigdataguy
wishiwasabigdataguy

Reputation: 147

Asynchronous request/response pattern?

I am fairly new to microservices and asynchronous architectures and I am wondering how typical request/response works in such environments.

Example 1:

  1. Say I have a web app that sends POST request to /example/doStuff and expects a response letting it know if that request was completed successfully or not.
  2. Microservice A's api handling /doStuff has logic that says whenever /doStuff is requested, send a doStuffInitiated event message to the message broker.
  3. As a result of doStuffInitiated event, microservice B and C react to this message by setting a flag in a db, etc, and produce doStuffCompleted message to the broker
  4. This where I am stuck: Is there a way to make microservice A wait for doStuffCompleted to be consumed before sending a response back to the web app that made the original request? Is that even the right way to go about this?

Example 2:

  1. Client facing web app makes GET request to /example/getStuff
  2. Microservice A api handles getStuff by sending stuffRequested event to kafka.
  3. In order to get the data to return a "stuff" object, data is required from 2 external data sources. Microservice B is in charge of handling these 2 data sources, so it consumes the stuffRequested event, gets the data from the 2 data sources, and sends stuffRequestCompleted event (with the "stuff" data in the payload) for microservice A to consume so it can return the "stuff" object to the client app.
  4. This where I am stuck: With all of this going on in the background asynchronously, how can I "suspend" sending back a response until I can consume stuffRequestCompleted and send back the requested data?

Thank you in advance.

Upvotes: 2

Views: 4088

Answers (1)

monad
monad

Reputation: 125

Instead of waiting for the asynchronous part to complete (which I suppose is an antipattern), I would do the A service to response immediatelly - saying the request was received and the proces behind it was started.

Then, you have two options:

  1. in the front-end periodically check (request) status of the operation,
  2. implement server to client communication with websocket.

Upvotes: 4

Related Questions