Unlocking the Power of Azure Durable Functions: Simplifying Serverless Complexity
Monik Bhalodiya
ASP.NET | C# | MVC | .Net Core | MS Sql | EF | Web API | Angular | Azure DevOps | Azure Functions | Azure Logic Apps | Azure Services | Ajax | Jquery | HTML | CSS | JavaScript | Bootstrap
Microsoft Azure offers a serverless service called Azure Functions, which is great for running small pieces of code in the cloud. However, when developers need to manage state, handle long-running tasks, or organize more complex processes, they might need more. That's where Azure Durable Functions help. They are an extension of Azure Functions that allow for managing state, tracking progress, and restarting workflows when needed. Durable Functions are especially helpful for managing tasks with multiple steps, making them a valuable tool for Azure developers.
Durable Functions are mainly used to make managing complex tasks easier in serverless applications. Here are some common ways they help:
Function chaining (Linking tasks in a sequence)
In the function chaining pattern, a series of functions run one after the other. Each function's output is passed as input to the next function. Queues between these functions help keep the system strong and scalable, even though control moves from one function to the next.
In the above example, F1, F2, F3, and F4 are the names of different functions in the same application. The flow of the code follows regular programming rules, running from top to bottom. You can also use things like conditionals, loops, and error handling (try/catch/finally) in your code.
Fan-Out/Fan-In (Running tasks in parallel and then combining the results)
In the fan out/fan in pattern, you run several functions at the same time and then wait for all of them to complete. Afterward, you usually combine or process the results from these functions.
With regular functions, you can "fan out" by having the function send several messages to a queue. However, "fanning in" is harder. To do this, you need to write code to track when the functions triggered by the queue finish, and then save their results.
Async HTTP APIs (Handling long-running tasks triggered by web requests)
The async HTTP API pattern helps manage long-running tasks and their status for external clients. Typically, this pattern works by having an HTTP endpoint start the long task, and then sending the client to a status page where they can check to see when the task is complete.
Durable Functions makes it easier to work with long-running tasks by handling much of the code for you. For example, the Durable Functions quick start samples (in C#, JavaScript, TypeScript, Python, PowerShell, and Java) show a simple REST command to start a new orchestrator function. Once the function starts, it provides webhook APIs to check the status of the function.
Monitoring (Keeping an eye on things continuously)
The monitor pattern is a flexible process that repeats in a workflow, like checking until certain conditions are met. A basic timer can handle simple tasks, like cleaning up periodically, but its timing is fixed and can be hard to manage. Durable Functions allow for flexible timing, better management of tasks, and multiple monitors from one process. For example, instead of letting a client monitor a long task, the task itself can check an external source and wait for updates.
Durable Functions let you easily create multiple monitors that watch certain endpoints. The monitors stop when a condition is met or can be ended by another function. You can also adjust the monitor's wait time based on conditions, like exponential backoff.
领英推荐
Human interaction (Handling tasks that need human approval or input)
Many automated processes need human involvement, but humans are not as available as machines. To handle this, processes can use timeouts and backup plans. For example, in an approval process, if a manager doesn't approve an expense report in 72 hours, the system escalates the request to a higher authority, like the manager's boss.
You can use an orchestrator function to implement this pattern. It uses a timer to ask for approval and escalates if there's a timeout. The orchestrator waits for an external event, like a notification from a person.
Aggregator or stateful entities (Managing data over time in an organized way)
The sixth pattern is about combining event data from different sources over time into one entity. The data may arrive in batches or over long periods. The aggregator may need to act on the data as it comes, and clients might need to query the combined data.
The challenge with using stateless functions is managing concurrency. You have to prevent multiple threads from changing the same data at once and make sure the aggregator runs on only one VM.
Key Benefits of Using Durable Functions for Workflow Management
Challenges and Limitations of Using Azure Durable Functions
Real-World Use Cases of Azure Durable Functions
Methods for Developing Durable Azure Functions
Azure portal: You can create functions directly in the Azure portal, which is handy for testing without needing to install anything on your computer. It's good for trying things out, but not ideal for creating production-level code.?
Command line: You can develop Azure Functions from the command line using any platform and your preferred text editor, like Visual Studio Code, along with the Azure Functions command-line tools.
Visual Studio: Visual Studio has an Azure Functions extension with templates for creating functions and projects, plus it provides useful features like debugging and code suggestions.