There are a few choices when it comes to implementing custom background processing tasks. The main options in the Microsoft Azure cloud are to use either Azure Functions or Azure Web Jobs. Functions are newer, cooler, and may seem to be the obvious choice going forward, however, the choice isn’t really that easy. This article outlines the points you need to consider when deciding between Azure Functions or Azure Web Jobs.
Azure Functions are Web Jobs
Before we get into the specifics of when Azure Functions are appropriate, or when Azure Web Jobs are appropriate, it’s best to mention that Azure Functions are built on top of the Azure Web Jobs foundation. So, essentially, Azure Functions are Web Jobs. You could almost say that between the two, it’s Web Jobs all the way down.
With Azure App Service and Web Apps, Microsoft implemented Azure Web Jobs as a means to more easily host background processes, long or short running. Web Jobs are hosted within an App Service Plan either stand-alone or along side a Web App (or API App, or Mobile App).
When you get down to it, Azure App Service is hosted on Managed VMs (Virtual Machines) underneath. An Azure App Service Plan is the way that the PaaS (Platform as a Service) services within Azure App Service (Web Apps, Mobile Apps, and API Apps) define the underlying VM.
Web Jobs allow for you to share the resources of an App Service Plan between a Web App and Web Job if you wish in order to help save on hosting cost. Or, alternatively, you could choose to host the Web Jobs in their own dedicated App Service Plan.
With Web Jobs, your background processes can be executed as any command-line executable or script (.ps1, bash, executable, etc). This can be very convenient, but it still requires you to deploy out an entire application often times, since most custom background processes require more complex code bases than a PowerShell script or other command-line script.
In an effort to make the implementation of background processing easier in Azure, Microsoft built out Azure Functions. Azure Functions are actually built on top of Azure Web Jobs in the underlying platform. Consequently, at the core of Azure Functions is the same functionality of Web Jobs. You can setup and run any command-line executable or script as an Azure Function too.
The main different with Azure Functions is that it’s a service that takes the PaaS offering of Web Jobs and turns it up to the max. Instead of building an entire application, you only need to author and deploy individual methods (or functions) of code in an even further managed PaaS platform.
Now with the true difference and similarity of Azure Functions and Web Jobs defined, let’s get into the specifics of when you would choose to use one over the other.
Choosing Web Jobs
Do you have background processing that you need to implement? If yes, then Azure Web Jobs is a viable solution to fill that need. You can implement any type of background processing task as an Azure Web Job. There really isn’t much of a limit to what you can do.
Here are some highlights of the features of Azure Web Jobs:
- Web Jobs can be configured to be manually triggered or run on a schedule.
- Web Jobs can be configured to be Continuously running (aka running constantly, all the time)
- Web Jobs can be setup to be Triggered based on events in other Azure Services, such as a new messaged added to a Storage Queue or Service Buss Queue or Topic
- Web Jobs can be long running
- Web Jobs can be short running
- Web Jobs can be implemented in any language as either a command-line executable or script
As you can see from the above list, Azure Web Jobs can be implemented to fill any background processing need. Implementing background processing with Azure Web Jobs in an App Service Plan is generally a cheaper option over other IaaS based approaches.
Choosing Azure Functions
All the same features listed above for Azure Web Jobs also apply to Azure Functions. You can implement Azure Functions in any language as either a command-line script or executable. They can be manually executed, scheduled, or even triggered by other Azure Services in a similar way.
Even though Azure Functions can be long or short running, there are some caveats to that. Let’s go through what those are.
With the Azure Functions Consumption Plan you only pay for your functions when they are actually executing. This helps save significant cost over paying for an entire VM or App Service Plan. If you have an Azure Function that’s only executed a few times per week, as example, this could be extremely cheap.
What does paying for when the Function is executing really mean? The cost of Azure Functions are calculated in relation to the memory usage and total execution time. This relation of memory and execution time is measured in terms of Gigabyte-Seconds (GB-s).
This is an ideal option for hosting Functions that are short running and only intermittently run. With the emphasis on short running there is a maximum timeout threshold to how long Azure Functions can execute. This timeout threshold is set to 5 minutes. If the Azure Function completes within this timeout, then you’ll be able to use the Consumption Plan pricing without issue. However, if the Azure Functions take longer than 5 minutes to complete execution, then it will timeout and the process will be terminated.
Due to the timeout threshold of Azure Functions there are certain architecture and code design practices that will need to be followed in order to get longer running processes to run successfully within an Azure Functions Consumption Plan. The main practice would be to break up the Function into smaller separate functions. These separate functions would then be implemented using one or more message queues to communicate.
App Service Plan
The Azure Functions App Service Plan pricing utilizes an App Service Plan (just as Web Apps, API Apps, or Mobile Apps) to host Azure Functions. This is a more costly option than Consumption Plan.. With App Service Plan pricing you don’t pay for only when the Function is executing, but rather for the reserved resources of the underlying VM.
The reasons to use the App Service Plan pricing may not seem obvious, but there are 2 very good reasons that can make it a very useful option:
- Share resources with a Web App, API App, or Mobile App by running within the same App Service Plan.
- Reserve dedicated resources for Azure Functions that are either longer running, executed more frequently, or both.
By using an App Service Plan to share resources between Azure Functions and another App Service app (Web App, API App, or Mobile App) then the Azure Functions an utilize a possibly under-utilized App Service instance. This also has the benefit of the Azure Functions essentially being Free to run since the App Service Plan is already paying for the server resources.
Longer running Azure Functions (over 5 minutes) won’t run well with the Consumption Plan due to it’s timeout threshold. When using an App Service Plan to host and execute Azure Functions, the timeout threshold does not exist. Azure Functions are subject to all the same features as Web Jobs when hosted in an App Service Plan. This means Azure Functions in an Azure Service Plan do not need to be architected or designed differently to be broken up into smaller executable pieces.
Consumption or App Service Plan?
As you can see above there are some valid reasons for choosing to use either Azure Web Jobs or Azure Functions. While Azure Functions are built on top of Web Jobs, there still are some differences. However, the largest difference with Azure Functions is the pricing plans. While Consumption pricing only has you paying for when Azure Functions are actually executed, it does have a fairly big limitation, under certain circumstances, which is the timeout threshold. The App Service Plan pricing may be more expensive in certain circumstances, but it also allows you to have longer running Azure Functions, along with the ability to share the unused resources / capacity of an App Service Plan.
Azure Functions are the newer edition of Platform as a Service and the Serverless wave of hosting background processing within Microsoft Azure. Although, Web Jobs are still viable, it’s likely better to use Azure Functions if you can moving forward. However, the choice between Consumption Plan and App Service Plan may be a slightly difficult one depending on the needs of the application.