Categories
Uncategorized

Keep A Consumption-Based Function App Warm With A Scheduled Task

With Azure Function Apps using a Consumption plan, they will need to be warmed up if not used for 20 minutes to prevent having cold starts for the users in place. If you’re serving an API using a Function app, you’ll want to put this in place to keep performance ideal.

Something to note with this solution – it works well for low-traffic APIs where the goal is to serve an API using the consumption app for low costs. Assuming larger traffic use, you may be better off switching to a dedicated App Service plan, to prevent the cold start issue at all, because cold starts will still come when scaling out.

To follow this guide, I’ll assume you already have a Function app in place. Create a new function with the following:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace YOUR_NAMESPACE
{
  public static class KeepWarm
  {
    [FunctionName("KeepWarm")]
    public static void Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, ILogger log)
    {
      log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }
  }
}

That’s it! After this is deployed, the cold start issue of your API should be removed, as running the log function every 15 minutes will prevent the system from needing a cold start.

Further Reading

https://mikhail.io/2018/05/azure-functions-cold-starts-beyond-first-load/

Leave a Reply

Your email address will not be published. Required fields are marked *