Intermittent Delay Before SQL Query Execution in Azure App Service (EF Core with Failover Group)

Woi Ang 20 Reputation points
2025-07-02T08:28:48.5366667+00:00

We're experiencing sporadic delays in our Azure App Service (ASP.NET Core with EF Core) when interacting with an Azure SQL Database configured in a failover group. The issue is not related to query execution time—once a SQL query starts executing, it completes in ~15ms. However, we’ve observed that the query does not begin executing until ~10 seconds after the server receives the incoming HTTP request.

Key Observations:

  • The delay is intermittent, affecting otherwise performant queries under normal load.
  • Application Insights traces (with .etl.zip available) show the delay occurs before SqlCommand.ExecuteRead starts.
  • ExecuteRead completes promptly (~15ms), but isn’t invoked until 10 seconds after request receipt (from Application Insights trace log).
  • The delay period does not show any obvious blocking, error logs, retries, or EF Core exceptions.
  • App Service is running on Azure and uses EF Core with EnableRetryOnFailure configured but there is no retry - only 1 query shown in the App Insights sample.
  • Application connects to Azure SQL via failover group connection string.
  • We have checked the session count of the Azure SQL Server - max count in the chart is only 16 - way below the default pool limit of 100. There is no DBContext leak (undisposed) as we will log error in the DBContext finalizer if Dispose() is not called, which is not happening.

Please help:

  • Recommending any diagnostic steps or instrumentation (e.g., SqlClient logging, App Service metrics, EF Core event hooks) to isolate and address the root cause
  • Determining whether this is caused by connection acquisition delays, retry attempts, or other infrastructure-level latency. How can we tell the delayed SQL command is blocked by:
    • Connection pool exhaustion or slow connection acquisition
    • DNS resolution, or failover redirection
    • SqlClient or EF Core retry logic triggering silently before ExecuteRead
    • SNAT ports are used up?

Thanks.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,993 questions
{count} votes

Accepted answer
  1. Bhargavi Naragani 6,535 Reputation points Microsoft External Staff Moderator
    2025-07-08T06:08:39.97+00:00

    Hi Woi Ang,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this!

    Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Issue:

    Intermittent Delay Before SQL Query Execution in Azure App Service (EF Core with Failover Group)

    Solution:

    Thanks. Think our problem is resolved. Basically, marking the region, we don't want GC to happen and then set:

    System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.SustainedLowLatency;

    which seems to work better for our webapp.

    Everything else seems to be the default values.

    Please click Accept Answer and kindly upvote it so that other people who faces similar issue may get benefitted from it.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Woi Ang 20 Reputation points
    2025-07-04T08:16:09.9166667+00:00

    Thanks for the further suggestions.

    1. We restrict our App Service to only a single instance (which is always-on on Premium v2 plan) - so it won't be scaling or restart - I also have logs when restarted.
    2. We already have AppInsights profiler trace when it was slow and usually see excessive long wait time here:User's image

    So it looks like 'allocMem' taking a long time but mainly 'Waiting'? Just wondering if is a system level memory management issue? I downloaded the etl from the transaction details and it looks kind of like this:User's image

    1. As in the snapshot above (I was looking at w3wp), our hosting model is 'InProcess' - so it lives in the application pool of IIS and Kestrel is out of the picture.
    2. I can be sure is not the delay before the controller code runs as I had logs in the controller and could see the delay happens after that.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.