Azure Durable Functions #1 Introduction

Azure Durable Functions are extension of Azure functions which let you write stateful functions in a serverless environment.

Let’s discuss some key points about durable functions in this post.

Why Durable Functions?

Azure Durable functions came in to picture to solve some scenarios which was difficult to solve using regular azure functions.

Issue 1 : Solving Fan in Fan Out Pattern

Suppose you have an option in your application where user can select a particular customer and select to generate the invoice for a particular period and send customer and email. Customer would have around 50-75 orders and orders will have custom logics to handle based on order type. So you need to perform logic and calculate the order amount then sum the total amount.

So basically you need to achieve following scenario. Solving such a pattern is not that easy with regular function.


Issue 2 : Exception Handling in Function chain.


You have a function chain. where you use azure queue to pass out put of a one function to another. Suppose an error occurred in a middle function in the chain. So it is not that a easy task to rollback the whole flow unless we have state saving mechanism implemented.


Above are some example where, why we need to use Azure durable functions over regular azure function.

How Durable Functions Solve Above Issues

Mainly in durable functions we have following 2 parties.

1. Orchestrator Function

This is  new type of function which will manage the state for you. In Orchestrator function you can define the workflow in the code it self. When an orchestrator function is triggered.
  • It will trigger the first activity in the workflow and go to sleep
  • When that activity is completed it will automatically wakes up and call the next activity in the workflow
  • When calling the next activity orchestrator can pass the output from the previous activity as it maintains the state.

2. Activity function

  • This is a normal azure function which will perform a single action in the workflow.
  • It will take inputs from the orchestrator function and once completed it will pass the out put to the orchestrator function again.
Following diagram will show you how data will be passed on a durable function.



Suppose you have a workflow where you have 2 steps. Output of the first step need to be passed in to the second step. So in Durable functions it will
  1. Http Trigger will call the orchestrator
  2. Then Orchestrator call Function 1
  3. Function 1 return it’s out put to orchestrator
  4. Then orchestrator call Function 2 passing the out put of Activity 1

How It manages State

Though we should not worry about much how behind the scene this works it is better to have a high level idea how it manages the state.

1. Storage Account

When you create a durable function you need to give a connection string to a  storage account which will be used by the durable function runtime to mange it state.
  • Storage Queue – Storage queue is used as the communication mechanism to trigger the next function
  • Storage Table – Store the state of the orchestrator function

2. Event Sourcing

When storing the state runtime uses pattern called event souring. What is pattern does is rather saving the current state is will save all the actions let up to that state. In this pattern
  • It will always append a new row to the table
  • it will store the full history
Following diagram shows an example on how this event sourcing would save the state.



3. Task Hub

A task hub in Durable Functions is a logical container for Azure Storage resources that are used for orchestrations. Orchestrator and activity functions can only interact with each other when they belong to the same task hub.
If multiple function apps share a storage account, each function app must be configured with a separate task hub name. A storage account can contain multiple task hubs. The following diagram illustrates one task hub per function app in shared and dedicated storage accounts.

Benefits

1. It allow to Define workflow in code






In durable functions whole workflow will be handled by a function called “Orchestrator Function”. It will manage the state of all the activities in the workflow. So it is very easy to understand and picture the overall picture of the

2. Error handling is possible at any point.

As there is a single place which manages the workflow with the state we can handle errors at any point. Durable functions are coming with exception handling, rollback mechanisms and timeout handling.

3. It allows you to check the workflow or cancel workflow at any point.

4. State management is fully handled so you don’t need to worry about it


I’ll discuss what are patterns we can use durable functions in next posts.



Comments

Popular posts from this blog

Responsive Web Design

Affine Cipher in C#

Contract First Development in WCF 4.5