Bird
Raised Fist0
Azurecloud~30 mins

Durable Functions for workflows in Azure - Mini Project: Build & Apply

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
Durable Functions for workflows
📖 Scenario: You are building a simple workflow using Azure Durable Functions to manage a multi-step process. This workflow will orchestrate two tasks: sending a welcome email and updating a database record. Durable Functions help keep track of the workflow state even if the system restarts.
🎯 Goal: Create a Durable Function workflow with an orchestrator function that calls two activity functions: one to send a welcome email and another to update a database record. The workflow should run these tasks in order.
📋 What You'll Learn
Create an orchestrator function named OrchestratorFunction.
Create two activity functions named SendWelcomeEmail and UpdateDatabaseRecord.
The orchestrator should call SendWelcomeEmail first, then UpdateDatabaseRecord.
Use the Durable Functions context to call activity functions.
Ensure the orchestrator returns a completion message.
💡 Why This Matters
🌍 Real World
Durable Functions are used to build reliable, stateful workflows in cloud applications, such as order processing, user onboarding, and batch jobs.
💼 Career
Understanding Durable Functions is valuable for cloud developers and architects working with Azure to create scalable and fault-tolerant workflows.
Progress0 / 4 steps
1
Create the activity function SendWelcomeEmail
Write a Python Azure Function named SendWelcomeEmail that takes a name parameter and returns the string "Welcome email sent to {name}".
Azure
Hint

Define an async function named main that takes name as a string parameter and returns the welcome message.

2
Create the activity function UpdateDatabaseRecord
Write a Python Azure Function named UpdateDatabaseRecord that takes a user_id parameter and returns the string "Database record updated for user {user_id}".
Azure
Hint

Define a new async function named update_database_record that takes user_id and returns the update message.

3
Create the orchestrator function OrchestratorFunction
Write a Python Azure Durable Functions orchestrator named OrchestratorFunction that calls the activity functions SendWelcomeEmail with "Alice" and then UpdateDatabaseRecord with "user123" in order. Use the context.call_activity method and return a string combining both results separated by a comma.
Azure
Hint

Use context.call_activity to call each activity function with the given parameters. Return a combined string of both results.

4
Complete the Durable Functions setup with function registrations
Add the necessary code to register the orchestrator function OrchestratorFunction and the activity functions main and update_database_record with Azure Functions using df.Orchestrator and df.Activity. Export these as main for each function.
Azure
Hint

Use df.Activity.create to register activity functions and df.Orchestrator.create to register the orchestrator function. Assign them to main or the function name as needed.

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