5

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?

ilkerkaran
  • 4,214
  • 3
  • 27
  • 42
Tom Soyer
  • 49
  • 3
  • Maybe it is connected with SQL Max Pool Size (in connection string)? – Denys Prodan Jul 31 '18 at 16:16
  • SQL Server caches data pages it reads into memory (which you can see with `SET STATISTICS IO ON`) which maybe involved. – Mark G Jul 31 '18 at 16:51
  • It would be awesome if you could provide a [mcve]. – mjwills Jul 31 '18 at 21:27
  • 1
    I have the same issue. I don't know the reason, but after some idle time the request is slow, next requests are fast. And the service is always on and has no DB connection. It seems how internally the Kestrel is implemented. – Maxim T Feb 19 '19 at 12:45

2 Answers2

0

When query execute getting time rather than desire then its throw timeout exception. We can resolve it by setting commandtimeout=0. When we set commandtimeout=0 then it will response after completing execution.

Md. Abdul Alim
  • 707
  • 1
  • 6
  • 19
-1

I was also thinking that maybe it's about the connection string, here it is:

Data Source=mydbserver;Initial Catalog=db1;Persist Security Info=True;User ID=bigboy;Password=bigboy;multipleactiveresultsets=True; Max Pool Size=200; Pooling=true;Connection Timeout=30; Connection Lifetime=0;Min Pool Size=0;

Tom Soyer
  • 49
  • 3