8 Steps to Solve Cold Start Problem in Azure Functions
This tutorial will show a cost-effective way to set up Logic App on Azure to resolve this issue without coding in just a few steps.
Azure Functions is a serverless architecture that can reduce your administration work on the backend.
By default, it will be set up in the Consumption plan to save costs by removing server instances while having no traffic. But it will cause a high latency in the first request or even have no response in API due to no server instances available at some time.
I experienced such a weird problem that Azure Function’s server was silently stopped without any notice. Even if I sent an Http request to Azure Functions, Azure Functions still had no response and no instance got initialized.
To solve this issue, I upgraded it to a premium plan with a persistent server instance. However, this comes with very expensive server costs for the development phase.
How to save costs in the development phase and avoid the cold start problem in Azure Functions?
I discovered a tricky way to set up Logic Apps which can periodically send HTTP requests to the health check endpoint of Azure Functions for keeping it online. But it seems hard to find an example of how to do this.
This tutorial will show you how to set up Logic App on Azure to resolve this issue without coding in just a few steps.
To resolve this issue, we plan to implement the following
- We need to send HTTP requests frequently to Azure Functions to keep it alive.
- If it is down, send an alarm email and start Azure Functions.
- Wait for a few sends until Azure Functions finish the initialization.
- Then send an HTTP request to the health-check endpoint of Azure Functions.
- If it’s fine, send an email to notify you that Azure Functions is successfully restarted and online.
Let’s set up our plans in Logic App on Azure.
Step1: Login to Azure and Create a Logic App
- Click Logic app designer
Step2: Create Recurrence
You can decide how frequently to check your health check endpoint.
On these days: ( decide which day in a week )
At these hours: ( decide which hour in the day )
At these minutes: ( specify the frequency in minutes )
- You can adjust those parameters for your working hours as needed.
Next, we need to create an Http Request after the Recurrence
Step3: Create an Http Request
You set up the Method and URL for your health check’s API endpoint in Azure Functions.
Step4: Create an Alarm email
If the previous Http Request failed, then send this email.
Step5: Create Invoke Resource operation to start Azure Functions
Create Invoke Resouce operation as below
Subscription: <Fill in your subscription name>
Resource Group: <Fill in your resource name>
Resource Provider: Setup Microsoft.Web
Short Resource Id: Site/<Setup You Azure Functions Name>
Client Api Version: <Azure Api Version>
Action name: Setup Start
- Subscription, Resource Group, and Short Resource Id: Go to your Azure Functions → Click Property → you will find them
- Client Api Version: Go to your Azure Functions → Click json view → You can see the version number
Why we need to set up Action Name as Start?
Because I found this can function well while restart cannot work.
Step6: Create a Delay
We need to create a delay in a few seconds before sending the health Check request because restarting the Azure Functions might take some time to work well.
Step7: Create an Http Request
After the delay, we set up an Http Request to our health-check endpoint for verifying the health of Azure Functions.
Step8: Create a notification email
When the previous request is successful, we create a notification email as below.
Then you should have the following logic and click Run Trigger → Run
- To make your lives become much easier, we send an email at the final stage to notify you that Azure Functions can automatically recover after unexpected events happen.
- If you only get an alarm email while do not receive the final email, remember you should take action to check Azure Functions immediately.
That’s it. I hope you learned this tricky way to set up Logic Apps to save money by using cheap Azure Functions in the Consumption plan.
With this approach, you don’t need to even manually check Azure Functions during unexpected incidents because they can automatically be restarted to ensure high resilience by Logic Apps.
In Logic Apps, we can easily implement logic without coding and bug-free. It can definitely save you time and effort when compared to implementing the same features in shell scripts on the server.
If you liked this story, please show your support by 👏 this story or subscribe to our email to get the latest tutorials in the future.
Thank you for reading!
You may be interested in
Tips to Setup Serverless Architecture with Azure Functions or AWS Lambda.
Serverless architecture is a way to reduce your administration and maintenance work on infrastructure. You can put all…
How to set up Zero Downtime Deployment for Azure Functions in Jenkins?
This tutorial details how to create a script to deploy Azure Functions with Zero Downtime Deployment in Jenkins.