When to use asynchronous or synchronous methods in controllers (index, search, delete, create) of ASP.NET MVC applications?
The general recommendation of using asynchrony is when the operation takes at least 50ms. Less than that does not compensate. In fact you have to measure your situation, there are cases that even more may not compensate. And this may vary over time with various updates.
Asynchronmentalism is useful to improve user experience when there is some operation that takes a lot of time to execute. So a customer can remain available when he orders something for a long service. It’s not used to make something faster.
If you consider that asynchrony rarely gains in operations that require only processing, without IO, then it’s rarely useful anywhere, unless you set up the HTTP server to not accept too many requests and overload the server, or the IO operations are too long, or at least you don’t have much control of the time it will take.
There will be scale gain in doing some long IO operation on action.
I can lose performance?
Yes, asynchrony is not free. Whenever you use it you will have a loss of performance, especially if you use thread. The gain it gives is concurrent execution, so you can meet more requests. Specific request will not be faster under any circumstances.
So make sure there’s some gain before using this mechanism.
In which situations should I use one or the other, could give examples?
Access to database, file system, various server services that make IO, services external to the server, especially through the internet, finally any IO that is long.
IO asynchronism can release an ability to meet more requests if done properly when there is gain.
If I have a lot of users searching a single async page with a database request, wouldn’t that generate a long queue depending on the application’s run time? How this is managed not to stop others from running?
Each request to the page is independent and this is already a good thing to improve scalability.
The queue will be generated according to the HTTP server configuration. What’s the point of not having the queue on the HTTP server and having it in your application or in the database query?
The use of asynchronous methods prevents Ddos attacks?
There are cases that can facilitate Ddos. There is a question of how to do it in a way that does not cause more problems than solutions.
If your application’s direct client is an HTTP server and the application lets it meet more requests asymptotically, it can jam the application or database and end up stopping everything. The problem is that the HTTP server is your client there and it can ask a lot, unlike a normal user. When the customer can ask for more that the service can meet is an invitation to disaster.
The same goes for any other resource that does IO and is less capable than the HTTP server. This you have to evaluate on a case-by-case basis. Of course there are some ways to scale the IO to meet every demand. You need to assess whether it pays off or whether it’s better to go the simple way. There is no single rule.
Scaling up causes Ddos to take longer or even be avoided in "weak" attacks. But after a certain point it is possible that the scale gain worsens the situation because it transfers the problem to a place where it is more difficult to do throttling.
Additional information
Making the client page (browser) asynchronous is interesting in many situations because it allows the page to continue functioning normally while waiting for a request to return to the server. But you cannot guarantee that the requisition will be made asynchronously. It is always "the law" that you can’t trust the customer that you don’t have full control.
I’m only talking about ASP.NET MVC. There may be other restrictions due to the other technologies involved in the operation.
If you want to dig deeper, there’s a article in MSDN Magazine.
Documentation.
Relacionados: https://answall.com/questions/51268/qual-a-diferent%C3%A7a-entre-comunica%C3%A7%C3%A3o-ass%C3%Adncrona-e-s%C3%Adncrona
– Marconi