I have the following async method on an MVC controller running in an application on IIS:
public async Task<ActionResult> GetStatus(Record record)
{
string key = $"status:{record.Key}";
Record cached = MemoryCache.Default.Get(key) as Record;
if (cached == null)
{
using (var client = new MyWebService.MyWebServiceClient())
{
var info = await client.GetStuffAsync(record.Foo);
record.Bar = info.Bar;
}
MemoryCache.Default.Set(key, record, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(15) });
}
else
{
record = cached;
}
return this.Json(record, JsonRequestBehavior.AllowGet);
}
The method is called via AJAX from the client's browser. The browser will send many (~100) requests at once and process each response as it comes back.
I'm noticing that it seems to be limiting the number of simultaneous requests to around five. What I'm stumped about, however, it that the bottleneck seems to be IIS responding to requests rather than the web service.
For example, if I do the following:
- On a fresh instance (nothing in the
MemoryCache
), send a bunch of requests, let's say for keys "A1", "A2", ..., "A100". Each request takes about five seconds to load, which is expected as theclient.GetStuffAsync
takes about that long. The requests seem to be returning in batches of about five at a time. - Load the exact same page, so the same requests ("A1", "A2", ..., "A100") are made. The data loads almost instantaneously because all of the results are cached. This is expected.
- Open another page and send a different batch of requests, say "B1", "B2", ..., "B100". This starts to load in simular manner to the first request.
- While this is going on, load the initial page ("A1", "A2", ..., "A100"). The page essentially freezes, waiting for the other requests ("B1", "B2", ..., "B100") to all complete. Then the page loads immediately.
This last scenario is the one that has me stumped. I would have thought that the await
on the web service would allow IIS to switch to other requests in the meantime. I'm OK with the long-running web service being the bottleneck, but right now it looks like IIS queuing up other requests even when it is waiting on the web service.