Azure Function sometimes takes too long to load

Andreas Ellerbrock 6 Reputation points
2021-12-02T23:04:43.52+00:00

I have a couple functions that fetch data from cosmosDB but the load time ranges from a couple seconds to over 3 minutes.
154587-screen-shot-2021-12-02-at-45453-pm.png

//index.js  
module.exports = async function (context, req, inputDocument) {  
    context.log('Get all users');  
  
    context.res = {  
        // status: 200, /* Defaults to 200 */  
        body: inputDocument  
    };  
}  

this is the function json

{  
  "bindings": [  
    {  
      "name": "req",  
      "authLevel": "anonymous",  
      "methods": [  
        "get"  
      ],  
      "direction": "in",  
      "type": "httpTrigger"  
    },  
    {  
      "type": "http",  
      "direction": "out",  
      "name": "res"  
    },  
    {  
      "name": "inputDocument",  
      "databaseName": "wlc_dev",  
      "collectionName": "users",  
      "connectionStringSetting": "wlc-admin_DOCUMENTDB",  
      "sqlQuery": "SELECT * FROM c",  
      "direction": "in",  
      "type": "cosmosDB"  
    }  
  ]  
}  

Any idea what to do? I've set up the Log Analytics Workspace but I don't have enough data to find the root cause.

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
2,673 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,002 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,353 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MughundhanRaveendran-MSFT 12,396 Reputation points
    2021-12-03T10:31:28.87+00:00

    @Andreas Ellerbrock ,

    Thanks for reaching out to Q&A.

    There could be various reasons for this random slowness/ delay in Function execution.

    From the code, I can see that the document is read and set to response's context. So if the size of the input document is large, it is expected to take some time.

    If we look at this issue from the Function platform perspective, you might want to check the diagnose and solve problems blade in the Function app portal and run the "Function app down and reporting errors" detector. Check the web app restarted detector to see if there were any restarts during the time the slowness was seen. Might be a coldstart if you are on Consumption plan or if you havent enabled "always on" on dedicated App service plan.

    154774-image.png

    If the frequency of this issue is more. I would suggest you to add some logging in your code that would indicate which part of the code took longer time to execute. This would help in isolating the issue.

    Please let me know the outcome.

    I hope this helps!

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.