Getting to grips with Azure Functions and WebJobs

Microsoft Azure

Azure Functions are a mechanism to allow you to develop serverless applications within Azure and to run small pieces of code or ‘Functions’ in the cloud without having to architect a complete application or infrastructure to run it.

Like most of the Microsoft solutions these days, it not only supports C#, but also other programming languages such as F#, Node.js, Java and PHP. But most importantly, you’ll only pay for the time that your Functions run, and Azure will handle any scaling requirements.

Functions are essentially the evolution of Azure WebJobs. They are built on top of the WebJobs architecture, which means you’re likely to come across references to WebJobs from time to time and seeing as they are still supported, it’s definitely worth understanding them. So here goes…

WebJobs reside within the Azure App Service and allow you to run a script or code in the context of an Azure App Service. It is not mandatory for a Function to run within an Azure App Service even though it can, but it is mandatory for a WebJob. As a result, a WebJob can’t benefit from the pay-per-use pricing model that a Function can. If you already have an Azure App Service and there are sufficient resources, a WebJob could utilise the same App Service with no additional costs. Alternatively, a new dedicated App Service Plan could be created for the WebJob.

Azure Functions support a consumption plan model where you only pay for the time that your code runs, or you can use the App Service Plan model where your Functions can run within an existing App Service that you may be using for a Web App, API or Mobile App. If you are already using an App Service and the App Service Plan has sufficient resources to support Functions, then you wouldn’t incur any additional costs for running the Functions.

Typically, you’d use Functions for data processing, systems integration, part of IoT solutions and building simple APIs and microservices. Image and batch processing and tasks that need to run on a scheduled basis also lend themselves well to Functions.

Both Functions and WebJobs can be configured to be manually triggered, run on a scheduled basis, continuously run or triggered based on events in other Azure Services. Functions are also suitable for running short and infrequent background tasks that can benefit from the Consumption Plan model.

Alternatively, long running and frequent tasks may be better suited to WebJobs which use the App Service Plan where the cost will be fixed, and you can benefit from the sharing of resources with a Web App, API or Mobile App.

In summary, Functions and WebJobs are viable options to be part of cloud native solutions for serverless architectures and microservices, or running background tasks in a cost-effective manner.

The full list of benefits can be found below.

  1. Multiple language support including C#, F# or JavaScript
  2. Pay-per-use pricing model so you only pay for the time that your code is running
  3. Use of your own dependencies as Functions support NuGet and NPM
  4. Utilise other Azure services and SaaS offerings
  5. Flexible development approach, developing your Function directly within the Azure Portal or an IDE such as Visual Studio

Coming up in my next blog, I’ll be taking a look at the features and benefits of Azure SQL, Microsoft’s fully managed version of the relational database SQL Server.

Posted by Tom Barrand