Web API 2 - are all REST requests asynchronous?

Do I need to do anything to make all requests asynchronous or are they automatically processed this way?

I have done some tests, and it seems that each request comes in its own flow, but I think it's better to ask, as I may have been mistaken.

Update: (I have a bad habit of not explaining completely - sorry) Here is my concern. The client browser makes a REST request to my server http: //data.domain/com/employee_database/? Query = state: Colorado . This matches the corresponding method in the controller. This method queries the database and returns an object, which then turns into a JSON structure and returns to the calling application.

Now let's say that 10,000 clients perform a similar request to the same server. So, I immediately have 10,000 requests. Will my controller method be called simultaneously in 10,000 separate threads? Or should the first request return before calling the second request?

I am not asking about the code in my handler method with asynchronous components. For my case, the query becomes the only SQL query, so the code has nothing that can be processed asynchronously. And until I get the requested data, I cannot return from the method.

+4
source share
4 answers

Honestly, your question is not very clear. If you create an HTTP GET using the HttpClient , say the GetAsync method, the request starts and you can do whatever you want in your thread until you get a response. Therefore, this request is asynchronous. If you ask about the server side that processes this request (assuming it is an ASP.NET Web API), then it is asynchronous or independent of how you implemented your web API. If your action method does three things, say 1, 2, and 3, one after the other, synchronously in blocking mode, the same thread sends a request to the service. On the other hand, let's say that # 2 above is a web service call, and this is an HTTP call. Now, if you use HttpClient and you make an asynchronous call, you may find yourself in a situation where one request is served by more than one thread. For this to happen, you had to make an HTTP call from your action method asynchronously and use the async . In this case, when you call await inside the action method, the action of your action is executed, and the thread serving your request can serve some other request, and ultimately, when the response is available, the same or some other thread will continue from where he was stopped earlier. A long boring answer, perhaps, but difficult to explain simply with words, typing, I think. Hope you get some clarity.

UPDATE: Your action method will execute in parallel in 10,000 threads (ideally). Why do I say ideally because the CLR thread pool with 10,000 threads is not typical and probably impractical. There are physical limitations, as well as restrictions imposed by the structure, but I think the answer to your question is that the requests will be served in parallel. The correct term here will be "parallel," but not "asynchronous."

+1
source

I think you should use async IO / operations such as database calls in your case. Yes in Web Api, each request has its own thread, but threads can end if there are many consecutive requests. Threads also use memory, so if your api gets into too many requests, it can put pressure on your system.

The advantage of using asynchronous synchronization is that you use your system resources wisely. Instead of blocking the thread when it waits for the database call to complete in the synchronization implementation, async will free the thread to handle more requests or assign it what the process needs for the thread. As soon as the IO (database) call completes, another thread will take it from there and continue implementation. Async will also make your api faster if your I / O operations take longer.

+1
source

Will it be synchronization or asynchronous selection. You choose to write your action. If you return the task and also use async IO under the hood, it will be asynchronous. In other cases, it is synchronous.

Do not be tempted to hit async on your action and use Task.Run . This is asynchronous synchronization (known anti-pattern). It should be truly asynchronous right down to the kernel of the OS.

No infrastructure can make IO synchronization automatically asynchronous, so it cannot happen under the hood. Async IO is based on a callback, which is a major change in the programming model.

This will not answer what you should do, of course. This will be a new question.

0
source

By default, there is no async for REST. the request is processed synchronously. However, your web server (IIS) has several parameters for maximum flows that can work at the same time, and it supports the queue of the received request. Thus, the request is sent to the queue, and if the stream is available, it starts differently, the request waits in the IIS queue until the stream is available

0
source

Source: https://habr.com/ru/post/1500921/


All Articles