This content originally appeared on Level Up Coding - Medium and was authored by Stefano Giannattasio
Sometimes stateless Azure Functions cannot help you design a serverless architecture for complex solutions. This is where Durable Functions come to your aid.
Durable Functions is an extension of Azure Functions through which you can define a stateful workflow using one or more orchestrator functions. You can focus on business logic without worrying about managing states and checkpoints.
In this article, before starting to talk about common Durable Functions patterns, it is fundamental to explain (with few words) which types of durable functions exist and what is the role of each of them.
Function Types
With Durable Functions, you use different types of functions that work together to reach a common goal. Each function is intended to play a different role in the workflow.
- Orchestrator Function. As the name suggests, this type of function orchestrates the workflow. Here you decide how and when other functions are being called. Orchestrator functions have to be deterministic which means that the result can never change between subsequent executions, and therefore actions you can perform in this type of function are subject to constraints.
- Activity Function. You can think of an activity function as a worker invoked by the orchestrator that takes charge of a task and performs some activities in order to complete it.
- Entity Function. It provides a way to read and update small states. An entity has a unique identity and an internal state. You can refer to the entity through its identifier and read and update its state.
- Client Function. This is the entry point of your workflow. It is necessary to trigger an orchestrator or entity function. A client function uses the durable client output binding and can be triggered by all the available triggers as for classic Azure Functions.
Architectural Patterns
Basically, there are six typical types of patterns suitable for Durable Functions:
- Function chaining
- Fan-out/Fan-in
- Async HTTP APIs
- Monitoring
- Human interaction
- Aggregator
1. Function Chaining
This pattern is very simple and consists of chaining functions so that the output of one function is the input of the next one. It means executing functions in a specific order. This can be obtained by calling them inside the orchestrator where you can specify the chain of execution.
Working with asynchronous calls, every call has to use the await/yield keyword in order to wait for the response of a function before calling the next one.
2. Fan-out/Fan-in
If with the previous pattern you execute functions in sequence, this pattern is thought for parallel execution. You can easily fan out by executing multiple instances of an activity function asynchronously, similar to how you would do to execute multiple tasks in parallel. Then you just need to wait that each function ends and then aggregate results or continue with your workflow.
Inside the orchestrator, you have to track the state of the activity functions.
3. Async HTTP APIs
This is a built-in pattern, which is very useful when your workflow is composed of long-running operations. The idea behind this pattern is to provide as soon as the client function ends its execution, an endpoint through which you can check the status of the execution. Basically, the client function triggers the orchestrator and then returns a set of endpoints of webhook HTTP APIs exposed by the Durable Functions extension that you can use to query the status, send events or terminate the execution.
4. Monitor
The monitor pattern has been designed to automatize the process of status checking. In contrast with the Async HTTP APIs pattern, the client does not need to check the status of the orchestrator execution. Through a timer, you can define a polling rate and check at regular intervals when a specific condition is met.
You can define an activity function invoked when the condition you are checking for is verified and perform some action to inform the user, for example, you can use SendGrid to send an email or Twilio SMS service to send an SMS. Said that, you can however expose the webhook HTTP API to check the status of your monitor, and terminate the execution if needed.
5. Human Interaction
Sometimes your business logic may require human interactions. Handling them in an automatized process is often complex because you need to define some logic to wait for a response (humans are not responsive as machines) and to exit the execution or perform default operations in case the interaction does not arrive within a time limit.
A common example of this pattern is asking the user for a challenge to approve the execution. Also in this case you could use the Twilio SMS service to send a verification code and another function to expose a webhook API endpoint through which the user can send the code received.
6. Aggregator
This pattern leverages Entity functions to aggregate data coming from one or multiple sources like the Event Hub. You could receive data in batches or not over a long time period. Using this pattern you create an entity instance for the metric you want to aggregate and it holds the status.
A client should be able to query the aggregate values.
References
- Microsoft Azure documentation
Level Up Coding
Thanks for being a part of our community! Before you go:
- 👏 Clap for the story and follow the author 👉
- 📰 View more content in the Level Up Coding publication
- 🔔 Follow us: Twitter | LinkedIn | Newsletter
🚀👉 Placing developers like you at top startups and tech companies
Azure Durable Functions Patterns was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Stefano Giannattasio
Stefano Giannattasio | Sciencx (2022-11-06T22:23:04+00:00) Azure Durable Functions Patterns. Retrieved from https://www.scien.cx/2022/11/06/azure-durable-functions-patterns/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.