wasAsked.

Azure Durable Functions – Orchestrator and Activity Functions

EPAM · Role not specified · technical

Published

Year not specified · Azure

Community answers

Posted by wasAsked editorial collection

Durable Functions is an extension of Azure Functions for writing stateful workflows in code. It persists progress automatically, so long-running processes survive restarts and can wait for days. Function types - Client (starter) function: a normal trigger (HTTP, queue) that starts an orchestration and returns status URLs. - Orchestrator function: defines the workflow — the order of steps, branching, waiting and error handling. It calls activities and other orchestrations. - Activity function: performs the actual work: calling APIs, database writes, sending emails. Activities can do any I/O. - Entity functions: small stateful objects (durable entities) for things like counters or aggregators. Example (isolated worker model): [Function(nameof(ProcessOrder))] public static async Task<string> ProcessOrder([OrchestrationTrigger] TaskOrchestrationContext ctx) { var order = ctx.GetInput<Order>()!; await ctx.CallActivityAsync(nameof(ReserveStock), order); var paid = await ctx.CallActivityAsync<bool>(nameof(ChargePayment), order); if (!paid) { await ctx.CallActivityAsync(nameof(ReleaseStock), order); // compensation return "Payment failed"; } await ctx.CallActivityAsync(nameof(SendConfirmation), order); return "Completed"; } [Function(nameof(ReserveStock))] public static Task ReserveStock([ActivityTrigger] Order order) => /* call inventory API */; How it works: event sourcing and replay - The framework records every step in a history store (Azure Storage, or the newer Durable Task Scheduler or MSSQL backends). - After each await, the orchestrator may unload; when an activity completes, the orchestrator is replayed from the beginning, using recorded results instead of re-running completed activities. Orchestrator rules (because of replay) - Code must be deterministic: no DateTime.Now (use ctx.CurrentUtcDateTime), no Guid.NewGuid (use ctx.NewGuid), no random numbers, and no direct I/O — put all I/O in activities. - Use ctx.CreateTimer for delays instead of Task.Delay. Patterns it supports - Function chaining, fan-out/fan-in (run activities in parallel and aggregate results), async HTTP APIs with status polling, monitors, human interaction (WaitForExternalEvent with timeouts), and sagas with compensation. Make activities idempotent, because they can be retried.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics