Azure Durable Functions
Azure Durable Functions is a programming model based on Microsoft serverless’ platform Azure Functions and therefore, has additional benefits over a standard function, such as state management, retry activities, and being able to easily cancel workflows that are still in progress. It allows you to write a workflow as code and have the execution run with the scalability and the reliability of serverless with high throughput.
Why Durable Functions?
This is very important to know, Why we should 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 like
- Retry activities
- Run activities in parallel
- Timeout workflows
- State management for free
- Check on workflow progress with REST API
- Develop and test locally
You can consider these concepts as the main components of the Durable Functions. Those play a vital role while working with the Azure Durable Function
- Orchestrator Client
- Orchestrator Function
- Activity Function
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 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.
The Orchestration Client is used to start the orchestrator function, and can also be used to check the status of the function, as well as terminate them.
The Orchestrator Function plays a major role while building the Durable function and the 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.
It automatically set a checkpoint during its execution, shut itself down while waiting for other functions to finish executing, then replays through the checkpoint to resume execution.
It is important to remember how an Orchestrator function operates differently than a standard function. While a normal function only executes once per event, the orchestrator is restarted many times while waiting for other functions to complete.
This kind of behavior means that this function needs to be deterministic. It must return the same result each time. It is crucial then to not use DateTime.Now, Guid.NewGuid() or anything generating a different result in this method
Not every function can be used with an orchestrator. Those functions become an Activity Function when an ActivityTriggerAttribute is on a DurableActivityContext parameter.
Activity Function is called 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 Functions VS Durable Functions
How To Create Durable Functions Using Visual Studio 2022
- Open the Visual Studio 2022 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.
- Now provide a project name and choose a location for your project and then click on the Create button.
- On the next window, select the Durable Function Orchestration, select the Storage Account (AzureWebJobsStorage) as Storage Emulator. Finally click on the Create button.
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.
- Below is the Azure Function code