Azure Durable Functions - Developing Serverless Stateful Workflow
Tags: azuredurablefunctions, azurefunctions, serverless, statefulworkflows
Published: 2021-02-26 15:56:09

# Knowledge Sharing Azure Durable Functions at my workplace 

I had an opportunity to share technical knowledge with my colleagues at [Forefront Consulting](http://forefront.se/) during our monthly meeting in our division Technology. I was glad to share insights and knowledge about the basics of one of my [favorite Azure cloud services](https://medium.com/@jonah.andersson/a-net-software-developers-top-6-favorite-microsoft-azure-technologies-45f945f72605?WT.mc_id=AZ-MVP-5004251), Azure Durable Functions. Interesting questions came up that are worth checking out more. 

#What is Serverless and What are Azure Durable Functions? 

Well, if you have not worked with Azure development or is not familiar with Azure technologies, probably this article is jargon to you. It would be hard to understand right away what it is. However, if you are curious what it is and how it can help solve some of your application development problems, this cloud service (if you are working with Azure) is worth checking out!

I hope that this article will enlighten you and give you the basics of it. I personally like to share that this great Serverless technology exists!

#What is Serverless?

**Serverless Computing** is starting to gain its popularity these days, especially in the world of cloud development. 

> Serverless computing (or serverless for short), is an execution model where the cloud provider (AWS, Azure, or Google Cloud) is responsible for executing a piece of code by dynamically allocating the resources. And only charging for the amount of resources used to run the code. The code is typically run inside stateless containers that can be triggered by a variety of events including http requests, database events, queuing services, monitoring alerts, file uploads, scheduled events (cron jobs), etc. The code that is sent to the cloud provider for execution is usually in the form of a function. Hence serverless is sometimes referred to as “Functions as a Service” or “FaaS”. Following are the FaaS offerings of the major cloud providers:
AWS: AWS Lambda
Microsoft Azure: Azure Functions
Google Cloud: Cloud Functions
While serverless abstracts the underlying infrastructure away from the developer, servers are still involved in executing our functions.(Source:[Serverless-Stack.com](https://serverless-stack.com/chapters/what-is-serverless.html))

So, Serverless is not really without servers. The infrastructure exists - there are servers behind "Serverless". The only big difference is that the cloud provider like [Microsoft Azure](https://azure.microsoft.com/en-us/) takes care of it for you so that you, your organization and development team can focus on solving and creating solutions with these cloud services like Durable Functions. 

#What are Azure Durable Functions? 

**Azure Durable Functions** is an extension of [Azure Functions] (https://docs.microsoft.com/en-us/azure/azure-functions?WT.mc_id=AZ-MVP-5004251) 

>Azure Functions - a serverless solution that allows you to write less code, maintain less infrastructure, and save on costs. Instead of worrying about deploying and maintaining servers, the cloud infrastructure provides all the up-to-date resources needed to keep your applications running. You focus on the pieces of code that matter most to you, and Microsoft Azure Functions handle the rest.

Azure Durable Functions on the other hand are built on top of Azure Functions. It does more than standard Azure Functions can do, especially when you have to develop a serverless long-running stateful workflow from the code along with the Azure Functions in a [serverless](https://en.wikipedia.org/wiki/Serverless_computing) environment. 

#Major Benefits of Using Azure Durable Functions 

* Define and write workflows by code 
* Create long-running tasks 
* Solves the need to work with multiple Azure Functions
* Create serverless stateful workflows that require human interaction, wait for external events, monitoring, aggregation and more
* Solve the need to make sure that the status of your workflow is consistent
* Focus on developer productivity 
* Auto-scaling capabilities 
* Pay only for what you use (Check out [Azure Pricing Calculator](https://azure.microsoft.com/en-au/pricing/calculator?WT.mc_id=AZ-MVP-5004251))
* Integration with other cloud services and APIs

#Azure Durable Task Framework - The Technology Behind

>Behind the scenes, the Durable Functions extension is built on top of the [Durable Task Framework](https://github.com/Azure/durabletask), an open-source library on GitHub that's used to build workflows in code. 
Like Azure Functions is the serverless evolution of Azure WebJobs, Durable Functions is the serverless evolution of the Durable Task Framework. Microsoft and other organizations use the Durable Task Framework extensively to automate mission-critical processes. It's a natural fit for the serverless Azure Functions environment.

#Major Components in Azure Durable Functions 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbzsdxwfju6px82p7qox.png)

**Client functions** are the entry point for creating an instance of a Durable Functions orchestration. They are the starting point of the entire process. Client Functions in Azure Durable Functions can run in response to an event from several sources, an event message posted to queue, HTTP request, or event stream event triggers, and more. Client functions can be written in different programming languages available and supported. 

Below is an example of an HTTP Trigger Client that kick starts the orchestrator.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rtqliejncomnjyq5bhy.PNG)

**Orchestrator functions** describe how actions are executed. It is an important part of Durable Functions. The workflow of actions should be in the order in which they are to run. When you write the orchestration logic in code, it is important that these functions are deterministic. 

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vd36dkj65cx74e1oxnw.png)

One great thing about the orchestrator is that it sleeps! 
When it sleeps you do not get charge for the service. Stateful workflows solve some problems that normal Azure Functions cannot.
For function chaining pattern for example, the orchestrator sleep while the current activity is doing its tasks. When it is finished, then orchestrator wakes up to start the next activity and so on. 

Here is my example of how a simple workflow code looks in an orchestrator.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqtmb2lwu243e17k1bii.png)

Learn more about [Deterministic APIs] (Orchestrator logic must be deterministic because the whole function will be “replayed” 
More info about Deterministic API’s: 
https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-code-constraints#using-deterministic-apis	
?WT.mc_id=AZ-MVP-5004251 
)


**Activity functions** are the basic units of work in a durable function orchestration. An activity function contains the actual work performed by the tasks being orchestrated.

Entity functions define operations for reading and updating small pieces of state, known as durable entities. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exmaj4ovp9duyq7ieprh.PNG)

**Entity Functions** is a recent addition to Azure Durable Functions. Although these feature is only available in Durable Functions 2.0 and above. They are currently supported in .NET and JavaScript (as of this date this articles publication).

> Entity functions define operations for reading and updating small pieces of state, known as durable entities. Like orchestrator functions, entity functions are functions with a special trigger type, the entity trigger. Unlike orchestrator functions, entity functions manage the state of an entity explicitly, rather than implicitly representing state via control flow. Entities provide a means for scaling out applications by distributing the work across many entities, each with a modestly sized state. Read more about [Entity Functions] (https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-entities?tabs=csharp?WT.mc_id=AZ-MVP-5004251). 

#Durable Functions Patterns

Durable Functions have different known patterns that are worth checking out. 

* Function Chaining
* Fan out/Fan in 
* Async HTTP APIs
* Monitor 
* Human Interaction 

Read more about it in depth [Azure Durable Functions Patterns](https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview?tabs=csharp?WT.mc_id=AZ-MVP-5004251)

#Best Practices of Developing Durable Functions (Orchestrator)

There are important things to consider when designing and creating your stateful workflows with Durable functions. As mentioned, orchestrator workflows must be deterministic.

Check out the DOs and DONTs when coding your workflow in your orchestrator function.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zjpmu4aa05z9np3bochj.png)

#Further Reading and Learning Resources 

* One of my tech talks - Azure Durable Fundamentals at Azure User Group Sundsvall Sweden https://youtu.be/fDej9n-kzNM
* Microsoft Azure Functions Documentation
 https://docs.microsoft.com/en-us/azure/azure-functions?WT.mc_id=AZ-MVP-5004251 
* Microsoft Azure Durable Functions Documentation
  https://docs.microsoft.com/en-us/azure/azure-functions/durable?WT.mc_id=AZ-MVP-5004251 
* Azure Core Tools 
https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash?WT.mc_id=AZ-MVP-5004251  
* Channel9 on Building Workflows with Durable Task Framework https://channel9.msdn.com/Shows/On-NET/Building-workflows-with-the-Durable-Task-Framework?WT.mc_id=AZ-MVP-5004251 
* Azure Functions University on YouTube https://youtube.com/c/marcduiker-serverless
* Pluralsight Course ”Azure Durable Fundamentals” by Mark Heath 

I hope you learned something and became interested to learn more.

If you like this article, please re-share the knowledge! There’s power in sharing knowledge. 

Feel free to leave feedback and ask questions. Connect! 

-----------------------------

**About Author:** Jonah Andersson is a Filipina-Swedish Software Engineer who codes full stack system development in C# .NET, Microsoft Certified Azure Developer, Microsoft MVP for Azure, Mentor and AzureHeroes Inclusive Leader. She currently works as a Software Developer Consultant at Forefront Consulting in Sweden. Jonah is passionate about continuous learning, developing and sharing knowledge about Microsoft Azure cloud technologies. A woman who codes in tech who enjoys writing, advocating gender equality in the tech industry through mentorship and by being role model on her spare time.

Follow me [Twitter](https://twitter.com/cjkodare), connect via my [LinkedIn](https://linkedin.com/in/jonahandersson), visit my [website](https://jonahandersson.tech/)