Loading...
Page 1 of 6
Introduction
Azure Durable Functions is an extension to Azure Functions that enables the creation of stateful workflows in a serverless environment. This document provides an overview of its features, workflows, and basic concepts.
What is it?
- An extension to Azure Functions.
- Write "stateful" functions in a "serverless" environment.
- Define workflows in code.
Why?
Assume these workflows:
i) Function Chaining Workflow
sequenceDiagram Func1 ->> Func2: M1 Func2 ->> Func3: M2
ii) Fan-out Fan-in Workflow
graph TD A[Entry Point] --> B[Func1] A --> C[Func2] A --> D[Func3] B --> E[Aggregate Data] C --> E D --> E
- Hard to do with normal functions.
- Introduces errors. This now becomes harder.
Page 2 of 6
Features of Durable Functions
- Parallel execution.
- Error handling.
- Easily understand "Orchestrator Function."
- Suitable for human workflows.
- Solves the state problem:
- Track workflow progress.
Basic Concepts
1. Orchestrator Functions
- Define the workflow.
- Trigger Activity Functions.
- Sleeps during activities.
- Wakes up when an activity is completed and starts triggering a new activity.
2. Activity Functions
- Performs a single step in a workflow.
- Individual Azure Functions.
3. Storing Orchestrations
- Durable Client binding.
Example:
sequenceDiagram Durable Client ->> Orchestrator Function: Trigger Orchestrator Function ->> Activity Function: Steps Activity Function --> Durable Client: Sequence/Parallel
Page 3 of 6
Additional Notes
Orchestrator Functions/Durable Functions use Azure Storage internally (called Task Hub).
Examples:
- Storage Queues:
- Messages to trigger the next function.
- Storage Tables:
- Store state of orchestrations.
- Event Sourcing:
- Append new rows.
- Store full execution history.
Orchestrator Function Rules/Constraints
Must be deterministic:
- It is because the whole function will be "replayed."
Don't:
- Use current date time.
- Generate random numbers/UUIDs.
- Access data stores.
Do:
- Use
IDurableOrchestrationContext.CurrentUtcDateTime. - Pass configuration into your orchestrator function.
- Retrieve data in activity functions.
Page 4 of 6
General Guidelines
-
Must be non-blocking:
- No I/O to disk or network.
- No Thread.Sleep.
-
Do not initiate async operations:
- Except on
IDurableOrchestrationContextAPI. - No
Task.Run,Task.Delay,Task.WhenAll.
- Except on
-
Avoid infinite loops:
- Use
"ContinueAsNew"to handle iterations.
- Use
-
Logging:
- Use
ILogger. - Example:
log = CreateReplaySafeLogger(log);
- Use
Monitoring Workflows
-
Workflow operations:
- Get status.
- Send event.
- Terminate event.
These operations rely on APIs obtained from the initial request.
Handling Errors
-
Error handling:
- Handle errors in Activity Function (Preferred).
- Handle errors in Orchestrator:
- Less chances of issues as orchestration is deterministic.
- Example: Retry mechanism.
CallActivityWithRetryAsync();
-
Termination:
- To cancel, call the terminate event API.
Page 5 of 6
Fan-Out / Fan-In Pattern
-
Issues in self-implementation:
- Race condition.
- Getting stuck forever.
-
Recommended approach:
- Use
Task.WhenAll().
- Use
-
Activity Function:
- Use multi-threading for parallelism.
Sub-Orchestrations
-
Creating sub-orchestrations:
- Call orchestrator from another orchestration.
- Create a sub-orchestration for specific steps only (e.g., Fan-Out pattern).
-
Code examples:
CallSubOrchestratorWithRetriesAsync(); CallSubOrchestratorAsync();
Waiting for Human Interaction (External Event)
Process Overview
sequenceDiagram
participant Process as Process Uploaded Video
participant Supervisor as Send Email to Supervisor
participant Response as Wait for Response (from Supervisor)
participant Publish as Publish or Reject
Process ->> Supervisor: Send Email
Supervisor ->> Response: Wait for Response
Response ->> Publish: Publish/Reject
- Use
ctx.WaitForExternalEvent. - Timeout handling:
- Add a timespan in
WaitForExternalEventcall. - Catch timeout exceptions.
- Add a timespan in
Page 6 of 6
External Orchestrations
-
Infinite loops:
- Avoid writing infinite loops as they add an infinite list of events.
-
Restart orchestration:
- Use
"ContinueAsNew"to restart with input data from the old instance. - Example:
CreateTimer();
- Use
-
Ending orchestration:
- Use
"TerminatePostURI". - Implement proper exception handling.
- Use
Production Bits
-
Deployment:
- Use ARM Templates, Visual Studio Deployment, or ADO pipelines.
-
Security:
- Orchestration and Activity Functions are called directly.
- Ensure entry points are secure.
References & Related Topics
- Azure Durable Functions Documentation
- Patterns for Azure Serverless Applications.
- Error Handling in Distributed Systems.
- Event sourcing and workflow orchestration.