Azure Durable Functions

Introduction

Azure Durable Functions is an extension built on top of Azure Functions or Azure Web jobs to help us maintain and orchestrate a long running durable workflows using Azure Functions. This is developed using Durable Task Framework and is Open Source.

Components

There are three main components/functions in Durable Function

  • Client Function – Function that takes the trigger to start an orchestration
  • Orchestration Function – Function that manages or coordinates the sequence of activity functions
  • Activity Function – Unit of work that helps in writing business logic that can follow single responsibility principle.

Workflows

Durable functions help us create complex stateful workflows and some of the common workflow patterns are described here.

FanOut-FanIn Example

Code is available here

Lets build a simple workflow using that follows the workflow pattern – FanOut and FanIn. This pattern involves calling one or more activity functions in parallel and aggregate the result in the orchestration function.

Workflow is represented in the image below,

Workflow to Book a vacation

  • Trigger to book a vacation – Client Function
  • Book Vacation – Orchestration Function
  • Book hotel – Activity Function F1
  • Book Flight – Activity Function F2
  • Compute total expense at the end incurred.- Activity Function F3

Prerequisites

Create Azure Durable Functions in VS Code

Follow the below steps to create default orchestration workflow in VS Code and we can add activity functions as described above,

  1. In a newly created workspace folder in VS Code , Press CTRL + SHIFT+P to select

2. Select the workspace folder

3. Select the preferred language

4 Select the Durable Functions Orchestration from the template

5. Provide the function name and name space name in subsequent workflow

6 Use either Azure storage account or local emulator in the pop-up that comes to select the storage account. Storage account is essential to maintain the state of the durable function.

7. This would have created a default template flow. Lets add/modify the components to solve the use case we have taken. The source code is available here

Client Function

Vacation Info – Local POCO class to store and pass the input.

Orchestration Function

Call two activity function – Book Hotel and Book Flight In parallel and wait for the result. Pass the result to Compute Total Activity Function.

Activity Function

This is one of the activity function

Conclusion

Run/Debug the function ,which is in the sample an HTTP Trigger to Client Orchestration. Once the function is started locally, trigger it like passing an input below,

POST http://localhost:7071/api/TriggerBookVacation HTTP/1.1
content-type: application/json

{
    "CityName" : "Singapore",
    "HotelName" : "Hyatt",
    "AirlinesName" : "AirAsia"
}

This gives a list of endpoints which to query the status of orchestration function, pass an event ta orchestration, or to cancel the orcehstration flow.

This sample. lets run the orchestration status endpoint to get the output .

Output

HTTP/1.1 200 OK
Connection: close
Date: Sun, 03 May 2020 13:48:01 GMT
Content-Type: application/json; charset=utf-8
Server: Kestrel
Content-Length: 292

{
  "name": "BookVacation",
  "instanceId": "bed9f8491d764779b3b977de3d493e74",
  "runtimeStatus": "Completed",
  "input": {
    "cityName": "Singapore",
    "hotelName": "Hyatt",
    "airlinesName": "AirAsia"
  },
  "customStatus": null,
  "output": 17000.0,
  "createdTime": "2020-05-03T13:45:53Z",
  "lastUpdatedTime": "2020-05-03T13:46:03Z"
}