I have an ASP.NET Core Web API running under Kestrel(Ubuntu) and I faced with a strange situation: When I run the series of the first 20 API calls, the first 3-5 calls are quite slow, then the response time is ok. Then I make a short delay (could be a minute or even less) and run the series of the API calls again, and again the first several calls are quite slow and only after the first 3-5 calls the response time becomes ok.
Initially, I thought the issue is in the Kestrel configuration, so I made the following settings:
var host = new WebHostBuilder()
.UseKestrel(options =>
{
options.Limits.MaxConcurrentConnections = 200;
options.Limits.MaxConcurrentUpgradedConnections = 200;
options.Limits.MaxRequestBodySize = 10000;
options.Limits.MinRequestBodyDataRate = new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10)));
options.Limits.MinResponseDataRate = new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10)));
options.Limits.KeepAliveTimeout = TimeSpan.FromDays(2);
})
.UseConfiguration(config)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
It helped me to make my service to work faster, but the issue is still on.
The basic logic of the service is the following: 1) get the request object 2) parse it into the instance of the POCO class 3) call the DB with the multiple SELECTs to get all the required data (for this purpose I am using Dapper and the method which allows running the multiple SQL queries with one go) 4) Update the instance of the object with the newly received data and insert the object into the DB
That's it.
And I can't figure out what causes this delay(idle time).
I had a guess that maybe I should have some dummy calls to keep the service running. So I added a Singleton which contains the Timer job to query the DB for some lookup data every min. But It did not help.
Then I tried to add another timer job to query the data which are required in the Step N3 for just the 1st record in the DB, without some specific req parameters and it did not help, moreover, it started working more slowly.
I also added the indexes on the table, to make the SELECTs to work faster and additionally I added WITH(NOLOCK) statement to all the SELECTs but it did not help.
Any ideas, guys?