0

I am developing an Asp Net MVC client that will use Asp Net Web Api. And I have to decide on how to better design my application. After searching the web for a while I found people suggesting (for example, here) to make MVC action methods asynchronous. I also found out that the main benefit of having asynchronous action methods is scalability of the server, i.e., the server will be able to serve more requests. There is one thing though, the web api I'm consuming has synchronous action methods, and will run on the same server. So my guess is there are no benefits for me to implement asynchronous action methods for my MVC, because even if my MVC action methods are asynchronous and the server will be able to scale from the "MVC point of view" in the end these methods will still consume synchronous Web Api methods, and because of this, the server will "inevitably" run out of its thread pool. Maybe I'm missing something or there are some other benefits of asynchronous action methods?

Here is a very simple sample code I wrote to make you better understand my issue:

This is the web api controller:

public class UsersController : ApiController
{
    private readonly IUserService _userService;
    public UsersController(IUserService userService)
    {
        _userService = userService;
    }

    // As you can see this method is not asynchronous
    public User Get(int id)
    {
        return _userService.GetUserById(id);
    }
    // some other code
}

This is the Mvc controller, I have two choices how to design my action methods:

public class UsersController : Controller
{
    // A) Make simple synchronous action methods 
    public ActionResult UserPageSync()
    {
        IUserWebServiceSync userWebServiceSync = new UserWebServiceSync();
        User user = userWebServiceSync.GetUserById(1);

        return View();
    }

    // B) Make asynchronous action methods 
    public async Task<ActionResult> UserPageAsync()
    {
        IUserWebServiceAsync userWebServiceAsync = new UserWebServiceAsync();
        User user = await userWebServiceAsync.GetUserByIdAsync(1);

        return View();
    }
}
Mykhailo Seniutovych
  • 3,527
  • 4
  • 28
  • 50
  • 1
    Having async and sync methods is fine. I'd wait until you have a problem with the performance of your application before you worry about it. Having lots of async methods means that when, for instance, your application goes off to a database to get some information, it will send the requests to your database in quicker succession than if your methods were synchronous. This means that it makes your data source the bottle neck rather than your actual application. I would suggest seeing how it goes, profile your application if you hit problems and decide the best way forward case by case. – Luke May 27 '17 at 17:32
  • @Luke Are you implying that if I the web api is called twice or more in an mvc action method the requests will be sent quicker, if the methods are asynchronous? And would you say it's better to make them async or sync for now? – Mykhailo Seniutovych May 27 '17 at 17:50
  • I think you might be confusing what WebAPI is, you wouldn't call WebAPI from an action method. Your web page might call WebAPI though via Javascript though. Yes, if your WebAPI method is defined as async all the way up your call stack, including an async database call then if this method is called twice in quick succession, the UI bound threads will be freed up for use by other callers, so yes in theory calls to your database could be less likely to be blocked by a lack of threads, thus they will hit your database more quickly depending on the load. – Luke May 27 '17 at 18:12
  • My whole website is synchronous and I don't have any problems with it when my site is under load, I just scale my site's resources up to meet the demands. I find it easier to scale up my web servers than I do my database, so I'm happy for my calls to be synchronous so that under serious load it doesn't hammer my database. I haven't found the need to use async methods yet and my site gets millions of hits. Is yours going to get millions of hits? :P – Luke May 27 '17 at 18:19
  • For my site, I might be tempted to make my database access async, if I know that the database can handle the load. If I was using a nosql database instead of MySQL as I am now. NoSQL is optimised for fast reads and writes so your application would benefit from async methods if you are using that. What database engine are you using? – Luke May 27 '17 at 18:22
  • @Luke "you wouldn't call WebAPI from an action method" - I was going to use HttpClient, wrap it in some UserWebService as demonstrated in my code above, and make web api requests from the action methods, so basically an action method will call UserWebService, that will use HttpClient, to make a request to the web api and fetch the response back into the action method, from where it'll go to a view. I wasn't going to call web api from javascript. Is it a bad strategy. Speaking of the database engine we are using MS Sql 2012. – Mykhailo Seniutovych May 27 '17 at 18:47
  • 1
    How is calling WebAPI from JavaScript a bad strategy? Calling WebAPI using HttpClient from within your action method is very strange. Is there a reason that you need to do that when you could just call the logic directly? – Luke May 27 '17 at 19:03
  • @Luke Unfortunately I don't have enough experience with javascript, and the timeline is pretty tight, so I would better leverage the c# skills I have, using HttpClient, more razor and less javascript. Besides if I do that in javascript, what will be the sense of having MVC layer, if the controllers don't have any logic, except returning the views? Do you think that strategy may lead to some issues later on? – Mykhailo Seniutovych May 27 '17 at 19:17

1 Answers1

3

the web api I'm consuming has synchronous action methods, and will run on the same server

This is a highly unusual design. But I'll ignore that.

You can think about it this way: if the MVC actions are synchronous, then they will take 2 threads per request (one for the MVC action, one for the WebAPI action). If the MVC actions are asynchronous, then they will take 1 thread per request (none for the MVC action, one for the WebAPI action). So there's still a clear benefit to doing async on your MVC actions.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810