Azure Durable Functions Tutorial

What Are Azure Durable Functions

In thisĀ Azure tutorial, we will discussĀ a complete Azure Durable Functions Tutorial.

Azure Durable Functions Tutorial

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

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.

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

The Azure Durable functions are capable of doing 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.

While deploying our Azure function to Azure, there is No headache for us thinking on the Infrastructure part, 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, that helps us to define the number of servers we need and it helps us to handle the Overall load.

Why Durable Functions?

This is very important to know. Why should we go with the Durable Functions? There is stuff that is difficult to implement with the regular Azure Function.

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 want to implement error handling in the code with the help of workflows, if we want to trigger parallel execution, etc

Durable Functions are stateful and help track the workflow status in each stage. This also helps to implement some advanced workflows for our business because of its 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, which is currently in Public Preview.

Main Components for Durable Functions

You can consider the below concepts as the main components for 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 is responsible for monitoring them. When you are creating 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 considered 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 the regular Azure Function, but we can perform those tasks efficiently with the help of the Durable functions. Let’s discuss a few key benefits of the Durable functions regarding the Workflow.

Build Workflows in Code

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

Parallel activities

The Azure function can help you perform multiple parallel activities in case 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

You can easily cancel the in-progress workflow with the help of Rest API with a Durable function. It was not at all accessible 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 functions quickly. 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.

durable functions

Supports Fan-out/fan-in

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

Benefits of Durable Functions

Asynchronous HTTP APIs

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

Monitoring

The following pattern for the Durable function is the Monitoring pattern that helps to enable 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 the case of business approval workflows. Suppose you will 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 develop 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 need to 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 create 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.

How to create the Durable Functions using Visual Studio 2019

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

Create Durable Functions Using Visual Studio 2019

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.

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 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

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 to enable the Durable Function options by enabling the Durable Function 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

How to Enable Durable Functions in Visual Studio 2019

Now, search for 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 to click on the I Accept button to accept the license agreement.

Develop Durable Functions Using Visual Studio

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 out as below.

How To Develop Durable Function Using Visual Studio 2019

Durable Functions Limitations

As we have already discussed, there are so many benefits that the Azure durable function provides us. 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 the non-deterministic APIs from the Azure Durable Functions. The API must be deterministic. This means the API always returns the same value by providing the same input irrespective of when or how frequently it gets 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 following the articles below

Wrapping Up

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