2

I need to implement the following flow:

Back-end makes HTTP request to Service A, waits for a response from Service A,
then makes HTTP request to Service B, waits for a response,
then makes concurrent HTTP requests to Service C, waits for responses.

if the response from Service A does not contain specific info required for the next request, break the flow and return an empty response.
the same is true for the response from Service B.

Services A, B, and C are different external APIs.

Question: Could you suggest a design pattern for this flow? My goal is to make the project maintainable. In case if tomorrow we'll need to replace Service C with Service D, or add new Service E to the flow.

I'm sure failures are inevitable... So my second question is how to handle network errors which may occur during any of the mentioned requests? Should it be a single try-catch block wrapping the flow, or separate try-catch blocks for each request?

I'm using Java/Kotlin with Spring framework if this is important.


I'll handle response caching, authentication, token refresh, logging, and configuration in separate classes. This question is specifically about the flow design.

  • I feel like I'm missing something because this strikes me as a very odd question. If your code needs to make three calls in sequence, you write a line of code that performs the first call, then below it another line that does the next, etc. And if the first response doesn't contain the information needed to proceed, you check it with an if statement and return the empty response. How else would you do it? – John Wu Mar 28 at 20:10
  • @JohnWu for example, with promises (or CompletableFuture in Java). – naXa Mar 29 at 7:05
  • naXa, correct me if I'm wrong but, using Spring RestTemplate + HttpClient already makes the solution synchronized. You just make one call after the other. Are you perhaps, asking how to make a pipeline? A data flow? – Laiv Apr 1 at 18:40
  • @Laiv you're right. Service A and service B use adapters that depend on Spring RestTemplate. Though Service C is different - it uses an adapter that depends on a library (Spotify Web API). Yes, a pipeline or data flow sounds like what I'm looking for. – naXa Apr 1 at 19:19
  • It might help. Basically, It allows to do what we do in NodeJS. Instead of chaining then one after another, you could resolve the stack recursively. – Laiv Apr 1 at 19:37
0

You can wrap each Service into it's own class, where all the classes implement a common interface. This would allow you to potentially change the service in the future without the client code (i.e the code that organises the flow) knowing which service is being called. The client code would only need to know that these 3 services need to be called in a particular sequence.

Network errors are potentially already handled by Java and would simply result in the request failing altogether.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.