What Are Azure Durable Functions

What Are Azure Durable Functions

In this Azure tutorial, we will discuss What Are Azure Durable Functions. Along with this, we will also discuss a few other topics like Why Durable Functions, Supported Languages for Durable Functions, Key Benefits of Durable Functions, and we also discussed Different Patterns Of Durable Functions, When To Use Durable Functions, How To Create Durable Functions Using Visual Studio 2019.

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

What Are Azure Durable Functions

So, Before we start the discussion on What are Azure Durable Functions, you should know What is Azure Function.

As we have discussed above, Azure Durable Functions is an extension of the Azure Functions. Compare to the Azure Functions, Azure Durable functions are more capable in terms of different functionality, Performance, etc. It is obvious that the Azure Durable functions are capable of doing that an Azure Function can able to 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 that, Azure functions are part of the serverless architecture meaning just we need to concentrate on writing the Function code with out taking any headache for the Infrastructure.

While deploying our Azure function to Azure, No headache for us thinking on the Infrastructure part, even we don’t have to define explicitly, how many servers we want to execute our Azure Function. Thanks to the Azure Function runtime that really helps us to define number of server e need and it helps us to handle the Overall load.

Why Durable Functions?

This is very important to know, Why we should go with the Durable Functions? There are stuffs that are difficult to implement with the regular Azure Function. So, here is the Durable Functions that helps us to perform many activities like if you want to define the workflow in your code, if you want to implement the error handling in the code with the help of workflows, if we want to trigger the parallel execution, etc

Durable Functions are stateful in nature that helps to track the status of the workflow in each stage. This also helps to implement some advanced workflows for our business because of the stateful nature.

Supported Languages for Durable Functions

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

  • It supports 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.
  • The durable function version 2.2.2 is supported with PowerShell and it supports the Azure function version 3.x. Currently in Public Preview.

Main Components for Durable Functions

You can consider the below concepts as the main components for the Durable Functions. Those play a vital role while working with the Azure Durable Function

  • Orchestrator Client
  • Orchestrator Function
  • Activity Function

Orchestrator Client

The Orchestrator client starts and stops the orchestrator functions and it takes the responsibility to monitor them. When you are creating an HTTP triggered function initially, You can call that HTTP triggered function as the Orchestrator Client. This is responsible to get 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 major role while building the Durable function and Orchestrator Function is considered to be 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 main Orchestrator Function.

Activity Function

Activity Function is called as 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 function executes one task.

What Are Azure Durable Functions

Key Benefits of Durable Functions

There are many benefits of durable functions. There are a few key tasks that we cannot able to perform with the help of the regular Azure Function, we can able to perform those tasks easily with the help of the Durable functions. Let’s discuss a few key benefits of the Durable functions in terms of the Workflow.

Build Workflows in Code

With the help of the Durable Functions, we can able to build the workflows from our code comfortably. That workflows help us to automate our business processes as well as help us for error handling in our code which is very important.

Parallel activities

The azure function can help you to perform multiple parallel activities in case you are using the multi-step workflows. That really helps to achieve the complex workflows.

Can Implement the Time Out Option in Workflow

In case of any major 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

You can easily cancel the in-progress workflow with the help of Rest API with a Durable function. It was not at all easy with the regular Azure Functions.

Different Patterns Of Durable Functions

Multiple Function execution

The durable function helps you to execute a sequence of functions i.e the chain of function easily. When I am saying the chain of function meaning the functions will execute sequentially. The OutPut of the first function will be the input for the second function and again the output of the second function will be the input for the third function.

Different Patterns Of Durable Functions

Supports Fan out/fan in

Durable Function supports the Fan-out/fan-in pattern. You might be wondering what exactly the meaning of the Fan-Out/fan-in pattern. As per this pattern, all the functions will execute in parallel and then waiting for all to finish.

Benefits of Durable Functions

Asynchronous HTTP APIs

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

Monitoring

The next pattern for the Durable function is the Monitoring pattern that actually helps to enable the flexible recurrence intervals. It also helps to manage the lifetimes of the task 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 case of business approval workflows. if you will consider the scenario where the approver is not available in case of the business approval workflow, This pattern helps to set the timeout, in case there is a huge delay in response. It also helps with sending the notification or reminder 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 have a requirement to develop a workflow along with the Azure Function. You can use the Durable functions in this scenario that helps to write the workflow from the code and you can able to write the complex approval workflows where you can also implement the timeout in case of major delays from the approver side and you can also send the reminder to the approver to approve the workflow, etc.
  • In case your requirement is to work for the recurrence intervals.
  • If you want to manage the task lifetimes.
  • If you have requirement to work with the multiple Azure Functions.

Different ways to Develop Durable Functions

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

Azure Portal

You can use the Azure Portal to develop Durable Functions. This is one of the best ways. You no need to install any software in your local machine and for you no need to take any headache 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 IDE for the development of Durable functions. Here you will get the Debugging options, Intelligence support that really makes the development experience easier. You will definitely enjoy working with Visual Studio. You need to install the  Azure development workload and Azure Functions and WebJob Tools Visual Studio extension along with the Visual Studio.

How To Create Durable Functions Using Visual Studio 2019

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

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

Select the Azure Functions project template and then click on the Next button.

How to create the Durable Functions using Visual Studio 2019

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

Create Durable Functions Using Visual Studio 2019

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.

How to create the Durable Functions using Visual Studio 2017

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

How To develop Durable Functions Using Visual Studio 2019

Below is the Azure Function code

Create the Durable Functions using Visual Studio 2017

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

Create the Durable Functions using Visual Studio

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

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
}
Develop Durable Functions Using Visual Studio 2019

Now our Azure Function project is ready, the next step is we need to enable the Durable Function options by enabling the Durable Function extensions.

How to Enable Durable Functions

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

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

How to Enable Durable Functions in Visual Studio 2019

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

Develop Durable Functions Using Visual Studio 2017

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

Develop Durable Functions Using Visual Studio

Now if you will click on the .csproj file by clicking on the Project name, you can find the reference has been added for the 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 able to 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 able to see i got the expected out as below.

How To Develop Durable Function Using Visual Studio 2019

You may also like following the below articles

Wrapping Up

Well, In this article, we discussed What Are Azure Durable Functions, Why Durable Functions, Supported Languages for Durable Functions, Key Benefits of Durable Functions, and we also discussed Different Patterns Of Durable Functions, When To Use Durable Functions, How To Create Durable Functions Using Visual Studio 2019.

Leave a Comment