17

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?

Graham
  • 7,431
  • 18
  • 59
  • 84
  • You mean `Cosmos DB`....based on my understanding, after 5 minutes of idle time, your function enter a cold start mode....Preventing to change to cold-start mode is quite easy. Just add a time trigger function within the same function app which executes every 5 minutes. But be aware that this may cause additional costs if your free execution credit is exceeded. – Hackerman Aug 01 '17 at 21:41
  • Well, it's still CosmosDB DocumentDB (hehehe) but it could have been any back-end service and we still see this cold start. Is this documented anywhere? – Graham Aug 01 '17 at 21:54
  • 5
    I found it here https://blogs.msdn.microsoft.com/appserviceteam/2017/03/16/publishing-a-net-class-library-as-a-function-app/ ...Its says: Cold start occurs on the first request to a Function App after it has gone idle, which happens after 5 minutes of inactivity. When a Function App starts up, it indexes all functions and compiles C# and F# scripts into an assembly in memory. So, compilation time will add to cold start time. Customers aren’t charged for cold start since function execution hasn’t started, but it does cause a delay in event processing. – Hackerman Aug 01 '17 at 22:01
  • You should put this an answer :) – Graham Aug 01 '17 at 22:18
  • No problem @Graham...Give me an hour or two. – Hackerman Aug 01 '17 at 22:41
  • Too late I think.... – Hackerman Aug 02 '17 at 14:11
  • 2
    To the what and why of cold starts: https://blogs.msdn.microsoft.com/appserviceteam/2018/02/07/understanding-serverless-cold-start/ – ElliotSchmelliot Jun 28 '18 at 18:48

1 Answers1

15

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.

Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
  • I added a Function with a 4-minute timer in the same Function App yet we're still seeing occasional 10-20 second execution times that seem to come after idle. Could it be something else at play here? – Graham Aug 03 '17 at 01:33
  • 6
    Move to an App Service Plan and leverage the Always-On feature: https://github.com/Azure/Azure-Functions/wiki/Enable-Always-On-when-running-on-dedicated-App-Service-Plan – evilSnobu Aug 05 '17 at 08:19
  • 4
    After thinking about this some more, if I was going to move to an App Service Plan I'd just write native node.js code and not be bothered with the Functions overhead. – Graham Sep 19 '17 at 14:52
  • 4
    and there it is.... We too built something we thought was simple and clean using Azure functions and then ran into this and now have to consider "the cost" of paying more for it. Back to server side until MSFT realizes a function with a 5+ second coldstart will result in users giving up. I understand offloading if a function has not been accessed in 30 days... but 5 minutes??? – Hell.Bent Nov 08 '17 at 11:24
  • 1
    Does anybody know, is this a compiled vs precompile issue? Somehow we have an Azure function called internally by an HTML inside the functions folder and it does not have this issue. – Hell.Bent Nov 28 '17 at 00:28
  • 1
    Added a keep alive trigger for our python azure function, expecting to having to trigger it every 4 minutes or so, but had to lower it 90 seconds or it would cold start every other poll. – sevenam Oct 10 '19 at 15:02