When to make an endpoint sync vs async while developing new api? - asynchronous

Creating an GET endpoint that generates upwards of 50,000 records, as this system is communicating with a message broker and where the work is done by another application layer to generate all the data. This process takes on average 30 seconds.
Can GET endpoints be async and when should an endpoint be async ?
My current async implementation for API is based on the Request Reply Pattern .
I have tried looking for other examples on this, found most async API's are POST requests. Cannot confirm if making an GET endpoint async breaks the common conventions.

Related

How Async/Await handle multiple calls at same API end point from multiple user at same time

I am working on an application where I have used async/await for every endpoint. My question here is how async/await handles multiple requests at the same time on the same API endpoint. For example, if I have an endpoint to save a user record and that endpoint has been hit by two different users at the same time, what will happen? Can somebody explain?
Here is the example code here I am registering a restaurant using async/await:
[HttpPost]
[Route("RegisterRestaurant")]
public async Task<IActionResult> RegisterRestaurant([FromBody] RegisterRequestDTO registerModel)
{
var response = await _uow.UserRepository.RegisterRestaurant(new DTO.ResponseDTO.GenericResponseDTO<RegisterRequestDTO> { Data = registerModel });
return Ok(response);
}
Everything is fine with this code. But what will happen if it is hit by multiple users from multiple places at same time?
My question here is how async/await handles multiple requests at the same time on the same API endpoint. For example, if I have an endpoint to save a user record and that endpoint has been hit by two different users at the same time what will happen? Can somebody explain?
This actually has to do with how ASP.NET works, not async/await.
When new requests come in, ASP.NET takes a thread from the thread pool, constructs any necessary instances (e.g., an instance of your controller), and executes the handler for that request. So, each request is independent by default, unless your code explicitly uses static or singleton instances.
async/await do allow threads to return to the ASP.NET thread pool more quickly, but the core mechanism of ASP.NET handling requests is unchanged.
Which user will hit this action method first he will Register the restaurant and mean while second user will wait until the first user await completes.

rest API return result from callback request of another endpoint

I want to standup an endpoint /foo which is a synchronous endpoint for clients but the response is dependent on a callback /foo_callback being called on the app as a result of the request to the synchronous endpoint.
to elaborate the workflow:
Flow diagram
I havent decided on a technology to use so ideally would look for a recommendation.
High level what I am thinking of is starting an async thread in the request handler and check for an update on a singleton map to see if the server has responded but I am wondering if there is a better way
I dont have control over the client and cannot really use websocket or long polling.

Better Event centric way to solve request-reply problem in Kafka or any streaming service

I am stuck in a problem while using Kafka in a microservice architecture . I am not able to understand how a microservice handling HTTP requests will be able to send a response to the user. I want to take data from HTTP and then publish it to topic named A then another validator service will validate it and publish it on another topic named B. I want to send processed data to HTTP response from subscribed data from topic B.
In my experience, one option is to respond immediately with 201 accepted, or embed a blocking validator library into your API, and properly return a 400 Bad Request.
Then future GET calls are required to read eventually consistent data that might come back from any consumer. E.g. the consumer process is Kafka Connector that writes to some database, or is a Kafka Streams/KSQL table, future API queries will return data from that. Your original client may need to make periodic HTTP calls until that data is available.

Async communication between spring boot micro services

I am new to spring boot and created 2 micro services.
They need to communicate with one other in synchronous and asynchronous way.
For synchronous communication, I can use the RestTemplate.
But how to do for asynchronous calling ?
my requirement for Asynchonous is:
lets say I am querying for something from one micro service. To fetch the queried data it will take sometime because of queried for large sum of data.
In this case, I need to save the request into some transaction table and return the response with transactionId and callBackAPI. After sometime if I call the callBackAPI with transactionId. Then I should be able to get the previously queried data.
Please help me with this.
Thanks.
Two solutions :
Async call from your client :
Spring provides an Asynchronous version of the RestTemplate :
AsyncRestTemplate
with this solution, your client is asynchronous, you don't need to store the data in a table with the transaction id and stuff.
Make your endpoint Asynchronous (if you don't need the response) :
Spring lets you create asynchronous methods(services) that you can call from your RestController. With this solution you can do what you described in the question(creating and storing a transaction id that will be returned directly to the client and start the async job).

Track status of multiple async requests when using Spring #Async

I am using spring boot for developing services in my application.
I have a scenario where-in the request submitted to the back-end would take some time to complete.
To avoid waiting the client I want to return the response immediately with a message your request has been accepted. The request would be in progress in a background thread.
I see Spring provides the #Async annotation which can be used to create a separate processing thread from the main thread and using that I am able to offload the processing in a separate thread.
What I want to do is when I return the initial response as accepted I also want to provide the client with a tracking key/token which the client can later use to check the status of the request.
Since there can be multiple clients who would be accessing the service there should be a way of uniquely identifying each client's request from another.
I see there is no such feature in Spring Async or Future which can return a tracking id as such.
One possibility I see it to put the Future returned in HttpSession and later use that to check for the status by the client. But, I prefer not to use HttpSession and want my services to be stateless.
Is there any way/approach I can accomplish my requirement.
Thanks,
BS
Generate the key before calling the Async method, and pass it to the method:
String key = generateUniqueKey();
callAsyncMethod(key);
return key;
The Async method will have to persist the status of the execution somewhere (let's call it dataStore). When the client asks for the status using the key, you look it up on the dataStore and return it.

Resources