Home NewsX Azure Durable Functions: FaaS for Stateful Logic and Complex Workflows

Azure Durable Functions: FaaS for Stateful Logic and Complex Workflows

by info.odysseyx@gmail.com
0 comment 4 views


Function as a Service (FaaS) It empowers developers to do their jobs better by providing a fast, scalable way to run tasks in response to events without having to manage infrastructure. function Transforming inputs into outputs makes it a lightweight choice for scheduled tasks, advance notifications, order processing, and other tasks that do not require more complex fault tolerance than simple retries. Plus, functions run on infrastructure in the cloud host, scaling up and down based on demand, and you only pay for the resources you consume.

But what if you have a workflow that requires more reliability and fault tolerance than short-lived functions can provide? What if your scenario involves long-running tasks, or your business logic requires the output of one function to be used as input to another?

It’s time for a solution designed to orchestrate higher-level workflows, but without having to sacrifice the convenience of FaaS.

Traditionally, when developers wanted to orchestrate complex state logic or workflows, they used: Database And queues to help track application state. Whether you use traditional infrastructure or a cloud service, this approach requires writing, testing, and managing scalable logic to create, test, and manage checkpoints, manage state, handle retries, and recover from failures.

Message queues provide a level of durability by isolating downstream users from the source data. However, managing this additional infrastructure and the complex logic to achieve fault tolerance adds time and complexity. In contrast, Azure Durable Functions handles the details, including state tracking, automatic retries, failure recovery, and load balancing. You can focus on your business logic while getting the added benefits of managed infrastructure, automatic scaling, and pay-as-you-go pricing.

Durable features provide the best of both worlds. Benefits of FaaS The Azure Functions platform lets you write code that essentially builds complex state logic. Serverless computing environment. It allows for very sophisticated stateful scenarios to be implemented without the overhead of traditional development models.

Complex state execution simplified

Durable functions can maintain state between function calls. This means that durable functions can be used to drive software patterns that were not designed for single-purpose functions. For example, you can design applications that wait for human interaction or use parallel processing to improve concurrency and reduce processing times.

Fan-out/fan-in is a common pattern where multiple functions are run in parallel, fanned out across multiple machines (other than those used by the app), and throughput is increased. The results are then combined and fanned in.

lilima_1-1725653200549.png

In a fan-out/fan-in pattern, a function app (F1) is fanned out when multiple functions are executed in parallel (F2) and fanned in when all functions are completed (F3).

A single-purpose function can fan out by sending multiple messages to a queue. However, to fan in, you need to write code to track when the queue-triggered function ends and to store the function output. This starts to get complicated. It’s easier to do this with a durable function using relatively simple code.

For example, the following code concisely implements the fan-out/fan-in pattern:

[Function("FanOutFanIn")] 
public static async Task Run( 
 [OrchestrationTrigger] TaskOrchestrationContext context) 
{ 
   var parallelTasks = new List>(); 
 
   // Get a list of N work items to process in parallel. 
   object[] workBatch = await context.CallActivityAsync

Code isn’t the only way to connect the dots. Microsoft offers a powerful low-code orchestrator in Azure Logic Apps, which provides an extensive library of pre-built connectors to integrate with other services.

But the advantage of a code-centric solution like durable functions is the added control and customization. Some C# uses JavaScript, TypeScript, Python, Java or Powershell Code lets you define and execute more complex workflows, with precise control over concurrency, execution error handling, and more.

Components of a durable function app

The basic unit of work of a durable function is: Activity function. These short-lived, stateless functions perform a single task, but can be orchestrated into more complex processes. For example, an e-commerce site might use an activity function to check inventory, another function to charge a customer, and another function to create a shipment. But processing an entire order requires a more advanced workflow.

That’s the role Orchestrator functions. As the name suggests, orchestrator functions describe how work is executed and in what order. They act as stateful glue, connecting activities and other orchestrator functions (sub-orchestration). The state of an orchestrator is implicitly maintained by an underlying framework of durable functions that track pending work, completed work, and intermediate results. A platform that maintains orchestrator state means that if a process crashes, it can be restarted from the point of failure rather than from the beginning. Needless to say, this is more efficient in terms of time and cost.

Orchestrators can also wait and listen for external events, making them useful for handling human interaction or other external triggers. In exchange for following some coding constraints, you can use orchestrator functions to execute powerful workflows.

Serverless State Objects: Durable Entities

Durable functions provide another elegant way to introduce stateful functionality to your applications. Entity Function. It allows for easy representation of stateful objects in a distributed environment. Entity Function. Like an object, each entity instance has a unique identifier that can be used to explicitly read and manipulate its internal state. Like an object interface, an entity defines a list of operations that limit the management of its internal state.

For example, an entity function could represent a counter or a user object. The following code shows a simple implementation. Counter Entity. There are three operations:add, resetand get) can be used to manipulate the state. Counter Entity. In a product like a smartwatch, there may be multiple instances of this. Counter Each entity stores the state of a specific user, such as the number of steps taken.

public class Counter
{
    public int CurrentValue { get; set; }

    public void Add(int amount) => this.CurrentValue += amount;

    public void Reset() => this.CurrentValue = 0;

    public int Get() => this.CurrentValue;

    [Function(nameof(Counter))]
    public static Task RunEntityAsync([EntityTrigger]TaskEntityDispatcher dispatcher)
    {
        return dispatcher.DispatchAsync();
    }
}

Durable entities make state management easy and intuitive. Another example is using durable entities to maintain and manage the state of Internet of Things (IoT) devices. For more scenarios, see: Practice of durable entities and Serverless circuit breaker with durable entities.

When using durable functions

You can mix and match function types to create sophisticated solutions such as data processing pipelines, large-scale extract, transform, load (ETL), business workflows, asynchronous APIs, and batch jobs. See sample architectures:

Next generation feature services

FaaS has brought about a game-changing change for programmers, and Durable Functions gives you much more control over the development and management of serverless workflows.

As part of Azure Functions PlatformDurable Functions provides high-performance scaling and Azure-native integration, and also lets you express state logic and workflows as code. You can run activities or sub-orchestration in parallel, configure automatic retries with backoff for greater resiliency, and easily set timeouts for your workflows or cancel them entirely. All of this is done automatically by Durable Functions, managing the state of your workflows.

Get end-to-end workflows up and running quickly with the scalability of the Azure Functions platform. Try this example to see for yourself.





Source link

You may also like

Leave a Comment

Our Company

Welcome to OdysseyX, your one-stop destination for the latest news and opportunities across various domains.

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

Laest News

@2024 – All Right Reserved. Designed and Developed by OdysseyX