Home > Enterprise >  REST API Design - Async REST Client Vs Async REST API
REST API Design - Async REST Client Vs Async REST API

Time:12-11

I already have REST API (for System-to-System communication) which takes lot of time to process.

I want to have asynchronous processing. I see two options here:

  1. To make the API itself as asynchronous, where it returns a LOCATION header giving another URI to fetch result.

  2. To make the client asynchronous - using asynchronous HTTP Client or AsyncRestTemplate etc.

I was wondering what is better way in such scenarios, as both seems to solve the issue.

CodePudding user response:

There are some pros and cons to each of the options you mentioned that you may want to consider when making a decision.

Asynchronous API

This approach has a lot of benefits, such as allowing the API to process requests in parallel and improving the overall performance and scalability of the system. However, this approach can also add some complexity to the API, as it requires the API to implement asynchronous processing and provide a mechanism for clients to fetch the result of a request.

Asynchronous client

This approach can provide a simpler and more straightforward solution, as it does not require any changes to the API itself. This approach can also improve the performance and scalability of the system, as it allows the client to process multiple requests in parallel and handle responses asynchronously. However, this approach may require the client to implement additional logic to handle asynchronous processing and fetching results, which can add some complexity to the client.

Summary

Making the API asynchronous can provide better performance and scalability, but may require more complex implementation, while making the client asynchronous can provide a simpler solution, but may not provide the same level of performance and scalability. You will need to weigh the pros and cons of each approach and decide which is best for your specific system based on your requirements and constraints.

CodePudding user response:

In general, it is better to make the API itself asynchronous, rather than relying on the client to be asynchronous. This is because making the API itself asynchronous allows for greater flexibility in how the API is used. For example, a client could choose to use a synchronous HTTP client to make the request, and still benefit from the asynchronous processing on the server-side.

CodePudding user response:

It depends on your specific use case and requirements.

If you have a lot of requests coming in from multiple clients, making the API asynchronous may be the best option as it allows you to scale better and process requests in parallel.

On the other hand, if your API is already built and you are just looking to improve performance of the requests, using an asynchronous client may be the best option as it will allow the requests to be sent in parallel and processed faster.

  • Related