🔥 Make sure you star the repo (opens new window) to keep up to date with new tips and tricks.

💡 Learn more : Azure Functions Premium plan (opens new window).

📺 Watch the video : Using Azure Functions Premium Plan to avoid cold-start (opens new window).

# Using Azure Functions Premium Plan to avoid cold-start

# Azure Functions cold starts

Azure Functions (opens new window) are incredible. You can use them to run pieces of code, without having to create the plumbing to run a complete application. This is perfect for processes that you run in the background or that are decoupled from your application (like a process that resizes images that users upload). You can run Azure Functions serverless, in their Consumption pricing plan (opens new window). This is great, because Functions that run in the consumption plan, scale automatically. And you only pay for functions that you execute, you don't pay the whole month for having a function that can run.

When you have functions that run in the consumption plan, they will have a 'cold start'. This is a small period of time that the Function App needs to warm up and start the execution of your function. This is needed because the Function runs in a shared environment and needs compute resources to be allocated to it. Once your Function executed, it will stay 'warm' for the next 20 minutes or so, to execute subsequent requests. How long you have to wait for a Function App to warm up, is different for each Function and typically ranges between 1 and 3 seconds. You can find a detailed analysis of how long cold starts can be in this article (opens new window).

There are several ways to deal with cold starts in Azure Functions. You can run your Function App in an App Service Plan (opens new window). When you do this, it runs on compute resources that you reserve and pay for, 24 hours per day. Functions that run in an App Service Plan do not scale automatically, like their consumption-plan counter parts.

Another way to avoid cold starts is to use the Azure Functions Premium Plan (opens new window). This is a plan that allows your Functions to scale automatically and eliminates cold starts. It also offers more advanced features, like VNET integration (opens new window) and a maximum function duration of 60 minutes (if you modify the host.json configuration (opens new window)).

In this post, we'll set up an Azure Function with a Premium Plan to see how it eliminates cold starts.

# Prerequisites

If you want to follow along, you'll need the following:

# Avoid cold starts with the Azure Functions Premium Plan

Let's create an Azure Function in a Premium Plan to avoid cold starts.

  1. Go to the Azure portal (opens new window)

  2. Click the Create a resource button (the plus-sign in the top left corner)

  3. Search for Azure Function and click on the result to start creating a new Azure Function a. This brings you to the create Function App blade b. Select a Resource Group for the Function App or create a new one c. Fill in a Name for the Function App d. Leave the Publish setting to Code. We'll work with the code that an out-of-the-box Function comes with e. Select .NET Core for the Runtime Stack setting f. Select a Region for the Function App g. Click Next: Hosting

    (Hosting configuration for a new Function App in the Azure portal)

    h. In the Hosting tab, leave the Storage account and Operating System settings as they are i. For Plan type, select Premium j. The Sku and size indicate the compute resources that you reserve for this Function App. Leave the Sku and size as they are. You can scale this up if you want in the settings of the Function App k. Click Review + Create and Create after that to create the Function App

When the new Function App is deployed, you need to create a new Function in it.

  1. Navigate to the new Function App in the Azure portal
  2. The Function App is a container that can contain and run one or more Functions. Click New Function to start creating a new Function
  3. Select In-portal and click Continue. This makes it easy for us to create and run the Function

(Create new Azure Function in the Azure portal)

  1. Now select Webhook + API and click Create. This creates a new C# Azure Function that is triggered by an HTTP request

When the Function is created, you'll see that it has some out-of-the-box code in it. We'll leave that in there and use it to test the Function.

Function Apps that run in the Premium Plan can eliminate cold starts. They can do that by prewarming Function instances so that they are ready to handle incoming requests. When you click on the Function App name (above the Functions) and click Platform Features > Scale out, you can configure the instances of the Premium Plan. You can configure the minimum of instances that the plan should run and the maximum. Configuring the maximum amount is a great way to restrict the amount of instances that can be run and thus restrict the amount of money that you spend. Each Premium Plan needs at least one instance to run and be pre-warmed, so using the Premium Plan will cost you a minimum fee per moth. You can increase the minimum amount of instances and prewarmed instances to make sure that requests to your app never run into a cold start. Let's leave it at one for now.

(Configure Azure Function Premium Plan instances in the Azure portal)

Let's test it. Click on the Function that we've created earlier and click the </> Get Function URL link to retrieve the URL for the Function. The default code takes a parameter called name, so you can take the URL and append &name=somename to it to test it out. Paste the URL in a browser to test it out.

(Get Function URL in the Azure portal)

This should be instantaneous as there is no cold start. The Function executes on the pre-warmed instance and executes immediately.

(Result of executing the Azure Function)

# Conclusion

The Azure Functions Premium Plan (opens new window) is a powerful plan to run your Azure Functions in. You can use it to eliminate cold starts and still have the benefits of serverless, like automatic scaling. Go and check it out!