Warming up Azure Functions to prevent the cold start

licitdev
2 min readMay 21, 2019

--

Azure Functions is a server-less computing service by Microsoft that scales based on the workload. This is an always free service that provides for 1 million requests and 400,000 GB-s of resource consumption per month.

A function runs like a container and will spin down after a certain timeout between 10–20 minutes. It takes around 5–10 seconds or longer for a container to cold start depending on your project. For my instance, my function is required to access a database and caching service. That adds to the time needed before the function could be executed.

Hence, keeping Azure Functions in a warmed up state is crucial especially if it is not accessed frequently and you would like your requests to be completed fast. By keeping the function in a warmed up state, the database connection could be reused which saves on time needed to reestablish a connection.

An easy way to keep an instance of your function running is to access it on a regular basis. Personally I “ping” my functions every 5 minutes to keep them alive.

“Ping” once every 5 minutes to keep Azure Functions in the warmed state

The following cron time string can be used in various ways.

*/5 * * * *

A timer based function could be created with the above cron time string as the schedule. In the timer based function, you could do a request to the functions which you wish to keep alive.

Creating a new timer function in Azure Function

Another way is to utilise cron on your server to “ping” the functions. The following code could be called by cron to access the function. This method is good if you already have a personal server running.

curl — silent https://{function_app_name}.azurewebsites.net/api/webhook?code={function_access_code}

You may wish to add a short circuit in your azure functions so that each “ping” would be completed faster. For example, if your function requires POST fields, the short circuit could be checking for missing fields. Or adding a query string for a GET request so that your function is able to recognise and short circuit the request.

--

--