I was always puzzled about this thing when I was a newbie to Azure. What this thing really is and why its called Durable?

Turns out, its not that complex. Well, you can always read about it in the official Microsoft docs, they are basically a spin off of Azure functions to persist outputs of complex workflows you might have in your business logic . We all know, Functions are stateless, but what if you want to implement sequential or a parallel workflow where final outputs are dependent on the previous history of outputs given by Functions.You can easily use the Durable Functions to chain your Azure functions in a meaningful way. It gives an impressions of a stateful workflow over a serverless architecture.

There are various chaining patterns in this link, please go through it and get amazed by the wonderful things Durable functions can achieve.

My curiosity about Durable functions , lead me to write a very simple naive example to implement it. I will explain how it works and what wonderful things go behind it.

I will explain 2 patterns here, Sequential and Fan in Parallel (but there are many others, please see the above link). I will take an easier scenario, concatenating 2 strings to get a third string.

Sequential:

It looks like below:

Sequential execution

Here are the components for Durable Function for above workflow. These components might change as and when you implement different patterns, but some of the below will always be present in your pattern.

Starter function (Start): Our function which will get the request body, start the Orchestration function, get the status of the Orchestration function and return output.

Our function which will get the request body, start the Orchestration function, get the status of the Orchestration function and return output. Orchestration Function (Run in Sequence): A durable function starts with triggering of orchestration function. This is the main function which triggers all subordinate functions. This function maintains the state between various functions, by writing the history to Azure storage account. Every function signals its completion, by passing the state to the orchestration function. This is the most important component which will always be present.

A durable function starts with triggering of orchestration function. This is the main function which triggers all subordinate functions. This function maintains the state between various functions, by writing the history to Azure storage account. Every function signals its completion, by passing the state to the orchestration function. This is the most important component which will always be present. Sub ordinate functions(RunFuncktion1 and RunFunktion2 and CombineStrings) : These function perform the actual business logic and give the output to the Orchestration Function.

: These function perform the actual business logic and give the output to the Orchestration Function. Azure Storage Account: You need a storage account to store the details of the various functions and its output so that state can be persisted.

Code for Starter function:

Starter function

We read the request, start the Orchestration using client, get the status of the Orchestration using the unique Instance ID and return the response. Our starter function will have the url:

[GET,POST] http://localhost:7071/api/DurableFunctionsTest_HttpStart

Code for Orchestration Functtion:

RunInSequence

We get the content as String and deserialize the data. Then, we call the sub ordinate functions with the context passed in Starter function using CallActivityAsync and return the output. Note, the use of await keyword. The await keyword makes the Orchestrator function non blocking(what happens behind the scenes will be explained soon).

Code for subordinate functions

Sub ordinate functions

These are simple string returning functions. Not much to explain.

Write all the above code in your favorite editor, configure a storage account in the local.settings.json file and run the above using func host start.

When you run the starter function, by making a POST request to the url as below, you get the below output.

Output

Pick up the statusQueryGetUri url, and put it in a browser, you will see the below output. The output tag has the concatenated string (raunak narooka) and it also shows what input was passed.