How to resolve external API latency while calling from Azure deployed application?

Viewed 177

Info : I have below 2 method which is part of Web API (not core API) and it is deployed in Azure

Method 1 :

public async Task<bool> ProcessEmployee(list<employee> EmployeeList)
                    var tasks = new List<Task<EmployeeResponseModel>>();

                    HttpClient localHttpClient = new HttpClient(); 
                    localHttpClient.Timeout = TimeSpan.FromSeconds(100);
                    

                    foreach (var employee in EmployeeList) // **having 1000 calls** 
                    { 
                     tasks.Add(GetAddressResponse(employee.URL,localHttpClient)); 
                    } 
                    var responses = await Task.WhenAll(tasks);
          }

Method 2 :

    private async Task<EmployeeResponseModel> GetAddressResponse(url, HttpClient client)
{
        var response = new EmployeeResponseModel();
        try
        {

            
            using (HttpResponseMessage apiResponse = await client.GetAsync(**url**))
            {

                if (apiResponse.IsSuccessStatusCode)
                {
                    var res= await apiResponse.Content.ReadAsStringAsync();
                    response = JsonConvert.DeserializeObject<EmployeeResponseModel>(res);
                   
                }
            }

            return response;
        }
        catch (Exception ex)
        {
        }
        return response;
    }

If i monitor from Azure -> Diagnose and Solve Problem -> Web App Slow all external API calls is showing latency issue

enter image description here enter image description here

But if i am calling same external API from Postman is is quite fast and having less latency

enter image description here

  1. method 1 and method 2 is part of one web api and it is deployed on Azure AppService.

  2. getAddress is external API which is been deployed in other environment and don't have much information

  3. if we are calling external API i.e 'getAddress' from 1) we are facing high latency more than 5 sec.

  4. if we are calling external API i.e 'getAddress' from Postman we receive response in 303 ms.

1 Answers

I guess it results from the location of the service plan.

If the location of the service plan is far away from you position, it may cause the latency. But it can't rule out other possibilities, so my suggestion is debug in localhost first to rule out the possibility of the code.

Related