Brief Article on Durable Functions

Oh, Boy New Thing To Learn!

Well, we can totally understand your feeling developers, you must be thinking that now what the heck is a Durable Functions and why you need to study it. The Durable Functions is useful for the folks involved with the serverless architecture. It is an extension of azure Functions which allows users to write stateful execution in numerous serverless projects. There are few huge benefits of Serverless Functions present, such as;

  • It is cheap
  • It can be scaled
  • It can write event-based code

Durable Functions

So, let’s first discuss the last benefit, when you write event-based code, then you can break your operations into smaller functions. You don’t have to struggle with infrastructure as it will automatically adjust for you. In this case, you can break your work into smaller and reusable parts which makes your workflow run smoothly. So, when your workload is reduced then you can focus on the business logic easily.

In this equation comes the Durable Functions, because when you’re application is divided into smaller parts, then you need more functions to run them. And, in some cases, you need to coordinate with them and define the order in which they should run. In Azure, Durable Functions are the pattern available that handle such task as other services have variations on this theme. Like, AWS version is called Step Functions.

Durable Functions With Example

Let’s assume you are selling train tickets online. So, you can imagine as a person buys a ticket, we need to;

  • Availability of ticket
  • Request seat map
  • Get mileage points as a loyalty member, etc

Sometimes these will run exactly, but in a large number of cases it won’t. Sometimes a user wants to first purchase the ticket and then check the rewards or it can work another way also. Then, you have to do some coding magic to make sure that no one understands your algorithm behind the reward program.

Orchestrator Functions

It doesn’t matter whether you are running functions at same moments or in the same order; you need to understand a function called orchestrator first. It is a function which understands your workflow, do as you want; it basically orchestrating other functions. Without Durable Functions, you face disorganization problem. It is pretty easy to get into something that resembles callback hell, and debugging can get really tricky.

So, orchestrator functions, on the contrary, manages the state and timing of all the other functions. Let’s go over some possible patterns to understand the functions properly.

Pattern 1: Function chaining

This is the most straight implementation of the pattern where literally one orchestrator controls few steps. Orchestrator starts a function and then register the complete function. The code for this pattern is;

const df = require(“durable-functions”)

 

module.exports = df(function*(ctx) {

 const x = yield ctx.df.callActivityAsync(‘fn1’)

 const y = yield ctx.df.callActivityAsync(‘fn2’, x)

 const z = yield ctx.df.callActivityAsync(‘fn3’, y)

 return yield ctx.df.callActivityAsync(‘fn3’, z)

})

Pattern 2: Fan out/fan in

To implement the multiple functions in parallel, you need the assistance of fan out/ fan in pattern. Which looks like;

const df = require(‘durable-functions’)

 

module.exports = df(function*(ctx) {

 const tasks = []

 

 // items to process concurrently, added to an array

 const taskItems = yield ctx.df.callActivityAsync(‘fn1’)

 taskItems.forEach(item => tasks.push(ctx.df.callActivityAsync(‘fn2’, item))

 yield ctx.df.task.all(tasks)

 

 // send results to the last function for processing

 yield ctx.df.callActivityAsync(‘fn3’, tasks)

})

Pattern 3: Async HTTP APIs

Sometimes you need to make a request to an API for an unknown amount of time. In that case, function to be fired when the first few APIs are completed. This pattern operates like;

 

const df = require(‘durable-functions’)

 

module.exports = df(async ctx => {

 const fn1 = ctx.df.callActivityAsync(‘fn1’)

 const fn2 = ctx.df.callActivityAsync(‘fn2’)

 

 // the responses come in and wait for both to be resolved

 await fn1

 await fn2

 

 // then this one this one is called

 await ctx.df.callActivityAsync(‘fn3’)

})

It’s A Start

If you are thinking that the learning Durable Functions is finished here, then my friend, there are more to learn. There are plenty of different shades of the Durable Functions available. So, stay tuned, you never know when we will share another dynamic article on in it again.

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href=""> <abbr> <acronym> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Send a Message