Bird
Raised Fist0
Azurecloud~5 mins

Durable Functions for workflows in Azure - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Durable Functions help you run tasks that take a long time or need to happen in order. They keep track of progress even if the computer restarts. This makes sure your work finishes without losing data.
When you want to run a process that has many steps, like ordering and shipping a product.
When you need to wait for a long time between steps, like waiting for a user to approve something.
When you want to retry a task if it fails without losing progress.
When you want to run multiple tasks at the same time and then combine their results.
When you want to keep track of a process that might stop and start again automatically.
Config File - function.json
function.json
{
  "bindings": [
    {
      "name": "context",
      "type": "orchestrationTrigger",
      "direction": "in"
    }
  ]
}

This file tells Azure Functions that this is an orchestration function. The orchestrationTrigger means it controls the workflow steps. The context is used to manage the workflow state and call other functions.

Commands
Create a new Azure Functions project using .NET runtime to build Durable Functions.
Terminal
func init DurableFunctionApp --worker-runtime dotnet
Expected OutputExpected
The project 'DurableFunctionApp' has been created.
→
--worker-runtime - Specifies the language runtime for the function app.
Add a new Durable Functions orchestration named OrderWorkflow to define the workflow steps.
Terminal
func new --name OrderWorkflow --template "Durable Functions Orchestration"
Expected OutputExpected
Created new Durable Functions Orchestration 'OrderWorkflow'.
→
--template - Specifies the function template to use.
Start the OrderWorkflow orchestration with input data containing order ID 123.
Terminal
func start-new OrderWorkflow --input "{\"orderId\":123}"
Expected OutputExpected
Started orchestration with instance ID: 12345abcde
Check the current status of the running workflow using its instance ID.
Terminal
func durable get-status 12345abcde
Expected OutputExpected
{ "runtimeStatus": "Running", "input": {"orderId":123}, "output": null }
Key Concept

If you remember nothing else from this pattern, remember: Durable Functions keep track of your workflow steps and state automatically, so your long or complex tasks never lose progress.

Common Mistakes
Not using the orchestration trigger binding in the function.json file.
Without the orchestration trigger, Azure won't treat the function as a workflow controller, so it won't manage state or call activity functions properly.
Always set the function.json binding type to 'orchestrationTrigger' for orchestration functions.
Starting the orchestration without providing required input data.
The workflow may fail or behave unexpectedly if it expects input but none is given.
Always provide the necessary input JSON when starting the orchestration.
Checking status with a wrong or missing instance ID.
You won't get the correct workflow status, making it hard to track progress.
Save the instance ID returned when starting the orchestration and use it to check status.
Summary
Initialize a new Azure Functions project with Durable Functions support.
Create an orchestration function that defines the workflow steps.
Start the orchestration with input data to run the workflow.
Check the workflow status anytime using the orchestration instance ID.

Practice

(1/5)
1. What is the main purpose of Durable Functions in Azure?
easy
A. To create virtual machines
B. To store large amounts of data
C. To monitor network traffic
D. To manage long-running workflows reliably

Solution

  1. Step 1: Understand Durable Functions role

    Durable Functions are designed to handle workflows that take a long time and need to be reliable.
  2. Step 2: Compare options

    Options A, B, and D describe other Azure services, not Durable Functions.
  3. Final Answer:

    To manage long-running workflows reliably -> Option D
  4. Quick Check:

    Durable Functions = Manage workflows reliably [OK]
Hint: Durable Functions = reliable long workflows [OK]
Common Mistakes:
  • Confusing Durable Functions with storage services
  • Thinking Durable Functions create VMs
  • Assuming Durable Functions monitor networks
2. Which of the following is the correct way to define an orchestrator function in Azure Durable Functions?
easy
A. function orchestrator(context) { /* workflow code */ }
B. function activity(context) { /* workflow code */ }
C. async function orchestrator(context) { /* workflow code */ }
D. async function activity(context) { /* workflow code */ }

Solution

  1. Step 1: Identify orchestrator function syntax

    Orchestrator functions must be async to support awaiting activity calls.
  2. Step 2: Differentiate activity and orchestrator functions

    Options C and D define activity functions, not orchestrators.
  3. Final Answer:

    async function orchestrator(context) { /* workflow code */ } -> Option C
  4. Quick Check:

    Orchestrator = async function [OK]
Hint: Orchestrators are async functions [OK]
Common Mistakes:
  • Using non-async function for orchestrator
  • Confusing activity function syntax with orchestrator
  • Omitting async keyword
3. Given the following orchestrator code snippet, what will be the output if the activity function returns 'Hello'?
const result = yield context.callActivity('SayHello');
return result + ' World!';
medium
A. "Hello World!"
B. "World! Hello"
C. "Hello"
D. "World!"

Solution

  1. Step 1: Understand callActivity result

    The activity function returns 'Hello', which is assigned to result.
  2. Step 2: Concatenate strings

    The orchestrator returns result + ' World!', so 'Hello' + ' World!' = 'Hello World!'.
  3. Final Answer:

    "Hello World!" -> Option A
  4. Quick Check:

    Result + ' World!' = 'Hello World!' [OK]
Hint: callActivity returns value used in concatenation [OK]
Common Mistakes:
  • Reversing string order
  • Returning only activity result without addition
  • Ignoring yield keyword effect
4. Identify the error in this orchestrator function code:
async function orchestrator(context) {
  const result = context.callActivity('Task');
  return result;
}
medium
A. Function should not be async
B. Missing await or yield before callActivity
C. Incorrect function name
D. callActivity should be called outside orchestrator

Solution

  1. Step 1: Check callActivity usage

    callActivity returns a promise and must be awaited or yielded inside orchestrator.
  2. Step 2: Identify missing await/yield

    The code calls callActivity without await or yield, causing incorrect behavior.
  3. Final Answer:

    Missing await or yield before callActivity -> Option B
  4. Quick Check:

    callActivity needs await/yield [OK]
Hint: Always await or yield callActivity in orchestrator [OK]
Common Mistakes:
  • Forgetting await/yield on callActivity
  • Thinking async keyword is wrong here
  • Misplacing callActivity outside orchestrator
5. You want to create a workflow that calls two activity functions in sequence and combines their results. Which orchestrator code correctly implements this?
hard
A. const result1 = await context.callActivity('Activity1'); const result2 = await context.callActivity('Activity2'); return result1 + ' & ' + result2;
B. const result1 = context.callActivity('Activity1'); const result2 = context.callActivity('Activity2'); return result1 + ' & ' + result2;
C. const result1 = await context.callActivity('Activity1'); const result2 = context.callActivity('Activity2'); return result1 + ' & ' + result2;
D. const result1 = context.callActivity('Activity1'); const result2 = await context.callActivity('Activity2'); return result1 + ' & ' + result2;

Solution

  1. Step 1: Understand sequential calls in orchestrator

    Each callActivity must be awaited to get the actual result before next call.
  2. Step 2: Check options for proper await usage

    Only const result1 = await context.callActivity('Activity1'); const result2 = await context.callActivity('Activity2'); return result1 + ' & ' + result2; awaits both calls, ensuring correct sequence and result combination.
  3. Final Answer:

    const result1 = await context.callActivity('Activity1');\nconst result2 = await context.callActivity('Activity2');\nreturn result1 + ' & ' + result2; -> Option A
  4. Quick Check:

    Await both activities for correct sequence [OK]
Hint: Await each callActivity for sequential results [OK]
Common Mistakes:
  • Not awaiting callActivity causing promises instead of results
  • Mixing awaited and non-awaited calls
  • Returning promises instead of strings