Azure Durable Functions Extension

Orchestrate and manage stateful workflows in Azure Functions

What is the Durable Functions Extension?

Azure Durable Functions is an extension of Azure Functions that enables you to write stateful functions in a serverless compute environment. It allows you to define and manage long-running, reliable, and complex workflows (orchestrations) that are crucial for many applications, such as asynchronous operations, human interaction, and event-driven processes.

Instead of managing state and execution manually, Durable Functions handle the complexities of state management, checkpoints, retries, and error handling for you, allowing you to focus on your business logic.

Key Features

  • Orchestrator Functions: Define the logic and control flow of your workflows using code (C#, JavaScript, Python, PowerShell).
  • Activity Functions: The individual units of work within an orchestration. They perform tasks like calling external services, database operations, or performing calculations.
  • State Management: Automatically tracks the execution state of your orchestrations, ensuring reliability and durability.
  • Checkpoints: Orchestrator functions automatically checkpoint their progress, allowing them to resume from the last checkpoint after a restart or failure.
  • Built-in Patterns: Provides implementations for common distributed application patterns like Function Chaining, Fan-out/Fan-in, Async HTTP APIs, and Monitoring.
  • Bindings: Integrates seamlessly with other Azure services through input and output bindings.
  • Scalability: Leverages the scalability of Azure Functions to handle a high volume of workflows.

Getting Started

To start using Durable Functions, you need an Azure subscription and the Azure Functions Core Tools installed. Follow these steps to set up your first Durable Functions project:

  1. Create a new Azure Functions project:
  2. func init MyDurableProject --worker-runtime  --docker
  3. Add a Durable Functions extension:
  4. This is typically included by default in newer templates. If not, you might need to add it via NuGet (for C#) or npm/pip.

  5. Create your first Orchestrator and Activity functions:
  6. Use the Azure Functions templates or create them manually.

    func new --template "Durable Functions orchestrator" --name MyOrchestrator
    func new --template "Durable Functions activity" --name MyActivity
  7. Run your functions locally:
  8. func start

You can then deploy your project to Azure Functions.

View Quickstart Guide

Core Concepts

Orchestrator Functions

Orchestrator functions are the heart of your stateful workflows. They are written in a specific way to be replayable. When an orchestrator function is replayed, it only executes the code that has not yet completed. This replay mechanism is fundamental to how Durable Functions achieve durability without complex state management code.

// Example in C#
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using System.Threading.Tasks;

public static class MyOrchestrator
{
    [Function("MyOrchestrator")]
    public static async Task RunOrchestrator(
        [OrchestrationTrigger] IDurableOrchestrationContext context)
    {
        string result1 = await context.CallActivityAsync("MyActivity_1", "Tokyo");
        string result2 = await context.CallActivityAsync("MyActivity_2", result1);
        string finalResult = await context.CallActivityAsync("MyActivity_3", result2);
        return finalResult;
    }
}

Activity Functions

Activity functions perform the actual work. They are regular Azure Functions triggered by an orchestrator. They can be called multiple times within an orchestration.

// Example in C#
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using System;

public static class MyActivities
{
    [FunctionName("MyActivity_1")]
    public static string MyActivity_1([ActivityTrigger] string name)
    {
        return $"Hello, {name}!";
    }

    [FunctionName("MyActivity_2")]
    public static string MyActivity_2([ActivityTrigger] string input)
    {
        Console.WriteLine($"Processing: {input}");
        return input.ToUpper();
    }

    [FunctionName("MyActivity_3")]
    public static string MyActivity_3([ActivityTrigger] string input)
    {
        return $"Final processed: {input}";
    }
}

Client Functions

Client functions are responsible for starting, querying, and managing orchestrations. They are typically triggered by HTTP requests or other events.

Common Patterns

Durable Functions provides built-in support for several common distributed application patterns:

  • Function Chaining: Execute a sequence of functions, where each function's output is the input for the next.
  • Fan-out/Fan-in: Parallelize work by calling multiple activity functions concurrently and then aggregating their results.
  • Async HTTP APIs: Implement long-running operations that can be polled via HTTP.
  • Monitoring: Periodically check the status of an operation or resource.
  • Human Interaction: Orchestrate workflows that require human approval or input.
Explore Pattern Examples

Learn More

Dive deeper into the world of Durable Functions with our comprehensive documentation and resources.