Durable Functions - Patterns - Fan Out And Fan In

Introduction

  • This is a series of articles to get our hands dirty with Durable Functions and their patterns in detail
  • Please refer to my previous article to understand the basics of durable functions
  • In this article, we will cover the Fan-out/fan-in pattern

It will cover the following things,

  • Application Patterns
  • Pre-requisites
  • What is a Fan-out/fan-in pattern?
  • Hands-on Lab – Create durable function using Fan-out/fan-in pattern in C#
  • Hands-on Lab – Deploy a durable function using Fan-out/fan-in pattern on Azure

Application patterns

The following application patterns can benefit from Durable Functions,

  • Chaining
  • Fan-out/fan-in(COVERED in this article)
  • Async HTTP APIs
  • Monitoring
  • Human interaction
  • Aggregator (stateful entities)

Pre-requisites

These are pre-requisites to start working on durable functions,

  • An Azure pay-as-you-go account enabled
  • Install Visual Studio 2019
  • Install Azure Storage Emulator

What is a Fan-out/fan-in pattern?

In the fan-out/fan-in pattern, we execute multiple functions in parallel and then wait for all functions to finish. Often, some aggregation work is done on the results that are returned from the functions.

Durable Functions - Patterns - Fan out and fan in

Hands-on Lab – Create a durable function in C#

Steps to be followed,

Open VS 2019 and create a project

Choose Azure function template and create a directory

Durable Functions - Patterns - Fan out and fan in

Choose durable functions orchestration template.

Durable Functions - Patterns - Fan out and fan in

Once the application has been created, we will add these functions,

  • Fan-in/Fan-out – Main functionality function which internally calls multiple functions in parallel and aggregates the response to pass into another function to return the result.
  • Function1_HttpStart – Function in the form of HTTP trigger which is responsible for initiating Fan-in/Fan-out and gather the response.

Durable Functions - Patterns - Fan out and fan in

Durable Functions - Patterns - Fan out and fan in

In the above example, the fan-out work is distributed to multiple instances of the F2 function. The work is tracked by using a dynamic list of tasks. Task.WhenAll is called to wait for all the called functions to finish. Then, the F2 function outputs are aggregated from the dynamic task list and passed to the F3 function.

Run (F5) the application and see all the functions that have been listed here,

Durable Functions - Patterns - Fan out and fan in

Copy the URL of Function1_HttpStart and run it in the browser.

Durable Functions - Patterns - Fan out and fan in

Copy the URL of statusQueryGetUri (GET URL) and run it in the browser.

Durable Functions - Patterns - Fan out and fan in

Here, you can see the response from FanOutFanIn which got instantiated from Function1_HttpStart with the desired output where I am capturing the sum of all the parallel function’s result and then passing it to the third function which can either manipulate it or return the same value.

Hands-on Lab – Deploy durable function on Azure

Steps to be followed,

Right-click on the function and click on publish and choose target as Azure.

Durable Functions - Patterns - Fan out and fan in

Choose an option as Azure function app (Windows).

Durable Functions - Patterns - Fan out and fan in

Create/choose the new/existing resource group and resource for azure function deployment.

Durable Functions - Patterns - Fan out and fan in

Click on Finish and let it get published and once published, go to the Azure portal and test the function app using the steps which we did in the above steps while creating durable functions.

This is a basic example of Fan-out/fan-in, where we run parallel functions and aggregate its response to generate output.

There are multiple other patterns that represent where to use duration functions, stay tuned for the next series of articles to get the hang of it.

Happy learning!


Similar Articles