What are Azure Durable Functions

This Azure tutorial will discuss a complete Azure Durable Functions Tutorial. So, before we start the discussion on What are durable functions in Azure, you should know what an Azure function is.

What are Azure Durable Functions

Azure Durable Function is an extended version of Azure Function that helps us develop stateful functions in a serverless environment, and stateful workflows using the code with the help of a different type of function called an orchestrator function.

As discussed above, Azure Durable Functions are an extension of Azure Functions. Compared to Azure Functions, Azure Durable Functions are more capable in terms of functionality, Performance, etc.

Azure Durable functions can do what an Azure Function can do with many extra features to automate the business process. Working with the Workflow from code becomes easy with the help of Durable functions.

Serverless

As we already know, Azure functions are part of the serverless architecture, meaning we need to concentrate on writing the Function code without causing any headaches for the Infrastructure.

Deploying our Azure function to Azure does not cause us any headaches regarding Infrastructure, even though we don’t have to define explicitly how many servers we want to execute our Azure Function. Thanks to the Azure Function runtime, we can define the number of servers we need and handle the Overall load.

Why Durable Functions?

This is very important to know. Why should we go with the Durable Functions? Some things are difficult to implement with the regular Azure Functions.

So, here are the Durable Functions that help us to perform many activities, if you want to define the workflow in your code, if you’re going to implement error handling in the code with the help of workflows, if we’re going to trigger parallel execution, etc

Durable Functions are stateful and help track the workflow status in each stage. Because of their stateful nature, they also help implement some advanced workflows for our business.

Supported Languages for Durable Functions

As of now, below are the languages that are supported to build Durable Functions

  • It supports the C# language.
  • Durable function version 1.7.0 or later is supported by the Azure Function runtime version 2.x.
  • Durable function version 1.8.5 or later version supports Python.
  • Function version 1.x supports F# script.
  • PowerShell supports the durable function version 2.2.2 and the Azure function version 3.x, which is currently in Public Preview.

Main Components for Durable Functions

The concepts below are the main components of durable functions. They play a vital role when working with the Azure Durable Function.

  • Orchestrator Client
  • Orchestrator Function
  • Activity Function

Orchestrator Client

The Orchestrator client starts and stops the orchestrator functions and monitors them. When you create an HTTP-triggered function initially, you can call that HTTP-triggered function the Orchestrator Client. This is responsible for getting the request, and then it starts the orchestrator function. So, this is the first step in the whole process.

using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Newtonsoft.Json;
using System.Net;

[FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, IDurableOrchestrationClient starter,
            ILogger log)
        {

Orchestrator Function

The Orchestrator Function plays a significant role in building the Durable function, which is the heart of the Durable function solution. In this function, you need to write the workflow in code. You can call the Activity function from the Orchestrator Function inside the code. You can also call another Orchestrator Function and Sub Orchestrator Function from this primary Orchestrator Function.

Activity Function

The activity Function is the basic unit of durable function that executes one task. You can call many activity functions inside the orchestration function. Each one of the Activity functions executes one task.

azure durable functions

Key Benefits of Durable Functions

There are many benefits of durable functions. There are a few essential tasks that we cannot perform with the help of regular Azure Functions, but we can perform those tasks efficiently with the help of Durable functions. Let’s discuss a few key benefits of Durable functions regarding Workflow.

Build Workflows in Code

With the help of the Durable Functions, we can comfortably build workflows from our code. These workflows help us automate our business processes and handle errors in our code, which is very important.

Parallel activities

The Azure function can help you perform multiple parallel activities if you are using multi-step workflows. That helps to achieve complex workflows.

Can Implement the Time Out Option in Workflow

In case of any significant delay in response, while running the workflow, you can set the timeout option for the workflows easily with the help of the Durable function.

Track the Workflow Progress Easily

Durable Function can help you to track the progress of a workflow easily with the help of the REST API implementation. You can use the query status API to track the workflow progress easily.

Easy Cancellation of the Workflow

With the help of Rest API and a durable function, you can easily cancel the in-progress workflow. It was not accessible with regular Azure Functions.

Different Patterns Of Durable Functions

Multiple Function execution

The durable function helps you execute a sequence of functions, i.e., the chain of functions, quickly. When I say chain of functions, I mean the functions will execute sequentially. The output of the first function will be the input for the second function, and the output of the second function will be the input for the third function.

durable functions

Supports Fan-out/fan-in

The durable Function supports the Fan-out/fan-in pattern. You might wonder what exactly the meaning of this pattern is. According to this pattern, all the functions execute in parallel and then wait for all to finish.

durable functions azure

Asynchronous HTTP APIs

The asynchronous HTTP API pattern is the problem of interacting with long-running operations with external clients. HTTP calls help us with this. Durable functions have built-in APIs that help us interact with long-running function executions.

Monitoring

The following pattern for the Durable function is the Monitoring pattern, which helps to enable flexible recurrence intervals. It also helps to manage the task’s lifetimes, and for a single orchestration, it creates multiple monitor processes.

Human Interaction

This is another excellent pattern from the Durable Function that helps us in business approval workflows. Suppose you consider the scenario where the approver is unavailable in the business approval workflow. In that case, this pattern helps to set the timeout in case there is a considerable delay in response. It also helps with sending notifications or reminders to the approvers in case of a delay in response from the approvers.

When To Use Durable Functions

In many cases, you can use the Durable Functions. So, below are some key requirements when you can go with the Durable Functions.

  • When you need to develop a workflow with the Azure Function, you can use the Durable functions that help write the workflow from the code in this scenario. You can write the complex approval workflows and implement the timeout in case of significant delays from the approver side. You can also remind the approver to approve the workflow, etc.
  • In case you are required to work for the recurrence intervals,
  • If you want to manage the task lifetimes.
  • If you are required to work with multiple Azure Functions,

Different Ways to Develop Durable Functions

There are different ways to develop or build Durable Functions. Below are a few relevant ways that you can use to create Durable Functions.

Azure Portal

You can use the Azure Portal to develop Durable Functions. This is one of the best ways. You do not need to install any software on your local machine, and you do not need to take any headaches for the infrastructure.

Visual Studio Code and other Command-line Interface

You can use the Visual Studio Code editor or the other command line interface to develop the Durable functions.

Visual Studio IDE

You can also use Visual Studio, one of the excellent IDEs for developing Durable functions. Here, you will get the Debugging options and intelligence support that makes the development experience more accessible. You will enjoy working with Visual Studio. You must install the  Azure development workload, Azure Functions, WebJob Tools Visual Studio extension, and Visual Studio.

How To Create Durable Functions Using Visual Studio 2019

Here, we will discuss how to create Durable Functions using Visual Studio 2019. Follow the below steps to develop Durable Functions using Visual Studio 2019.

1. Open Visual Studio 2019 from the local machine. Click on the Create a New Project button.

2. Select the Azure Functions project template and click the Next button.

what are durable functions azure

3. Now provide a project name, choose a location for your project and then click on the Create button.

durable functions azure c#

4. On the next window, select the HTTP trigger, select the Storage Account (AzureWebJobsStorage) as Storage Emulator, and then choose the Authorization level as Function. Finally, click on the Create button.

what are durable functions in azure

Now it will create the Project successfully. It contains 3 main files. Function1.cs file is the main class file containing the Azure Function code. The next one is the local.settings.json file, which is responsible for keeping all the configurations we need during the project development and testing the function in the local environment. The Host.json file is responsible for keeping all the configuration related to your Azure Function. See below. The project was created successfully with all three files

what are azure durable functions

Below is the Azure Function code

what is durable functions in azure

You can see below the code for the local.settings.json file

what is azure durable functions

Finally, you can see the code for the host.json file below

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
}
when to use azure durable functions

Now, our Azure Function project is ready. The next step is enabling the Durable Function options by allowing the extensions.

How to Enable Durable Functions

Now, to enable the Durable Functions, we need to add Microsoft.Azure.WebJobs.Extensions.Durable Task Nuget package to the Project.

Right-click on the Project and then click on the Manage NuGet Package

durable functions azure

Now, search for Microsoft.Azure.WebJobs.Extensions.DurableTask nuget package and then click on the Install button.

Develop Durable Functions Using Visual Studio 2017

The next step is to click on the I Accept button to accept the license agreement.

durable functions azure example c#

Now, if you click on the .csproj file by clicking on the Project name, you can find the reference has been added for Microsoft.Azure.WebJobs.Extensions.DurableTask nuget package

Develop Durable Function Using Visual Studio 2019
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.3.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.36" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Now our Durable function project is ready, let’s check if the function is working as expected. Press F5 to run the Project. You can see I got the Azure Function URL as below.

http://localhost:7071/api/Function1

How To Create Durable Function Using Visual Studio 2019

Now, open your Favorite browser and paste this URL, appending the name value with the query string parameter. You can see I got the expected output as below.

How To Develop Durable Function Using Visual Studio 2019

Check out Durable Functions vs Azure Functions

Durable Functions Limitations

As we have already discussed, Azure’s durable function provides us with many benefits. With the benefits, there are also a few key constraints concerning code. Let’s discuss a few critical constraints of Azure durable function.

  1. You must be very careful while updating the Azure Durable function app code. It might break the expected behavior of the Azure Durable function.
  2. It’s challenging to generate guides or random numbers.
  3. It is challenging to implement Thread.Sleep.
  4. You can not call non-deterministic APIs from Azure Durable Functions. The API must be deterministic. This means it always returns the same value by providing the same input, regardless of when or how frequently it is called.

You can check out a complete guide on durable functions limitations now.

Azure Durable Functions Pricing

The price of Azure Durable functions is the same as Azure Function Pricing.

You may also like the following articles below

Wrapping Up

In this article, we discussed a complete Azure Durable Functions Tutorial. I hope you have enjoyed it!

Azure Virtual Machine

DOWNLOAD FREE AZURE VIRTUAL MACHINE PDF

Download our free 25+ page Azure Virtual Machine guide and master cloud deployment today!