We have a simple Azure Function that makes a DocumentDB query. It seems like the first time we call it there is a long wait to finish, and then successive calls are very fast.
For example, I just opened our app and the first Function call took 10760ms, definitely noticeable by any end user. After this, all Function calls take approximately 100ms to process and are nearly imperceptible.
It seems as though there is some "wake up" cycle in Azure Functions. Is there some way to minimize this, or better yet is this documented somewhere so we can understand what's really going on here?
Vendor-lock is the biggest drawback of this functions. It is very difficult to run code deployed in Azure function outside the azure environment. As the language used in function app such as java, NodeJS, etc are not specialized but code to establish a connection between resources is specific to azure.
After further investigation, I realized that the following: Azure Function scripts sometimes takes anywhere between 10 seconds to more than 1 minutes to connect to SQL database. Sometimes the scripts will run in few millisecond and sometimes it will take more than 3 minutes to completely run the script.
Azure Functions are automatically woken up based on their triggers (e.g. timer, http). There is no requirement to do this manually.
Once your Function executed, it will stay 'warm' for the next 20 minutes or so, to execute subsequent requests.
Function apps running on a consumption plan do indeed have an idle time after which they effectively go to sleep. The next invocation is required to "wake them up" as you've observed and people have mentioned in the comments.
As to why this happens, it's so that Microsoft can most optimally distribute compute workloads in a multi-tenant environment while ensuring that you're only billed to the second for the time where your function is actually doing work. This is the beauty of serverless.
For workloads where this is not acceptable behavior, you could consider moving off of the consumption plan and on to the actual App Service plan. Alternatively, you could implement a timer triggered function that goes off every minute for example and use that as a "keep alive" mechanism by pinging the function that you don't want to go to sleep.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With